![](/img/trans.png)
[英]How to move fixed div 200px up when reaching the end of the page (when scrolling)?
[英]How to change size of image after scrolling 200px down the screen.
當您向下滾動200px時,我想更改div“框”大小的大小。
我想在屏幕達到200px或更多之后使“框”的寬度變為20px,一旦用戶向上滾動超過200px,框也必須返回其原始大小;
我必須在Jquery中這樣做嗎? 如果可以的話,任何人都能向我展示?
我做了一個小提琴來展示我的工作。
謝謝你的幫助
.wrap{
min-height:1000px;
background:grey;
}
.header{
position:fixed;
background:black;
height:100px;
width:100%;
position:relative;
position:fixed;
top:0px;
}
.box{
position:abosolute;
top:2px;
left:20px;
background:red;
z-index:999;
height:100px;
width:100px;
}
用純Javascript:
document.addEventListener("scroll", function() {
scrollHeight = window.pageYOffset;
document.getElementsByClassName("box")[0].style.height = scrollHeight >= 200 ? "20px" : "";
}, false);
使用jQuery,我會使用類似
$( window ).scroll(function() {
if($(window).scrollTop() > 200){
$('.box').css({'height': '50'});
}else{
$('.box').css({'height': '100'});
}
});
HTML
<div class="wrap">
<div class="header">
<div class="box"></div>
</div>
</div>
CSS
.wrap{
min-height:1000px;
background:grey;
}
.header{
position:fixed;
background:black;
height:100px;
width:100%;
position:relative;
position:fixed;
top:0px;
}
.box{
position:abosolute;
top:2px;
left:20px;
background:red;
z-index:999;
height:100px;
width:100px;
}
jsFiddle演示: http : //jsfiddle.net/LLbAu/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.