gpt4 book ai didi

JavaScript 代码不工作

转载 作者:行者123 更新时间:2023-12-03 09:48:36 25 4
gpt4 key购买 nike

当我滚动过去#main时,我试图将.nav的颜色更改为.past-main中使用的CSS,但是,由于某种原因,当我在浏览器中打开文件时,javascript 无法工作。没有语法错误或任何迹象表明我的代码有问题。所以我不知道还能做什么。这是我的代码:

HTML:

<head>
<meta charset="utf-8">

<title>Untitled Document</title>
<link rel="stylesheet" href="mooss.css">

</head>

<body>
<nav class="nav">
<a href="#" class="logo">[logo]</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>

<script>
// get the value of the bottom of the #main element by adding the offset of that element plus its height, set it as a variable

$(document).ready(function() {

var mainbottom = $('#main').offset().top + $('#main').height();

// on scroll,
$(window).on('scroll', function() {

// we round here to reduce a little workload
stop = Math.round($(window).scrollTop());

if (stop > mainbottom) {
$('.nav').addClass('past-main');
} else {
$('.nav').removeClass('past-main');
}

});

})
</script>

</body>

这是CSS:

.nav {
background-color: transparent;
color: #fff;
transition: all 0.25s ease;
position: fixed;
top: 0;
width: 100%;
background-color: #ccc;
padding: 1em 0;
/* make sure to add vendor prefixes here */;
}

.nav.past-main {
background-color: #fff;
color: #444;
}

#main {
height: 500px;
background-color: red;
}

#below-main {
height: 1000px;
background-color: #eee;
}

最佳答案

尝试删除 class="nav" 并通过 nav 访问它,而不是 .nav

// get the value of the bottom of the #main element by adding the offset of that element plus its height, set it as a variable

$(document).ready(function() {

var mainbottom = $('#main').offset().top + $('#main').height();

// on scroll,
$(window).on('scroll',function(){

// we round here to reduce a little workload
stop = Math.round($(window).scrollTop());

if (stop > mainbottom) {
$('nav').addClass('past-main');
} else {
$('nav').removeClass('past-main');
}

});

})
 nav {
background-color:transparent;
color:#fff;
transition: all 0.25s ease;
position:fixed;
top:0;
width:100%;
background-color:#ccc;
padding:1em 0;
/* make sure to add vendor prefixes here */
}

nav.past-main {
background-color:#fff;
color:#444;
}

#main {
height:500px;
background-color:red;
}

#below-main {
height:1000px;
background-color:#eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<nav>
<a href="#" class="logo">[logo]</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>

</body>

关于JavaScript 代码不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30944270/

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