这种滚动动画效果是怎么实现的?

http://johnpolacek.github.io/...有个插件网站

其中有一个滚动的动画效果:位置如下图
https://img1.mukewang.com/5bc6991e00015a0506610335.jpg

1、向下滚动 左右两边的内容向中间移动
2、而向上滚动 两个内容又退回到左右两边

看下了网站的代码,发现它是这么写的

witch (anim.attr('data-animation')) {

                    case 'fly-in-left':

                        anim

                            .parent().css('overflow','hidden');

                        scrolldeck.controller.animate(anim, { delay: windowHeight/2, duration: windowHeight/2, property:'left', start:-1200 });

                        break;

                    case 'fly-in-right':

                        anim

                            .parent().css('overflow','hidden');

                        scrolldeck.controller.animate(anim, { delay: windowHeight/2, duration: windowHeight/2, property:'right', start:-1200 });

                        break;

                        

animate()方法里的内容是另一个$.function类方法里封装了。。没法看懂- -

自己试了下用$(window).scroll()方法 完全没法实现,因为scroll()滚动不分上下方向- -上下滚动都是触发一样的内容

球有哪位大神有空帮忙看看这个网站上的那个动画效果,告诉我原理或者类似的简单实现方式,拜谢!

                        


胡子哥哥
浏览 1330回答 1
1回答

MM们

原理:先给元素设置一个left值让其隐藏,当滚动条到一定位置,改变其left值让其出现。改变幅度与滚动幅度正相关。透明度那个也是同理。不过,其中要判断滚动条向上滚动还是向下滚动。我给你实现了其中一个,另外自己摸索实践一下。代码很粗糙,理解一下,也不要管我在意我的命名,就想快点给你搞出来,所以很粗糙。只考虑实现,没考虑性能。DOM操作你可以自行优化。<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>ohyeah</title>&nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp; #wrapper {height: 100%;overflow: hidden;}&nbsp; &nbsp; &nbsp; &nbsp; #screen {width: 300px;height: 800px;}&nbsp; &nbsp; &nbsp; &nbsp; #mark{position: relative;width: 200px;height: 50px;font-size: 40px;left: -100px;}&nbsp; &nbsp; &nbsp; &nbsp; #cover{position: relative;width: 200px;height: 50px;font-size: 40px;left: 1360px;}&nbsp; &nbsp; &nbsp; &nbsp; #dialog {position: relative;width: 200px;height: 50px;margin: 0 auto;font-size: 20px;line-height: 50px;opacity: 0;}&nbsp; &nbsp; &nbsp; &nbsp; #ohyeah {height: 800px;}&nbsp; &nbsp; </style>&nbsp; &nbsp; <script src="jquery.js" type="text/javascript"></script>&nbsp;</head><div id="wrapper">&nbsp; &nbsp; <div id="screen"></div>&nbsp; &nbsp; <div id="mark">love</div>&nbsp; &nbsp; <div id="dialog">ooo</div>&nbsp; &nbsp; <div id="cover">love</div>&nbsp; &nbsp; <div id="ohyeah"></div></div><script type="text/javascript">&nbsp; &nbsp; var beforeScrollTop = $(window).scrollTop();&nbsp; &nbsp; $(window).scroll(function() {&nbsp; &nbsp; &nbsp; &nbsp; var afterScrollTop = $(window).scrollTop(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updown = afterScrollTop - beforeScrollTop;&nbsp; &nbsp; &nbsp; &nbsp; if( updown === 0 ) return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; beforeScrollTop = afterScrollTop;&nbsp; &nbsp; &nbsp; &nbsp; console.log(updown > 0 ? "down" : "up");&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var isUpDown = updown > 0 ? "down" : "up";&nbsp; //判断往上还是往下&nbsp; &nbsp; &nbsp; &nbsp; var scrollTop = $(window).scrollTop();&nbsp; &nbsp; &nbsp; &nbsp; if(isUpDown == 'down' && scrollTop >= 400) {&nbsp; //数据自取,可依据元素的scrollTop值&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var markLeft = parseInt($('#mark').css('left'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = markLeft + (scrollTop/10);&nbsp; &nbsp;//值的变动与滚动幅度现相关&nbsp; , 自行调节&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(markLeft <= 640) {&nbsp; &nbsp;//这个值是让他滚动到一个边界&nbsp; ,&nbsp; 自行调节&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#mark').css('left', left + 'px');&nbsp; &nbsp;//举例一个元素,其他的自己来咯&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else if(isUpDown == 'up' && scrollTop <= 700) {&nbsp; &nbsp;//往上时做相反&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var markLeft = parseInt($('#mark').css('left'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = markLeft - (scrollTop/10);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(markLeft >= -100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#mark').css('left', left + 'px');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //其他的如opacity left 同理,自己实践一下吧&nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript