滚动时显示粘滞标题,并在向上滚动时显示

时间:2018-07-15 14:27:49

标签: javascript jquery header fixed sticky

嗨,我正在尝试在我的网站上实现此效果,因此标题停留在页面的右上方,当我向上滚动时,它会显示我滚动的确切像素数量(使用jQuery)。 https://osvaldas.info/auto-hide-sticky-header 有没有更简单的方法可以做到这一点? 演示:https://osvaldas.info/examples/auto-hide-sticky-header/ 非常感谢

代码

window.addEventListener( 'scroll', function()
{
    //...
    if( wScrollCurrent <= 0 ) // scrolled to the very top; element sticks to the top
        element.style.top = '0px';

    else if( wScrollDiff > 0 ) // scrolled up; element slides in
        element.style.top = ( elTop > 0 ? 0 : elTop ) + 'px';

    else if( wScrollDiff < 0 ) // scrolled down
    {
        if( wScrollCurrent + wHeight >= dHeight - elHeight )  // scrolled to the very bottom; element slides in
            element.style.top = ( ( elTop = wScrollCurrent + wHeight - dHeight ) < 0 ? elTop : 0 ) + 'px';

        else // scrolled down; element slides out
            element.style.top = ( Math.abs( elTop ) > elHeight ? -elHeight : elTop ) + 'px';
    }
    //...
});
//...

0 个答案:

没有答案