JS-是否可以获得元素单击位置的确切宽度

JS - Is it possible to get the exact width of the element where it clicked?

本文关键字:位置 单击 元素 是否 JS-      更新时间:2023-09-26

对于音乐进度条,如youtube进度条。我只想将它与JS/JQuery一起使用。

例如,如果我有这样的代码:

<div style="pointer:cursor;width:250px;background:#cccccc">&nbsp;</div>

假设我点击这个div元素的一半,我希望JS返回125px。有什么办法吗?

试试看:

$('div').on('click',function(e){
    console.log(e.clientX - this.offsetLeft);
});