简体   繁体   中英

Jquery - Jittery scrolling when using window scroll

I have a menu at the top of a page that is initially set to position:absolute. Then I have a script with window scroll that changes the position to fixed once someone scrolls 113px down the page. It works fine except the menu jitters really bad when using the mousewheel (seems to only be an issue in Chrome and Opera). Strangely enough when using the actual scroll bar, no problem, it's just when using the mousewheel.

It abruptly moves up and down as if it's recalculating its position.

Is there anything I can add to this script or the css that will take away the jitters?

<style>
#topnavcontainer {width:100%; height:50px; position:absolute; top:144px; z-index:200; background:#faa619}
.topnavcontainer-scroll {position:fixed !important; top:30px !important}
</style>

<script type="text/javascript">
$(window).scroll(function() {
    var windscroll = $(window).scrollTop();
    if (windscroll >= 113) {

        $('#topnavcontainer').addClass('topnavcontainer-scroll');

    } else {

        $('#topnavcontainer').removeClass('topnavcontainer-scroll');

    }

}).scroll();
</script>

Found a solution. Applying the following CSS to the fixed elements did the trick:

-webkit-backface-visibility:hidden; -webkit-transform: translateZ(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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