5

I have a div that scrolls with a lot of text in it. My question is, is it possible to detect a click on the border of the div?

What I'd like to accomplish is if the user clicks on the bottom border (which is styled 4px wide with CSS), the div scrolls all the way to the bottom. Is this even possible without adding more markup?

Andrew
  • 763
  • 7
  • 21

3 Answers3

8

You can try this:

$('div').click(function(e){
  if(e.offsetY >$(this).outerHeight() - 4){
    alert('clicked on the bottom border!');
  }
});

Demo.

The .outerHeight() just returns the height of the content (including border). The e.offsetY returns the clicked Y relative to the element. Note about the outerHeight, if passing a bool true argument, it will include margin in the calculated value, the default is false, so it just returns content height + padding + border.

UPDATE: Looks like FireFox has its own way of behavior. You can see that when clicking, holding mouse down on an element, it's very natural and convenient to know the coordinates of the clicked point relative to the element. But looks like we have no convenient way to get that coordinates in the so-called FireFox because the e.offsetX and e.offsetY simply don't work (have no value). Instead you have to use the pageX and pageY to subtract the .offset().left and .offset().top respectively to get the coordinates relative to the element.

Updated demo

King King
  • 61,710
  • 16
  • 105
  • 130
  • Perfect! Could you explain what e.offsetY is? – Andrew Jun 14 '14 at 06:57
  • @Andrew I've already said in my answer, it returns the Y coordinate of the clicked point. To get the X coordinate, you use the `offsetX`. – King King Jun 14 '14 at 07:03
  • Oh. *facepalm.* I totally should've read the whole thing. Sorry, I was excited. – Andrew Jun 14 '14 at 07:04
  • @Andrew it's fine, if you feel it's helpful and does actually solve your problem, just mark it as the accepted answer, that way it's more distinct to be some future reference for other users. – King King Jun 14 '14 at 07:16
  • 1
    Done! (Sorry, I'm a bit new to this site.) Thanks! – Andrew Jun 14 '14 at 07:28
  • Hey, this doesn't work in firefox. Is there any way to fix that? (Not sure about IE, I refuse to touch that browser) – Andrew Jun 14 '14 at 09:56
  • @Andrew FireFox really sucks, actually I've tested even a simple `alert('???')` (without any other code) in `onload`, it does not work at all. Looks like there is something wrong with jsfiddle. I've just made another demo in jsbin instead: http://jsbin.com/nisitece/1/ (see my updated explanation for detail) – King King Jun 14 '14 at 10:21
  • 1
    Thanks for the update! Your updated version is also working in IE9 (first one wasn't). Really great answer! – Andrew Jun 15 '14 at 15:32
0

I never tried it, But I don't see why it shouldn't work :

  1. Calculate the height of the element.

  2. calculate the bottom border

  3. calculate the offset inside the element itself, like in here

    jQuery get mouse position within an element

Now you can check if the mouse position is inside the bottom border using some math.
I'm not sure how box-sizing fits into this, But that's how I would start around.

Community
  • 1
  • 1
Patrick
  • 3,289
  • 2
  • 18
  • 31
0

You have a wrapper around your element and set the padding to what ever you want to be detected.

jQuery

$("#border").click(function (e) {
  if(e.target !== e.currentTarget) return;
  console.log("border-clicked")
});
#border {
  padding: 4px;
  background: blue;
  box-sizing: border-box;
  cursor: pointer;
}

.box{
  height: 100px;
  width: 100%;
  background: white;
  cursor: default;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="border">
<div class="box"></div>
</div>

Vanilla JS

var border = document.getElementById("border");

border.onclick = function(e) {
  if(e.target !== e.currentTarget) return;
  console.log("border-clicked")
}
#border {
  padding: 4px;
  background: blue;
  box-sizing: border-box;
  cursor: pointer;
}

.box{
  height: 100px;
  width: 100%;
  background: white;
  cursor: default;
}
<div id="border">
<div class="box"></div>
</div>
Chris Happy
  • 7,088
  • 2
  • 22
  • 49
  • I don't think it'll necessarily avoid *many* lines of code, but I do think it's possibly simpler to do this way, yes. I did originally say "without more markup" in the question, but in hindsight I don't know why I was so opposed to extra markup. I'd probably use this solution if I ever needed to implement this functionality again. – Andrew Apr 26 '17 at 14:25