就是头部往下滚的时候会和在最上面的时候不一样。怎么做的
$(window).scroll(function(){
var scrollTop =$(this).scrollTop(); //获得滚动条距离顶部的位置
if(scrollTop == 0) {
//回到顶部时的操作
}
else {
//往下拉了,就在这里写改变头部的样式
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
.container{
width:100%;
height:60px;
background: #bf0;
position: fixed;
top:0;
}
.gg{
width:100%;
height:100px;
background: #000;
position: fixed;
top:60px;
}
.cc{
height:1280px;
}
</style>
</head>
<body>
<div class="container">包打听</div>
<div class="cc"></div>
<div class="gg">不要打听我</div>
<script>
$(window).scroll(function(){
if($(this).scrollTop() == 0) {
$(".gg").css("height","60px"); //返回顶部时变正常
}
else {
$(".gg").css("height","30px");// 给高度设置小的
}
});
</script>
</body>
</html>
$(window).scroll(function(){
height = $(window).scrollTop();
if(height > 45){//滚动条滚动距离
...//滚动条移动大于45px你需要的样式变化
}else{
...//小于等于45px需要样式变化
};
});
3 回答1.5k 阅读✓ 已解决
6 回答1.4k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
2 回答1k 阅读✓ 已解决
2 回答1.6k 阅读✓ 已解决
2 回答1.5k 阅读✓ 已解决
4 回答1.6k 阅读
监听
scroll
事件,获取到scrollTop
,将其设置成header的top
值,思路大概是这样。