특정 div를 스크롤 할 때 색상을 변경하기 위해 페이지 맨 위에 고정되는 메뉴를 만들려고합니다.특정 div를 지난 후 스크롤 할 때 메뉴 색상을 변경하십시오.
이 줄에 문제가 있다고 생각합니다. var mainbottom = $('#main').offset().top + $('#main').height();
크롬에서 오류가 발생했기 때문에 : Uncaught TypeError: Cannot read property 'top' of undefined
뭐가 잘못 되었나요? http://codepen.io/taylorleejones/pen/KJsvz?editors=011을하지만 나를 위해 감사를 작동하지 않습니다
<!doctype html>
<html lang="en-US">
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
.nav {
background-color:transparent;
color:#fff;
transition: all 0.25s ease;
position:fixed;
top:0;
width:100%;
background-color:#ccc;
padding:1em;
}
.nav.past-main {
background-color:#fff;
color:#444;
}
#main {
height:500px;
background-color:red;
}
#below-main {
height:1000px;
background-color:#eee;
}
</style>
</head>
<body>
<script type="text/javascript">
var mainbottom = $('#main').offset().top + $('#main').height();
$(window).on('scroll',function(){
stop = Math.round($(window).scrollTop());
if (stop > mainbottom) {
$('.nav').addClass('past-main');
}
else {
$('.nav').removeClass('past-main');
}
});
</script>
<nav class="nav">
<a href="#">link</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
</body>
</html>
내가 여기에서 코드를 가지고 :
여기 내 코드입니다! :)
코드는 jsfiddle에 잘 작동합니다. 코드에서 jQuery의 경로가 올바른지 확인하십시오? 소스를 확인하십시오. – lesssugar