繁体   English   中英

在jQuery中删除$(window).resize事件

[英]Removing the $(window).resize Event in jQuery

我正在开发的页面的一部分需要在用户单击按钮时将$(窗口).resize事件添加到div,以便在用窗口调整大小并将其固定为原始大小之间切换:

function startResize() {
    $(window).resize(function() {
        $("#content").width(newWidth);
        $("#content").height(newHeight);
    });
}

我无法解决的是,当再次单击该按钮时,如何“关闭”此事件,以便内容停止调整大小。

function endResize() {
    // Code to end $(window).resize function
    $("#content").width(originalWidth);
    $("#content").height(originalHeight);
}

任何有关这方面的帮助将不胜感激。

function endResize() {
    $(window).off("resize");
    $("#content").width(originalWidth);
    $("#content").height(originalHeight);
}

请注意,这非常突兀,可能会破坏其他代码。

这是更好的方法:

function resizer() {
    $("#content").width(newWidth);
    $("#content").height(newHeight);
}

function startResize() {
    $(window).resize(resizer);
}

function endResize() {
    $(window).off("resize", resizer);
}
function startResize() {
   $(window).on("resize.mymethod",(function() {
     $("#content").width(newWidth);
     $("#content").height(newHeight);
   }));
}

function endResize() {
   $(window).off("resize.mymethod");
}

在查询方法上使用命名空间将允许仅为您的方法关闭resize事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM