如何解决单击时跟踪位置
| 如何跟踪以百分比表示的元素中单击的位置? 基本上,它是用于滑块的...并且一旦单击滑块,我想跟踪针的移动位置(以%表示CSSin0ѭ属性)。解决方法
尝试这个:
<html>
<head>
<script type=\"text/javascript\" src=\"jquery.js\"></script>
<script type=\"text/javascript\">
jQuery(document).ready(function(){
$(\"#special\").click(function(e){
var xP = e.pageX * 100 / $(e.target).width();
var yP = e.pageY * 100 / $(e.target).height();
$(\'#status2\').html(xP +\',\'+ yP);
});
})
</script>
<body>
<h2 id=\"status2\">
0,0
</h2>
<div style=\"width: 100px; height: 100px; background:#ccc;\" id=\"special\">
Click me anywhere!
</div>
</body>
</html>
, 设法做到这一点:
$(\'#song-seeker\').click(function(e){
var offset = $(this).offset(); // somehow this.offsetLeft didn\'t work
var x = Math.floor(e.pageX - offset.left); // calculates pixel value of position clicked within element
x = Math.floor(x * 100 / $(this).width()); // transform to % value based on full clicked elements width
// Math.floor() used to prepare values with 0 values behind comma,therefore,CSS safe.
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。