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?
You can try this:
Demo.
The
.outerHeight()
just returns the height of the content (including border). Thee.offsetY
returns the clicked Y relative to the element. Note about theouterHeight
, if passing a booltrue
argument, it will includemargin
in the calculated value, the default isfalse
, so it just returnscontent 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
ande.offsetY
simply don't work (have no value). Instead you have to use thepageX
andpageY
to subtract the.offset().left
and.offset().top
respectively to get the coordinates relative to the element.Updated demo
You have a wrapper around your element and set the padding to what ever you want to be detected.
jQuery
Vanilla JS
I never tried it, But I don't see why it shouldn't work :
Calculate the height of the element.
calculate the bottom border
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.