gpt4 book ai didi

jquery - removeClass 删除该类,然后将其添加回来

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:13 25 4
gpt4 key购买 nike

我正在使用 jQuery 为我的网页的菜单栏制作动画。当您向下滚动时它会折叠并向左移动,当您滚动回顶部时它应该做相反的事情。但是,当您到达顶部时,它会向后移动一部分,然后迅速回到左侧。这是我的代码:

@charset "utf-8";
/* CSS Document */

body {
margin-top: 0;
margin-left: 0;
margin-right: 0;
background-image: url(html-bg2.png);
background-size: 0.5%;
background-repeat: repeat;
background-color: #111111;
overflow: auto;
}

#headbar_left {
position: fixed;
left: 0;
margin: 0;
//margin-top: 3vh;
width: 51vw;
height: 10vh;
background-color: #161616;
border-bottom: 1vh solid #8704ff;
transition: all 0.5s ease-in-out;
transform-origin: 100% -50%;
}

#headbar_right {
position: fixed;
right: 0;
margin: 0;
//margin-top: 3vh;
width: 51vw;
height: 10vh;
background-color: #161616;
border-bottom: 1vh solid #8704ff;
transition: all 0.5s ease-in-out;
transform-origin: 0% 50%;
}

#circle_logo {
position: fixed;
z-index: 1;
width: 14vh;
height: 14vh;
border-radius: 7vh;
background-color: #161616;
left: calc(50vw - 7vh);
box-sizing: border-box;
border: 1vh solid #8704ff;
text-align: center;
line-height: 14vh;
transition: all 0.5s ease-in-out;
}

#logo {
height: 100%;
display: inline-block;
vertical-align: center;
transition: inherit;
}

.spin {
transform: rotate(360deg);
}

.collapse {
transform: scaleX(0);
}

.move_left {
transition: all 0.3s ease-in-out !important;
transform: translateX(calc(7vh - 50vw));
}

#circle_logo:hover {
transition: all 0.2s ease-in-out !important;
border-radius: 0;
}

#body_content {
position: absolute;
width: calc(100vw - 28vh);
height: 105vh;
margin-right: 14vh;
margin-left: 14vh;
background-color: #161616;
z-index: -1;
box-shadow: 0 3vh 10vh 4vh black;
color: white;
font-family: "Lucida Console";
padding-top: 21vh;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="uh.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>uh</title>
</head>
<body>
<div id="top"></div>
<div id="headbar_left"></div>
<div id="headbar_right"></div>
<a href="uh.html"><div id="circle_logo"><img src="html-logo.png" id="logo"></div></a>
<div id="body_content"><center><font size="9.5em"><b>WHAT IS THIS PAGE?</b></font><p>no one knows</center></div>
</body>
<script>
$(document).scroll(function() {
if($(window).scrollTop() !== 0) {
$("#circle_logo").addClass('spin');
$("#headbar_left, #headbar_right").addClass('collapse');
setTimeout(function() {
$("#circle_logo").addClass('move_left');
$("#logo").attr("src", "up-arrow.png");
$("a").attr("href", "#top");
}, 500);
}
else {
$("#circle_logo").removeClass('move_left');
setTimeout(function() {
$("#circle_logo").removeClass('spin');
$("#headbar_left, #headbar_right").removeClass('collapse');
$("#logo").attr("src", "html-logo.png");
$("a").attr("href", "uh.html");
}, 500);
}
});
</script>
</html>

知道如何解决这个问题吗?谢谢!


编辑:我修复了它。这是新的 javascript:

var a = 1;

function remove() {
if (a === 0) {
$("#circle_logo").removeClass('move_left');
setTimeout(function() {
$("#circle_logo").removeClass('spin');
$("#headbar_left, #headbar_right").removeClass('collapse');
$("#logo").attr("src", "html-logo.png");
$("a").attr("href", "uh.html");
}, 500);
a = 1;
}
}

function add() {
if (a === 1) {
$("#circle_logo").addClass('spin');
$("#headbar_left, #headbar_right").addClass('collapse');
setTimeout(function() {
$("#circle_logo").addClass('move_left');
$("#logo").attr("src", "up-arrow.png");
$("a").attr("href", "#top");
}, 500);
a = 0;
}
}

$(document).scroll(function() {
if($(window).scrollTop() !== 0) {
add();
}
else {
remove();
}
});

最佳答案

您尝试的内容非常封闭。但问题在于您设置的 setTimeOut() 几乎在您滚动很多很多次的每个像素上都被触发。像这样它是固定的,我想你想要什么..:

$(document).scroll(function() {
setTimeout(function() {
if($(window).scrollTop() !== 0) {
$("#circle_logo").addClass('spin');
$("#headbar_left, #headbar_right").addClass('collapse');
$("#circle_logo").addClass('move_left');
$("#logo").attr("src", "up-arrow.png");
$("a").attr("href", "#top");
} else {
$("#circle_logo").removeClass('move_left');
$("#circle_logo").removeClass('spin');
$("#headbar_left, #headbar_right").removeClass('collapse');
$("#logo").attr("src", "html-logo.png");
$("a").attr("href", "uh.html");
}
}, 50);
});
@charset "utf-8";
/* CSS Document */

body {
margin-top: 0;
margin-left: 0;
margin-right: 0;
background-image: url(html-bg2.png);
background-size: 0.5%;
background-repeat: repeat;
background-color: #111111;
overflow: auto;
}

#headbar_left {
position: fixed;
left: 0;
margin: 0;
//margin-top: 3vh;
width: 51vw;
height: 10vh;
background-color: #161616;
border-bottom: 1vh solid #8704ff;
transition: all 0.5s ease-in-out;
transform-origin: 100% -50%;
}

#headbar_right {
position: fixed;
right: 0;
margin: 0;
//margin-top: 3vh;
width: 51vw;
height: 10vh;
background-color: #161616;
border-bottom: 1vh solid #8704ff;
transition: all 0.5s ease-in-out;
transform-origin: 0% 50%;
}

#circle_logo {
position: fixed;
z-index: 1;
width: 14vh;
height: 14vh;
border-radius: 7vh;
background-color: #161616;
left: calc(50vw - 7vh);
box-sizing: border-box;
border: 1vh solid #8704ff;
text-align: center;
line-height: 14vh;
transition: all 0.5s ease-in-out;
}

#logo {
height: 100%;
display: inline-block;
vertical-align: center;
transition: inherit;
}

.spin {
transform: rotate(360deg);
}

.collapse {
transform: scaleX(0);
}

.move_left {
transition: all 0.3s ease-in-out !important;
transform: translateX(calc(7vh - 50vw));
}

#circle_logo:hover {
transition: all 0.2s ease-in-out !important;
border-radius: 0;
}

#body_content {
position: absolute;
width: calc(100vw - 28vh);
height: 105vh;
margin-right: 14vh;
margin-left: 14vh;
background-color: #161616;
z-index: -1;
box-shadow: 0 3vh 10vh 4vh black;
color: white;
font-family: "Lucida Console";
padding-top: 21vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="top"></div>
<div id="headbar_left"></div>
<div id="headbar_right"></div>
<a href="uh.html"><div id="circle_logo"><img src="html-logo.png" id="logo"></div></a>
<div id="body_content"><center><font size="9.5em"><b>WHAT IS THIS PAGE?</b></font><p>no one knows</center></div>

关于jquery - removeClass 删除该类,然后将其添加回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43821500/

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