简体   繁体   English

在div中检测单击的水平位置

[英]Detecting clicked horizontal position in a div

When i click on streaming bar, position will be detected from left edge of the browser window but it has to be detected from left edge of the #progressBar div in this demo . 当我单击流媒体栏时,将在浏览器窗口的左边缘检测到位置,但在此演示中必须从#progressBar div的左边缘检测到位置。 So, because of positioning #progressBar div with left: 200px; 因此,由于将#progressBar div定位为left: 200px; 200px is added on horizontal position that is clicked. 在单击的水平位置上添加200px。

My simple detecting function: 我简单的检测功能:

function point_it(e){
      var x=e.clientX; 
      var seekSecond = Math.floor((x/1100) * ytplayer.getDuration()); //1100 is width of the progress bar 
      seekTo(seekSecond);
      document.getElementById("xPos").innerHTML=x;
    }

style: 样式:

    #progressBar{
position: relative;
top: 400px;
left: 200px;
width: 1100px;
height: 4px;
border: 2px solid gray;
margin: 10px;
z-index: 8;
}


#elapsedBar{
position: relative;
top: -1px;
width:0px;
height:3px;
border:1px solid;
border-color: #660033;
background-color: #660033;
margin:0px;
z-index: 10;
}

#loadedBar{
position: relative;
top: 0px;
width: 0px;
height:4px;
border:1px solid;
border-color: gray;
background-color: gray;
margin:0px;
z-index: 9;
}

You can subtract the left position from the clicked position, I only know the jQuery way of doing this: 您可以从单击位置中减去左侧位置,我只知道jQuery的方式:

// Get the "left" value
var leftPos = $("#progressBar").css("left");
// Remove "px"
var leftPos = leftPos.replace("px", "");

Then subtract leftPos from the value you are getting 然后从得到的值中减去leftPos

Just use e.offsetX in your function instead of e.clientX . 只需在函数中使用e.offsetX而不是e.clientX

e.client... is related to the browsers window, e.offsett... is related to the clicked target element. e.client...与浏览器窗口有关, e.offsett...与单击的目标元素有关。

You have to subtract 10 from x because of margin: 10px; 由于margin: 10px;您必须从x减去10 margin: 10px; . This causes var x=e.clientX; 这导致var x=e.clientX; to be 10 when clicking on the left side of the bar. 单击栏左侧时为10。

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

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