简体   繁体   English

更改滚动时的边框颜色

[英]Change border color on scroll

I want to change bottom border color of navigation bar on scroll down and change on scroll up.我想在向下滚动时更改导航栏的底部边框颜色并在向上滚动时更改。 It should be smooth transition.应该是平稳过渡。 For reference you can see Facebook messenger navigation bar border color ( https://www.messenger.com/ ).作为参考,您可以查看 Facebook 信使导航栏边框颜色( https://www.messenger.com/ )。 I don't want to use any plugins (scroll trigger etc).我不想使用任何插件(滚动触发器等)。 It should be in pure java script and CSS from scratch.它应该是纯 java 脚本和 CSS 从头开始。

MY NAVBAR CSS CODE我的导航栏 CSS 代码

.nav {
position: fixed;
top: 0;
left: 0;
z-index: 9999;
width: 100%;
height: 100px;
border-bottom: 1px solid #EAEAEA;
background-color: white;
}

HTML CODE HTML 代码

<div class="nav">
</div>

Try this, it's pure javascript just add a class using JS, work as add and remove class.试试这个,它是纯 javascript 只需使用 JS 添加一个 class,作为添加和删除 class 工作。

 //use window.scrollY var scrollpos = window.scrollY; var header = document.getElementsByClassName("nav")[0]; function add_class_on_scroll() { header.classList.add("nav-border"); } function remove_class_on_scroll() { header.classList.remove("nav-border"); } window.addEventListener('scroll', function(){ //Here you forgot to update the value scrollpos = window.scrollY; if(scrollpos > 100){ add_class_on_scroll(); } else { remove_class_on_scroll(); } console.log(scrollpos); });
 .nav-border{ border-bottom-color: red; transition: all 300ms linear; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM