简体   繁体   English

在屏幕上向下滚动200px后如何更改图像尺寸。

[英]How to change size of image after scrolling 200px down the screen.

I want to change the size of my div "box" size when you scroll over 200px down the screen. 当您向下滚动200px时,我想更改div“框”大小的大小。

I want to make the "box" become 20px in width after the screen has reached 200px or more, The box will also have to return back to its original size once the user has scrolled back up past 200px; 我想在屏幕达到200px或更多之后使“框”的宽度变为20px,一旦用户向上滚动超过200px,框也必须返回其原始大小;

Do I have to do this in Jquery? 我必须在Jquery中这样做吗? If so can any body show me? 如果可以的话,任何人都能向我展示?

I have made a js fiddle to show my working. 我做了一个小提琴来展示我的工作。

thanks for your help 谢谢你的帮助

http://jsfiddle.net/UHAWV/ http://jsfiddle.net/UHAWV/

.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;
}

In pure Javascript: 用纯Javascript:

http://jsfiddle.net/UHAWV/1/ http://jsfiddle.net/UHAWV/1/

document.addEventListener("scroll", function() {
    scrollHeight = window.pageYOffset;
    document.getElementsByClassName("box")[0].style.height = scrollHeight >= 200 ? "20px" : "";
}, false);

Using jQuery, I would use something like 使用jQuery,我会使用类似

$( window ).scroll(function() {
     if($(window).scrollTop() > 200){
       $('.box').css({'height': '50'}); 
     }else{
         $('.box').css({'height': '100'}); 
     }

});

HTML HTML

<div class="wrap">
    <div class="header">
        <div class="box"></div>
    </div>
</div>

CSS 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 demo: http://jsfiddle.net/LLbAu/ jsFiddle演示: http : //jsfiddle.net/LLbAu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM