简体   繁体   English

从父右边获取位置,以%为单位

[英]Get position from parent right edge in %

Given a clientX coordinate, how can I determine how far that point is from the right edge of the parent container? 给定一个clientX坐标,我如何确定该点距父容器的边缘有多远? I can do left edge correctly with 我可以正确地做左边缘

event.target.style.left = (((cX-event.target.parentNode.offsetLeft)) / event.target.parentNode.clientWidth)*100 +"%";

but can't seem to figure out the values to compare with, in relation to the right edge.. (edited) 但似乎无法找出相对于右边缘要比较的值。(已编辑)

or, actually, how far the right edge of event.target is from the right, since style.right positions from the right edge cX is the clientX of a JQuery UI draggable stop callback's event 或实际上,event.target的右边缘距离右边缘多远,因为style.right位置从右边缘cX是JQuery UI可拖动停止回调事件的clientX

Try using the getClientBoundingRect() 尝试使用getClientBoundingRect()

  function getPosition(e) {
      var rect = e.target.getBoundingClientRect();
      console.log(rect)
      var x =  Math.abs(e.clientX - rect.right);
      var y = e.clientY - rect.top;
      return {
        x,
        y,
        procent: x / (rect.width / 100)  
      }
   }

Example: https://jsfiddle.net/bogdanm/ocLn138v/ 示例: https//jsfiddle.net/bogdanm/ocLn138v/

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

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