javascript - 如何在向下滚动页面时添加CSS类

标签 javascript jquery

如何在向下滚动且高度 = 400 时添加 css display:unset ;

  <nav class="navbar navbar-expand-lg bg-warning fixed-top navbar-transparent" color-on-scroll="400"  style="display: none;">
</nav>

我用它来获得高度

const height = $(window).height();
const scrollTop = $(window).scrollTop();
console.log(scrollTop);

最佳答案

需要监听滚动事件,然后计算滚动了多少高度。之后,当滚动高度达到 400 时,添加你的 css。示例代码如下,

$(window).scroll(function(){
    if($(this).scrollTop()>= 400){           
        $('.navbar.navbar-expand-lg').css('display','unset');
    }
});

关于javascript - 如何在向下滚动页面时添加CSS类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109971/

相关文章:

javascript - 我如何在 Javascript 和 Django 中执行此操作?

Javascript JSON.parse 与外来字符的问题

javascript - 不变违规 : View config getter callback for component `RNCSafeAreaProvider` must be a function (received `undefined` )

javascript - 标记映射 : Insert the mindmap to the HTML

jquery - 使用查询更改滚动百分比的 CSS

javascript - 单击元素外部时重置输入字段

javascript - 使用 JQuery/JavaScript 调用/单击 mailto 链接

javascript - jquery if 语句无法正常运行

javascript - HTML5 history API popstate 事件返回错误数据

jquery - 将剑道图表转换为 pdf 时,它会在 pdf 图表上绘制线条