在jQuery中删除$(window).resize事件 [英] Removing the $(window).resize Event in jQuery
本文介绍了在jQuery中删除$(window).resize事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发的页面的一部分需要在用户单击按钮时将$(窗口).resize事件添加到div,以便在使用窗口调整其大小并将其固定为其原始位置之间切换大小:
Part of the page I'm developing requires a $(window).resize event to be added to a div when a user clicks a button, in order to toggle between resizing it with the window and leaving it fixed at its original size:
function startResize() {
$(window).resize(function() {
$("#content").width(newWidth);
$("#content").height(newHeight);
});
}
我无法解决的是如何关闭此事件再次单击该按钮时,内容将停止调整大小。
What I can't work out is how to "turn off" this event when the button is clicked again, so that the content stops resizing.
function endResize() {
// Code to end $(window).resize function
$("#content").width(originalWidth);
$("#content").height(originalHeight);
}
我们非常感谢您提供的任何帮助。
Any help with this would be greatly appreciated.
推荐答案
function endResize() {
$(window).off("resize");
$("#content").width(originalWidth);
$("#content").height(originalHeight);
}
请注意,这非常突兀,可能会破坏其他代码。
Note that this is extremely obtrusive and might break other code.
这是更好的方法:
function resizer() {
$("#content").width(newWidth);
$("#content").height(newHeight);
}
function startResize() {
$(window).resize(resizer);
}
function endResize() {
$(window).off("resize", resizer);
}
这篇关于在jQuery中删除$(window).resize事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文