jQuery $(document).on(“ scroll”)disable

时间:2018-07-11 16:46:39

标签: jquery sticky onscroll

到目前为止,我已经构建了一个函数,可以在滚动时将html元素粘贴到页面顶部,但是我想在其中创建一个函数,以便为每个分配的元素销毁$(document).on(“ scroll”)。有人可以帮助我实现这一目标吗? 到目前为止,我的代码是:

$(element).each(function() {
    var default_settings = {
        parent: null,
        offsetTop: 0,
        offsetBottom: 0,
        fixBottom: true
    };

    var elm = $(this),
        parent = $(settings.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;

    $(document).on("scroll", 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();
        }
    }
    });
});

0 个答案:

没有答案