滚动时固定标签位置

时间:2013-10-18 16:22:03

标签: jquery html css

我喜欢本网站标签的想法: http://www.usatoday.com/

滚动徽标时, 1-标签得到修复。 2-标签中有一个较小的徽标。

或者,您可以说,类似于Google plus标签。 我怎样才能做到这一点.. *我是数据库开发人员,只有Web技术背景。

感谢您的帮助。 的问候,

1 个答案:

答案 0 :(得分:0)

在桌面上,这应该不是很难:

var navigationIsFixed = false;
var $navigation = $("#navigation");
var headerHeight = $("#my-header").height() //To find the height of the header element
$("body").on('scroll', function(e)) { //Fires continuously while the user scrolls
    if ($("body").scrollTop >= headerHeight && !navigationIsFixed) {
        $navigation.css({
            position: "fixed",
            left:  0,
            right: 0,
            top:   0
        });
        $("#logo").stop(); //Make sure to stop any possible ongoing animations
        $("#logo").doCoolAnimation();
        navigationIsFixed = true;
    } else if (navigationIsFixed) {
        $navigation.css( {position: "static"} );
        $("#logo").stop();
        $("#logo").doCoolRevertAnimation();
        navigationIsFixed = false;
    }
}

在移动设备上,这是一个很小的字节。您必须将标题与两个不同div中的其余内容分开,因为正文可能无法响应滚动事件。

使用此方法时,您需要将滚动事件绑定到内容div

<div id="header">
    //Your header code, including the navigation
</div>
<div id="main-content-wrapper">
    //Everything else
</div>

的CSS:

#main-content-wrapper {
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch; //Enables inertia scroll on webkit mobile browsers
}

我希望我帮助过。)

相关问题