简体   繁体   English

如何在.click()上解除绑定另一个jQuery函数?

[英]How do I unbind another jQuery function on .click()?

I have this script that run to fix my menu bar to the browser on scroll. 我运行此脚本可以将菜单栏固定在滚动浏览器上。 Nothing really needs to change here (works as it should). 此处实际上不需要进行任何更改(应正常运行)。 However, you may need it... 但是,您可能需要它...

var div = $('#wizMenuWrap');
var editor = $('#main_wrapper');
var start = $(div).offset().top;

$(function fixedPackage(){   
    $.event.add(window, "scroll", function() {
        var p = $(window).scrollTop();
        $(div).css('position',((p)>start) ? 'fixed' : 'static');
        $(div).css('top',((p)>start) ? '0px' : '');

        //Adds TOP margin to #main_wrapper (required)
        $(editor).css('position',((p)>start) ? 'relative' : 'static');
        $(editor).css('top',((p)>start) ? '88px' : '');
    }); 
});

Now for the issue at hand. 现在解决当前的问题。 I have another script function that calls a modal pop-up (which again works as it should). 我有另一个脚本函数,该脚本函数调用模式弹出窗口(再次可以正常工作)。 However, it's not slick from a UI perspective when I scroll the page when the modals open. 但是,从模态打开时滚动页面时,从UI角度来看并不是一件难事。 So I want to disable the script above when the modal script below is called. 所以我想在调用下面的模式脚本时禁用上面的脚本。 In other words, when I click to open the modal pop-up, the script above shouldn't work. 换句话说,当我单击以打开模式弹出窗口时,上面的脚本不起作用。

$(function () {

var setUp = $('.setupButton');

// SHOWS SPECIFIED VIEW

$(setUp).click(function () {        
    $('#setupPanel').modal('show');
    //PREVENTS PACKAGE SELECT FIXED POSITION ON SCROLL
    $(setUp).unbind('click',fixedPackage);
});

})

As you can see above, I tried to unbind the scroll function (the first code snippet), but this is not correct. 如您在上面看到的,我尝试解除绑定滚动功能(第一个代码段),但这是不正确的。

These two scripts are in two separate js libraries. 这两个脚本位于两个单独的js库中。

I strongly disagree that you ought to be binding and unbinding the event. 我强烈不同意您应该绑定和取消绑定该事件。 There's no need! 没必要! A little logic in your scroll event to check to see if the modal is open should take care of the issue: 滚动事件中的一些逻辑检查模式是否打开应该可以解决此问题:

$(function fixedPackage(){   
    $(window).bind("scroll", function() {

        // if the modal is displayed, do nothing
        if ($('#setupPanel').is(':visible'))
            return;

        // -- existing code here --
    }); 
});

This way, if the modal element is visible, the code simply stops where it is. 这样,如果模态元素可见,则代码仅在其所在位置停止。 Once you hide the element, the code will continue to work as before without having to manage the state of event in some other script... confusing! 隐藏元素后,代码将继续像以前一样工作,而无需在其他脚本中管理事件的状态……令人困惑!

Also, as mentioned in some other comments, don't use $.event.add , use the public API method bind 另外,如其他评论中所述,请勿使用$.event.add ,而应使用公共API方法bind

Documentation 文档

When you store a jquery object into a var you can call functions directly: 将jquery对象存储到var中时,可以直接调用函数:

var setUp = $('.setupButton');
var div = $('#wizMenuWrap');
var editor = $('#main_wrapper');

setUp.click(...);
seTup.unbind(...);

editor.css(...);

div.css(...);

尝试

  $(setUp).unbind('click').die('click')

all you need to do is change your script to: 您需要做的就是将脚本更改为:

$(function () {

var setUp = $('.setupButton');

// SHOWS SPECIFIED VIEW

$(setUp).bind('click',function () {        
    $('#setupPanel').modal('show');
    //PREVENTS PACKAGE SELECT FIXED POSITION ON SCROLL
    $(setUp).unbind('click');
});

})

As explained in the jQuery Docs, Event handlers attached with .bind() can be removed with .unbind() . 如jQuery Docs中所述, Event handlers attached with .bind() can be removed with .unbind() For more information about bind and unbind: 有关绑定和取消绑定的更多信息:

.bind() .bind()

.unbind() .unbind()

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

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