简体   繁体   English

使用JQuery动画的视差滚动失败

[英]Parallax Scrolling with JQuery animation stumbles

I have implemented a parallax effect with a video. 我已经用视频实现了视差效果。 I wanted to do it for my own so I did it without any framework or plugin but it is slow and it stumbles around. 我想自己做,所以我没有任何框架或插件就完成了,但是它很慢而且偶然发现。

My idea was that there are 2 pictures, 1 video and 2 boxes in front of them. 我的想法是在它们前面有2张图片,1个视频和2个框。 So my code was that if i am on the position of the 1 picture, the pictures scroll slower (with margin-top) like this: 所以我的代码是,如果我位于1张图片的位置,则图片的滚动速度会变慢(边缘为上),如下所示:

$( window ).scroll(function() { 

var scroll = $(window).scrollTop(); 

   if(scroll>470){

            scroll = scroll-470;

            var scrollSlow = scroll*0.4;

        $('#Picture1').css('margin-top', scrollSlow);

    $('#InfoBox1').css('margin-top', -scroll);

            if(scroll<400){
         $('#Picture2').css('margin-top', -scroll);
    }
            $('#InfoBox2').css('margin-top', -scroll+heightPX);

            if(scroll<900){
         $('#Picture3').css('margin-top', -scroll+heightPX);
        }

      }
   }

But if I scroll down it doesn't work. 但是,如果我向下滚动它是行不通的。

Here is the online version: http://p-goetz.de/Parallax.html 这是在线版本: http : //p-goetz.de/Parallax.html

Problem: You are probably testing your website in chrome/safari, try using Firefox you will notice that the things are smoother. 问题:您可能正在使用chrome / safari测试您的网站,请尝试使用Firefox,您会发现事情更加顺利。

Reason: In some browsers when you scroll they jump 100px at once hence your parallax animation start looking odd. 原因:在某些浏览器中,当您滚动时,它们会一次跳100px,因此您的视差动画开始看起来很奇怪。

Solution: Try to use a custom scroll with smooth fx.I will recommend Nicescroll . 解决方案:尝试使用具有平滑fx的自定义滚动。我会推荐Nicescroll

The issue is the images/videos are so large, the browser lags when scrolling before they are completely loaded. 问题是图像/视频太大,滚动时浏览器在完全加载之前滞后。 One solution would be to wait for the images/videos to finish loading before presenting the page. 一种解决方案是在显示页面之前等待图像/视频完成加载。

$('body').hide();

var video = document.getElementById('PARALLAX_bild2');

video.addEventListener('loadeddata', function() { // video is loaded
   $('img').load(function() { // images are loaded
     // Do some fancy fade in of the page here. This is just an example.
     $('body').show();
   });
}, false);

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

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