0
$('.ccc').mousemove(function (e) {
        $('.ddd').text(" " + (e.pageX - $(this).offset().left - 0.5) + ", " + (e.pageY - $(this).offset().top) + " ");
    });

http://jsfiddle.net/n8rna/9/

这个函数的问题是 Firefox 和 Opera 计算的 div 边框略有不同,所以我在这些浏览器中有 0.5(X 方向)的差异。

那么我怎样才能在这个函数中只使用整数呢?

我想将数字四舍五入是最简单的解决方案,但我也愿意接受其他方法。

4

1 回答 1

2

Use the round function:

Math.round(2.3): // Equal to 2

If It's FF and Chrome causing you problems, you may need to check the user agents before you do the rounding. However, this seems like something that could be solved in another, less hacky, way.

于 2013-09-20T14:46:19.567 回答