简体   繁体   English

jQuery单击div的边框

[英]jQuery click on border of a div

I have a div that scrolls with a lot of text in it. 我有一个div,其中包含大量文本。 My question is, is it possible to detect a click on the border of the div? 我的问题是,是否有可能检测到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. 我想要完成的是如果用户点击底部边框(使用CSS设置4px宽的样式),div会一直滚动到底部。 Is this even possible without adding more markup? 如果不添加更多标记,这是否可行?

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). .outerHeight()只返回内容的高度(包括边框)。 The e.offsetY returns the clicked Y relative to the element. e.offsetY返回相对于元素的单击Y. 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 . 注意关于outerHeight ,如果传递一个bool true参数,它将在计算值中包含margin ,默认为false ,所以它只返回content height + padding + border

UPDATE : Looks like FireFox has its own way of behavior. 更新 :看起来FireFox有自己的行为方式。 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). 但看起来我们没有方便的方法在所谓的FireFox中获取坐标,因为e.offsetXe.offsetY根本不起作用(没有值)。 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. 相反,你必须使用pageXpageY减去.offset().left.offset().top 分别获得相对于元素的坐标。

Updated demo 更新的演示

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 jQuery在元素中获取鼠标位置

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. 我不确定盒子尺寸是如何适应这一点的,但这就是我如何开始。

You have a wrapper around your element and set the padding to what ever you want to be detected. 您的元素周围有一个包装器,并将填充设置为您想要检测的内容。

jQuery 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 香草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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM