1
我试图让一个元素更改位置固定滚动某一点后,但是没有任何反应时滚动。我有<script src="stiler/jquery-3.1.1.min.js"></script>
引用jQuery在<head>
。滚动时什么都没有发生。在DOM控制台中没有错误消息,没有任何东西。jquery没有响应滚动
$(document).ready(function(){
var wrap = $('#header-wrap');
$(window).on('scroll', function(e) {
if (this.scrollTop > 147) {
wrap.addClass('fix');
} else {
wrap.removeClass('fix');
}
});
});
body {
background-color: grey;
margin: 0;
min-height: 300vh;
}
#header-wrap {
top: 0;
width: 100%;
}
#redline {
top: 0;
left: 0;
width: 100%;
position: fixed;
height: 10px;
background-color: #b30027;
z-index: 99;
}
#velkommen {
height: 300px;
width: 100%;
background-color: white;
position: relative;
top: 10px;
margin-bottom: -60px;
}
#header {
position: relative;
left: 0;
width: 100%;
background-color: white;
height: 60px;
}
.fix #header {
position: fixed;
top: 10px;
}
h1 {
margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="header-wrap">
<div id="redline"></div>
<div id="velkommen"></div>
<div id="header"> <!--This tag should get class="fix"-->
<h1>#HEADER</h1>
</div> </header>
您的示例中存在一些差异。在你描述的问题中你已经列出了jQuery v3.1.1,但是在你的代码片段中你有2.1.1。在最后一次关闭' div'的HTML中也有一个错字。另外,对于您的编辑,在您的原始代码中,是否已将代码封装在'$(document).ready()' – disinfor
中。代码不是包裹在'$(document).ready()'中,没有。现在,使用与代码段中完全相同的代码。该片段没有列出3.1.1 jquery。 – student42