简体   繁体   English

确定鼠标点击是否发生在DIV的左半部分或右半部分

[英]Determining if mouse click happened in left or right half of DIV

I was wondering if it's possible to calculate if a mouse click happened over the left or right half of the div element: 我想知道是否可以计算鼠标单元的左半部分还是右半部分是否发生了鼠标点击:

$("div").click(function(e){
 // calculate if click happened on left or right half
});


<div>Variable Content</div>

Was hoping there would be a way to get the relative coordinates and relate them to the width of the div? 希望有办法获得相对坐标并将它们与div的宽度联系起来?

$("div").click(function(e){
   var pWidth = $(this).innerWidth(); //use .outerWidth() if you want borders
   var pOffset = $(this).offset(); 
   var x = e.pageX - pOffset.left;
    if(pWidth/2 > x)
        $(this).text('left');
    else
        $(this).text('right');
});

DEMO: http://jsfiddle.net/dirtyd77/QRKn7/1/ 演示: http//jsfiddle.net/dirtyd77/QRKn7/1/

Hope this helps! 希望这可以帮助! Let me know if you have any questions! 如果您有任何疑问,请告诉我!

This should do it: 这应该这样做:

$("div").click(function(e){
    var $div = $(this);
    alert(e.pageX >= ($div.offset().left + $div.width()/2) ? 'clicked right' : 'clicked left');
});
var x = evt.pageX - $(this).offset().left

if (x > $(this).width()/2) {
  //Right half
} else {
  //Left half
}

So the full code would be 所以完整的代码将是

$("div").click(function(e){
   // calculate if click happened on left or right half
  var x = evt.pageX - $(this).offset().left

  if (x > $(this).width()/2) {
    //Right half
  } else {
    //Left half
  }
});

To get the position of mouse within, you can calculate the difference between the mouse position and the div offset. 要获得鼠标位置,您可以计算鼠标位置和div偏移之间的差异。 Then compare it with the half width of the div itself and voilà. 然后将它与div本身的半宽和voilà进行比较。

EDIT 编辑

$(function ()
{

    $("#test").click(function(e){
       var offset = $(this).offset(); 
       var pos_x = e.pageX - offset.left;
       var middle = $(this).outerWidth() / 2;

       if(pos_x < middle)
       {
           alert('left part');
       }
       else
       {
             alert('right part');
       }
    });

});

You can check it out here: 你可以在这里查看:

http://jsfiddle.net/kZuML/ http://jsfiddle.net/kZuML/

Fiddle because you know - YOLO! 小提琴,因为你知道 - YOLO!

$("#special").on('click', function(e){

    var x = e.pageX - this.offsetLeft;
    var y = e.pageY - this.offsetTop; //You probably don't need Y. Unless you want to know height as well.

    var width = $(this).width(),
        where = width / 2;
    if( x > where ){
        console.log("Click was on the right");
    } else {
        console.log("Click was on the left");
    }
});

PURE JAVASCRIPT solution-Joining late to the party. PURE JAVASCRIPT解决方案 - 加入聚会后期。 but recenlty done this using my code. 但是使用我的代码重新做到这一点。 on body tag or div give it ID and call javascript function eg. 在body标签或div上给它ID并调用javascript函数,例如。 id="tt" onclick="showCoords(event)" id =“tt”onclick =“showCoords(event)”

 function showCoords(event) {
        var x = event.clientX;
        var y = event.clientY;
    //    var coor = "X coords: " + x + ", Y coords: " + y;
    //    document.getElementById("demo").innerHTML = coor;
        var ele = document.getElementById("tt");
        var width = ele.offsetWidth;
        var height = ele.offsetHeight;
        var half=(width/2);
       if(x>half)
        {
            alert('right click');

        }
        else
       {
           alert('left click');

        }


    }

A working JS solution: 一个有效的JS解决方案

onClick(e) {
    const clickTarget = e.target;
    const clickTargetWidth = clickTarget.offsetWidth;
    const xCoordInClickTarget = e.clientX - clickTarget.getBoundingClientRect().left;
    if (clickTargetWidth / 2 > xCoordInClickTarget) {
      // clicked left
    } else {
      // clicked right
    }
  }

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

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