当调用媒体查询时,我的功能仍在影响DIV的位置,我想停止它



我正在使用一个使元素固定在滚动上的函数,当我触发媒体查询时,我不希望此函数仍然受到影响。

function scroll_fixbar() {
    var topBar = $("#latestWrapper");
    if ($(this).scrollTop() > 145) {
        if (topBar.css('position') !== 'fixed') {
            topBar.css("position", "fixed");
            topBar.css("top", "0px");
        }
    } else {
        if ($("#AddFixedLatest").css('position') !== 'static') {
            topBar.css("position", "absolute");
            topBar.css("top", "145px");
        }
    }
}
function media_query(obj) {
    size = obj();
    var pathname = window.location.href;
    if (size != currentSize) {
        var sideBar = $('#sidebarWrapper');
        var wrapper = $('#wrapper');
        var menuBtn = $('.menuButton', '#menuOption');
        if (size == 'mobile') {
            currentSize = 'mobile';
        }
        if (size == 'tablet') {
            currentSize = 'tablet';
        }
        if (size == 'laptop') {
            $(window).scroll(function () {
                scroll_fixbar();
            });
            currentSize = 'laptop';
        }
    }
};
$(window).resize(_.debounce(function () {
    media_query(mqCSS);
}, 10));

$(window).load(function () {
    media_query(mqCSS);
});

调整浏览器大小时,当我触发"平板电脑"媒体查询时,scroll_fixbar(); CSS属性仍会影响我的DIV。

我该怎么做才能阻止这种情况发生,这是他们"刷新"div的一种方法,或者在(size == 'tablet')

时关闭功能

帮助赞赏。

尝试以下:

    if (size == 'mobile') {
        currentSize = 'mobile';
        $(window).off('scroll', scroll_fixbar);
    } else if (size == 'tablet') {
        currentSize = 'tablet';
        $(window).off('scroll', scroll_fixbar);
    } else if (size == 'laptop') {
        $(window).on('scroll', scroll_fixbar);
        currentSize = 'laptop';
    }

最新更新