gpt4 book ai didi

javascript - 向下/向上滚动时导航汉堡图标反复闪烁

转载 作者:行者123 更新时间:2023-11-27 23:03:31 30 4
gpt4 key购买 nike

我想要一个汉堡图标动态改变颜色的代码,以适应网站的黑色部分/部分和白色。它最初是 3 段白色,js 代码几乎是好的和正确的,但是当它在白色部分时它一直闪烁,并在红色和白色之间闪烁。

js

var top2 = $('#section2').offset().top; //white background
var top3 = $('#section3').offset().top;
var top4 = $('#section4').offset().top; //white background
var top5 = $('#section5').offset().top;


$(window).scroll(function() {
var scrollPos = $(window).scrollTop();
var $nav = $("#toggle .span");
if (scrollPos >= top2 && scrollPos <= top3) {
$nav.toggleClass('scrolled');
} else if (scrollPos >= top4 && scrollPos <= top5) {
$nav.toggleClass('scrolled');
} else {
$nav.removeClass('scrolled');
}
});

带有用于插入 js 类的媒体查询的 css #toggle .span.scrolled

 #toggle {
position: fixed;
right: 20px;
top: 14px;
z-index: 999;
width: 40px;
height: 40px;
cursor: pointer;
float: right;
/* transition: all 0.3s ease-out; */
visibility: hidden;
opacity: 0;
}

#toggle .span {
height: 3px;
background: #fff;
/* transition: all 0.3s ease-out; */
backface-visibility: hidden;
margin: 5px auto;
}

#toggle.on #one {
transform: rotate(45deg) translateX(2px) translateY(4px);
}

#toggle.on #two {
opacity: 0;
}

#toggle.on #three {
transform: rotate(-45deg) translateX(8px) translateY(-10px);
}

#resize {
z-index: 2;
top: 0px;
position: fixed;
background: #000;
width: 100%;
height: 100%;
visibility: hidden;
opacity: 0;
/* transition: all 1s ease-out; */
display: table;
}

#resize #menu {
height: 90px;
display: table-cell;
vertical-align: center;
padding-left: 0;
}

#resize #menu li {
display: block;
text-align: center;
padding: 20px 0;
font-size: 50px;
min-height: 50px;
font-weight: bold;
transition: all 0.3s ease-out;
}

#resize li:nth-child(1) {
margin-top:110px;
}

#resize #menu li a {
color: #fff;
font-size: 35px;
text-decoration: none;
cursor: pointer;
transition: all 0.4s ease-out;
}

#resize #menu li a:hover {
text-decoration: none;
color: rgb(233, 29, 29);
}



#resize.active {
visibility: visible;
opacity: 0.97;
}


@media(max-width: 900px) {
#toggle {
visibility: visible;
opacity: 1;
margin-top: 15px;
}

#toggle .span.scrolled {
background: #be1b22;
}

nav #brand {
margin-left: 18px;
}

#menu a {
font-family: 'Fjalla One', sans-serif;
font-weight: 200;
font-size: 20px;
letter-spacing: 1px;
}

nav #menu {
display: none;
}

nav {
margin-bottom: -71px;
}
}

html

<nav>
<span id="brand">
<a href="home.html"><img class="logo" src="../logo site/imgs/logo-transparent.png" alt="image"></a>
</span>


<ul id="menu">
<li><a href="home.html">Home</a></li>
<li><a href="Development.html">Development</a></li>
<li><a href="fees.html">Fees</a></li>
<li><a href="contact.html">Contact</a></li>

</ul>

<div id="toggle">
<div class="span" id="one"></div>
<div class="span" id="two"></div>
<div class="span" id="three"></div>
</div>
</nav>

我不知道它为什么闪烁,但我知道它必须对我的 if 语句做些什么!!!

最佳答案

只是更改了这些 $nav.toggleClass('scrolled');到这个 $nav.addClass('scrolled');傻我.....

关于javascript - 向下/向上滚动时导航汉堡图标反复闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58846930/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com