简体   繁体   English

具有垂直视差效果的滑块

[英]Slider with vertical parallax effect

I've created a simple slider for my client to be able to update using Flexslider , and elsewhere on their site they have a vertical scrolling parallax effect.我为我的客户创建了一个简单的滑块,以便能够使用Flexslider进行更新,并且在他们网站的其他地方,它们具有垂直滚动视差效果。 Now they want a slider with vertical scrolling parallax effects...现在他们想要一个带有垂直滚动视差效果的滑块......

Here's an example of the desired effect 这是所需效果的示例

For my parallax effect I used Parallax.js , which was great when I only needed a single image to have the effect, but not much use now I need the slider to have it.对于我的视差效果,我使用了Parallax.js ,当我只需要一个图像来产生效果时这很棒,但现在我需要滑块来拥有它并没有多大用处。

The above example uses Avia Slider , but the parallax effect appears to be done by something else (as Avia doesn't have any such effect that I can see).上面的例子使用Avia Slider ,但视差效果似乎是由其他东西完成的(因为 Avia 没有任何我能看到的这样的效果)。

How can I create a simple parallax effect for my existing Flexslider slider (or any other slider)?如何为现有的 Flexslider 滑块(或任何其他滑块)创建简单的视差效果? It seems like it should be a simple effect that doesn't necessarily need a jQuery plugin.看起来它应该是一个简单的效果,不一定需要 jQuery 插件。

Here's how I solved it:这是我解决它的方法:

I added transform: translate3d(0,0,0) to the slider element, and then modified it as the user scrolls down (eg. transform: translate3d(0,10px,0) ).我将transform: translate3d(0,0,0)到滑块元素,然后在用户向下滚动时对其进行修改(例如transform: translate3d(0,10px,0) )。 A simple, clean solution:一个简单、干净的解决方案:

$(window).scroll(function() {
    if($(window).scrollTop() > 0) {

        var parallaxDistance = ($(window).scrollTop()/2), 
            parallaxCSS = "translate3d(0, "+ parallaxDistance +"px , 0)";

        $('.slides').css('transform', parallaxCSS);

    } else {

        $('.slides').css('transform', 'translate3d(0, 0, 0)');
    }
});

One way would be to just place the element absolute.一种方法是将元素绝对放置。 The effective distance the parallax effect should take place has to be in the overflow: hidden area.视差效果应该发生的有效距离必须在overflow: hidden区域。 Which means, if you want a completely fixed background the width of this picture has to be width: 100vw .这意味着,如果你想要一个完全固定的背景,这张图片的宽度必须是width: 100vw

Then all you have to do is trigger a scroll event and transform your picture using translateX() or by changing the position of the background-image and multiply the value with the actual speed.然后您所要做的就是触发滚动事件并使用translateX()或通过更改background-image的位置并将该值与实际速度相乘来转换您的图片。 Since the scroll event triggers really often and the browser won't rerender every time you could use requestAnimationFrame() .由于滚动事件确实经常触发,并且浏览器不会在每次使用requestAnimationFrame()时重新渲染。

var translate = function() {
    $('#object').css('transform','translateX('+($(window).scrollTop()*.4)+'px)');
}

$(window).on('scroll', function() {
   window.requestAnimationFrame(translate);
});

Here a little prototype, so you have a better idea.这里有一个小原型,让你有一个更好的主意。

 $(document).on('ready', function(){ var $outerWrapper = $('#outerWrapper'); var $innerWrapper = $('#innerWrapper'); var $innerWrapperWidth = $innerWrapper.outerWidth(); var $slides = $('.slide'); var slideWidth = $slides.eq(0).outerWidth(); var effective = slideWidth * 0.5; $outerWrapper.on('scroll', function(e){ for (var i = 0; i < $slides.length; i++) { $element = $slides.eq(i); var s = $element.offset().left; if (s < slideWidth && s > 0) { var percent = s/slideWidth; var translate = effective * percent; if ($element.hasClass('parallax')){ $element.css('background-position', '-' + translate +'px 0'); } } } }); });
 .slide{ width: 298px; height: 148px; background-color: #dadada; border: 1px solid #ccc; float: left; background-image: url('http://www.technocrazed.com/wp-content/uploads/2015/12/Landscape-wallpaper-7.jpg'); background-size: 100%; } .slide.parallax{ background-size: 150%; } #innerWrapper{ width: 1500px; height: 150px; } #outerWrapper{ width: 300px; height: 150px; overflow-x: scroll; overflow-y: hidden; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="outerWrapper"> <div id="innerWrapper"> <div class="slide"></div> <div class="slide parallax"></div> <div class="slide"></div> <div class="slide parallax"></div> <div class="slide"></div> </div> </div>

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

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