谁能告诉我这段代码有什么问题吗?它只会使我的浏览器崩溃,仅此而已......
$(window).resize(function() {
var p = $("#outer_wrap");
var offset = p.offset();
alert("left: " + offset.left + ", top: " + offset.top);
});
最佳答案
尽量不要使用alert
函数,而是使用console.log
(确保控制台窗口打开),这对我有用。这可能是由于跨浏览器处理 resize
事件引起的(某些浏览器在调整窗口大小时不断发送该事件。
来自jQuery
:
Code in a resize handler should never rely on the number of times the handler is called. Depending on implementation, resize events can be sent continuously as the resizing is in progress (the typical behavior in Internet Explorer and WebKit-based browsers such as Safari and Chrome), or only once at the end of the resize operation (the typical behavior in some other browsers such as Opera).
关于javascript - ".resize"事件与 't work with "偏移量“在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9989405/