简体   繁体   English

jQuery函数销毁滚动事件

[英]jquery function destroy on scroll event

I have build so far a jquery function that stick html element to the top of the page when scrolling but destroy on scroll event is not working. 到目前为止,我已经建立了一个jquery函数,在滚动时将html元素粘贴到页面顶部,但在scroll事件上销毁则不起作用。 I know that i should store the function for scroll event in variable but i dont know how to destroy it later. 我知道我应该将滚动事件的函数存储在变量中,但是我不知道以后如何销毁它。 Can someone help me? 有人能帮我吗? This is my code so far: 到目前为止,这是我的代码:

(function ( $ ) {

$.fn.sticky = function(data) {
    var settings = $.extend({
        parent: null,
        offsetTop: 0,
        offsetBottom: 0,
        fixBottom: true,
        destroy: false
    }, data );

    this.each(function() {
        var elm = $(this),
        parent = $(settings.parent);

        if(settings.parent === null) {
            parent = elm.parent();
        }

        var elmTop = elm.offset().top,
            elmLeft = elm.offset().left,
            elmWidth = elm.width(),
            elmHeight = elm.height(),
            parentTop = parent.offset().top,
            parentHeight = parent.outerHeight(true),
            offs = elmTop - settings.offsetTop,
            margin_top = elmTop - parentTop,
            margin_bottom = parentHeight - elmHeight - margin_top,
            diff = parentHeight - elmHeight + parentTop - settings.offsetTop - settings.offsetBottom;

        var sticky_elm = function() {

            var w_top = $(window).scrollTop();

            if(w_top > offs && w_top < diff) {
                elm.attr("style", "position: fixed; top: "+settings.offsetTop+"px; left: "+elmLeft+"px; margin-left: 0;");
                if(!elm.next('[data-sticket-controller]').length) {
                    elm.after('<div data-sticket-controller style="visibility: hidden; opacity: 0; position: relative; width: '+elmWidth+'px; height: '+elmHeight+'px;"></div>');
                }
            }
            else if(w_top >= diff) {
                if(settings.fixBottom) {
                    elm.attr("style", "position: absolute; bottom: "+settings.offsetBottom+"px; left: "+elmLeft+"px; margin-left: 0;");
                }
            }
            else {
                elm.removeAttr("style");
                elm.next("[data-sticket-controller]").remove();
            }
        }

        if(settings.destroy) {
            $(document).off("scroll", sticky_elm);
        }
        else {
            $(document).on("scroll", sticky_elm);
        }
    });
};

}( jQuery ));

An event namespace would be useful in this case since the event listener is on the $(document) itself, so I have updated the fiddle with event namespace and a unique identifier for each sticky element so that we can turn off the event listener at any time later as long as we have the sticky element ID. 在这种情况下,事件名称空间会很有用,因为事件侦听器位于$(document)本身,所以我用事件名称空间和每个粘性元素的唯一标识符更新了小提琴,以便我们可以在任意位置关闭事件侦听器时间过后,只要我们有粘性元素ID。

https://jsfiddle.net/c289wLe1/11/ https://jsfiddle.net/c289wLe1/11/

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

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