Removing the $(window).resize Event in jQuery

后端 未结 2 1258
情书的邮戳
情书的邮戳 2020-12-24 05:13

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 a

相关标签:
2条回答
  • 2020-12-24 05:54
    function endResize() {
        $(window).off("resize");
        $("#content").width(originalWidth);
        $("#content").height(originalHeight);
    }
    

    Note that this is extremely obtrusive and might break other code.

    This is better way:

    function resizer() {
        $("#content").width(newWidth);
        $("#content").height(newHeight);
    }
    
    function startResize() {
        $(window).resize(resizer);
    }
    
    function endResize() {
        $(window).off("resize", resizer);
    }
    
    0 讨论(0)
  • 2020-12-24 06:02
    function startResize() {
       $(window).on("resize.mymethod",(function() {
         $("#content").width(newWidth);
         $("#content").height(newHeight);
       }));
    }
    
    function endResize() {
       $(window).off("resize.mymethod");
    }
    

    using a namespace on the query method will allow to to turn off the resize event for you method only.

    0 讨论(0)
提交回复
热议问题