jquery - 使标签位置在滚动时固定

标签 jquery html css

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

在 Logo 下方滚动时, 1- 选项卡得到修复。 2- 较小的 Logo 进入选项卡。

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

感谢您的帮助。 问候,

最佳答案

在桌面上这应该不难:

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;
    }
}

在移动设备上,它要难一点。您必须将 header 与两个不同 div 中的其余内容分开,因为正文不会像人们想象的那样响应滚动事件。

使用这种方法时,您需要将滚动事件绑定(bind)到内容 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
}

希望我能帮到你 :)

关于jquery - 使标签位置在滚动时固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454426/

相关文章:

jquery - 如何使用 jQuery 获取 $el 之前的文本?

javascript - 内容不会 float 在页面右侧

javascript - 哪些 Bootstrap css 和 javascript 文件应该链接到我们的 html 页面?

javascript - 在 div 中包装 slider 降低高度

javascript - backbone.js 事件

javascript - 数据表提交表单服务器端数据

css - 将 HTML5 [hidden] 设置为 true 的元素的 CSS 选择器是什么?

html - 居中标题标签问题

javascript - 无法使用 jQuery(Chrome 扩展)选择某些 Facebook 类

javascript - 这些 .less 文件从哪里加载?