javascript - 滚动到页面顶部后使导航栏恢复正常

标签 javascript jquery css

所以我正在尝试学习 javascript 和 jQuery。我正在编写一个元素网站,并希望在滚动页面时使导航更小和透明。我写了这个,它工作正常`

$(document).scroll(function(){
  $('.nav').css({"opacity": "0.85", "height": "55px"});
  $('.nav-container').css({"margin-top": "-13px", "font-size": "1.4em"})
});

`

但我希望它在滚动到顶部时恢复正常。似乎没有为此的 jQuery 事件。

最佳答案

我个人建议:

$(document).scroll(function () {
    // select the relevant elements:
    $('#nav, .nav-container')[
        // if the window is at the 'top', we use the 'removeClass' method,
        // otherwise we use 'addClass':
        $(window).scrollTop() == 0 ? 'removeClass' : 'addClass'
    // and pass the 'scrolled' class-name to the method:
    ]('scrolled');
});

使用 CSS:

.nav.scrolled {
    opacity: 0.85;
    height: 55px;
}

.nav-container.scrolled {
    margin-top: -13px;
    font-size: 1.4em;
}

JS Fiddle demo .

这也使用更正的(有效 HTML)。

引用资料:

关于javascript - 滚动到页面顶部后使导航栏恢复正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033879/

相关文章:

javascript - 为什么这个 node.js + mongoose 代码不保存到数据库中?

javascript - 在 jsp 中调用 onclick 方法之前进行表单验证

jquery - select2永久打开下拉列表

javascript - 滑动窗帘按钮 Action

html - 容器 div 不显示边框加上左右 div 没有完整的 bg 颜色覆盖

html - 无法在表单中输入文本或单击搜索按钮

javascript - 使用 Javascript Math 创建圆形图案中的虚线

javascript - 调用 scrollTo 时错位的滚动条

javascript - 如果任一输入字段已填写,如何验证表单?

html - 当我只悬停一个时,为什么会显示所有列表元素的箭头?