以正确方式到达窗口边缘的事件(滚动)
以防万一,这是我的第一个问题。
我有: 具有透明背景的导航菜单,并在到达窗口顶部边缘时尝试更改背景。
window.addEventListener("scroll", navSticky);
function navSticky(){
let nav = document.getElementById('navbar');
let bounding = nav.getBoundingClientRect();
if(bounding.top <= 0 ){
nav.classList.add("sticky");
}else{
nav.classList.remove("sticky");
}
}
*{
margin: 0;
text-align: center;
}
body{
background: lightgrey;
}
header h1{
padding: 40px;
}
nav{
position: sticky;
top: 0;
padding: 12px;
background: linear-gradient(to right,
rgba(0,0,0,0),
rgba(0,255,0, 0.5),
rgba(255,0,0, 0.5),
rgba(0,0,0,0));
color: black;
}
nav:before{
content: "now bg transparent";
}
.container{
min-height: 1000px;
padding: 20px;
}
nav.sticky{
background: linear-gradient(to right,
rgb(0,255,0),
rgb(255,0,0));
color: white;
}
nav.sticky:before{
content: "now bg isn't transparent";
}
<body>
<header>
<h1>Header, that ask u to scroll page</h1>
</header>
<nav id="navbar">
</nav>
<div class ="container">
When we scroll page, and nav reached top of screen, .sticky add to classList<br>
</div>
</body>
它的工作原理,但我有几个问题:
- 没有 js 是否可以做同样的事情?
- 是否可以优化此脚本,因为滚动事件调用如此频繁。
谢谢!
Just in case, its my 1th question.
I have:
nav menu with transparent background and trying to change backrgound when reached top edge of window.
window.addEventListener("scroll", navSticky);
function navSticky(){
let nav = document.getElementById('navbar');
let bounding = nav.getBoundingClientRect();
if(bounding.top <= 0 ){
nav.classList.add("sticky");
}else{
nav.classList.remove("sticky");
}
}
*{
margin: 0;
text-align: center;
}
body{
background: lightgrey;
}
header h1{
padding: 40px;
}
nav{
position: sticky;
top: 0;
padding: 12px;
background: linear-gradient(to right,
rgba(0,0,0,0),
rgba(0,255,0, 0.5),
rgba(255,0,0, 0.5),
rgba(0,0,0,0));
color: black;
}
nav:before{
content: "now bg transparent";
}
.container{
min-height: 1000px;
padding: 20px;
}
nav.sticky{
background: linear-gradient(to right,
rgb(0,255,0),
rgb(255,0,0));
color: white;
}
nav.sticky:before{
content: "now bg isn't transparent";
}
<body>
<header>
<h1>Header, that ask u to scroll page</h1>
</header>
<nav id="navbar">
</nav>
<div class ="container">
When we scroll page, and nav reached top of screen, .sticky add to classList<br>
</div>
</body>
Its work, but I have several questions:
- is it possible to do same without js?
- is it possible to optimise this script cuz scroll event calling so often..
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 API 允许您在元素进入或离开视口时观察元素。它在主线程之外执行此操作,因此您不会获得任何渲染阻塞代码。
在您的具体情况下,请观察
元素。每当元素离开视图时,导航栏就会变得粘性。在回调中检查
isIntersecting
属性是true
还是false
。此属性指示观察到的元素是否(部分)在视图中。The API allows you to observe elements when they enter or leave the viewport. It does this away from the main thread, so you won't get any render blocking code.
In your specific case, observe the
<header>
element. Whenever the element leaves the view is the same point when thenavbar
becomes sticky. In the callback check if theisIntersecting
property istrue
orfalse
. This property indicates if the observed element is (partially) in view or not.