简体   繁体   English

如何制作视差效果

[英]How to make parallax effect

Im trying to make responsive parallax effect on my website.我试图在我的网站上制作响应式视差效果。 I have this structure:我有这个结构:

<section id="first">
    <div class="text fixed" >
        <h1><b>aSs</b> lorem ipsum dolor sit amet</h1>
        <p>Ogólnie znana teza głosi, iż użytkownika moze rozpraszać zrozumiała zawartośc strony, kiedy ten <span class="blue"> chce zobaczyć sam jej wygląd.</span></p>
        <a class="btn btn-orange" href="#">Zobacz naszych <b>pracowników</b> ></a>
    </div>
</section>
<section id="second"></section>

CSS: CSS:

    #first{
    background: url(../images/tlo1.jpg) no-repeat;
    width: 100%;
    background-size: cover;
    background-position: 50%;
    height: 699px;
    text-align: center;
    position: relative;
    white-space: nowrap;
}
.fixed{
   position:fixed;
   top: 300px;
   left: 0;
}
.static{
    position: absolute;
    left:0;
    bottom: 0;
}

.text{
    display: inline-block;
    vertical-align: middle;
    white-space: normal;
    background: #fff;
    padding: 30px 0;
    width: 100%;
    color: #7B7878;
    font-size: 16px;
    font-weight: 300;
}

Js: JS:

pozycjaBlueBox = $("#first").offset().top + $('#first').height();
    $(document).scroll(function(){

        if($(window).width() <=1024){
            pozycjaScrolla = $(window).scrollTop() + $("#first .text").offset().top+$("#first .text").height();
        }else{
            pozycjaScrolla = $(window).scrollTop() + $("#first .text").offset().top-60;
        }

        if($(window).width() > 768){
            if(pozycjaScrolla >= pozycjaBlueBox){
                $("#first .text").removeClass('fixed').addClass('static');
            }else{
                $("#first .text").removeClass('static').addClass('fixed');
            }
        }

    });

I want to have fixed text class until the bottom of this class doesn't comes into contact with the top of #second.我想有固定的文本类,直到这个类的底部不与#second 的顶部接触。 It works well on large desktops but when I resize to lower resolution it's mess.它在大型桌面上运行良好,但是当我调整到较低的分辨率时,它就一团糟。

In its most basic form a parallax function is just a tween bound to the scroll bar.在最基本的形式中,视差函数只是一个绑定到滚动条的补间。 You can extend pretty much any tweening library with the function below,您可以使用以下函数扩展几乎任何补间库,

$(window).scroll(Parallax(1, 10, obj.paralaxUpdate));

// normalizes the animation times : this value can be really any positive number 
var normalizer = 1000; 

Parallax = function(start, stop, tweenUpdateer){
    //scrollTop() returns the distance to top of page
    var topVert = $(this).scrollTop();

    topVert <= start ? var progressNum = 0 : 
    topVert >= stop ? var progressNum = normalizer : 
    var progressNum =  (topVert - start) * (normalizer / (stop - start) || 0);   
    // progressNum is now a value from 0 to 1000 (0 to *normalizer* )
    tweenUpdateer(progressNum);

}

This can now be used with any tweening object, for example Gsap's TweenLite or TweenJS's Tween这现在可以与任何补间对象一起使用,例如 Gsap 的 TweenLite 或 TweenJS 的 Tween

id = document.getElementById("SomeID");
//Gsap syntax
var obj = new TweenLite.to(id, normalizer, {css:{top:"-20px" },paused:true});
obj.paralaxUpdate = obj.progress;
//TweenJS syntax
var obj = Tween.get(id, {paused:true}).to(top:"-20px"}, normalizer);
obj.paralaxUpdate = obj.setPosition;

If you want to make your own tweening objects : its worth taking a look at the source of Gsap or TweenJS.如果您想制作自己的补间对象:值得查看 Gsap 或 TweenJS 的源代码。

TweenJS : http://www.createjs.com/#!/TweenJS/documentation TweenJS : http://www.createjs.com/#!/TweenJS/documentation

GSAP : https://greensock.com/docs/#/HTML5/GSAP/ (My personal favorite) GSAP : https://greensock.com/docs/#/HTML5/GSAP/ (我个人最喜欢的)

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

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