Parallax Effect on Text

不想你离开。 提交于 2019-12-25 04:04:12

问题


A perfect example of what I am looking to achieve is on this page as soon as you land on it (parallax BG, text fades, and text is parallaxed):themenectar.com

I am using parallax.js for my backgrounds which works great. I am also using this snippet below which fades my title out on scroll:

function scrollBanner() {
    $(document).scroll(function(){
        var scrollPos = $(this).scrollTop();
        $('.page-title, .breadcrumbs').css({
            'opacity' : 1-(scrollPos/200),
        });
    });
}

scrollBanner();

Everything works fine, except for that I am stumped on how to achieve a parallax effect on my title.

Right now when I scroll my title just scrolls up, as it would any other element. How could I go about making my text parallax too? Such as in the example on themenectar.com


回答1:


I would use your scrollPos variable to manipulate the CSS property transform: translateY(n); something like this:

var transY = scrollPos / 2;
$('.page-title').css('transform':'translateY(' + transY + ')');

Of course play around with the numbers to get the desired effect.

The Transform CSS property is good for performance reasons (https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/)



来源:https://stackoverflow.com/questions/41453849/parallax-effect-on-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!