视差滚动原理及实现

作为一种优雅酷炫的页面展示的方式,视差滚动(Parallax Scrolling)正受到越来越多前端设计师的宠爱,优设的2014年网页设计趋势中依然能够看到它的影子,所以我们不得不来好好研究下视差滚动的原理和实现方式。

1.视差滚动

视差滚动(Parallax Scrolling)指网页滚动过程中,多层次的元素进行不同程度的移动,视觉上形成立体运动效果的网页展示技术。国内外的设计师们实现了很多酷炫的效果,大家请移步欣赏,《14个超棒的带有故事趣味性视差滚动网站》、《17 Inspiring Examples of Parallax Scrolling Sites》、《18 Beautiful Examples of Parallax Scrolling in Web Design》。

2.特性

视差滚动效果酷炫,适合于个性展示的场合。
视差滚动徐徐展开,适合于娓娓道来,讲故事的场合。
视差滚动容易迷航,需要具备较强的导航功能。

3.原理

传统的网页的文字、图片、背景都是一起按照相同方向相同速度滚动的,而视差滚动则是在滚动的时候,内容和多层次的背景实现或不同速度,或不同方向的运动。
有的时候也可以加上一些透明度、大小的动画来优化显示。

4.实现

4.1简单实现

利用background-attachment属性实现。
background-attachment: fixed || scroll || local
默认情况下,此属性取值scroll,页面滚动时,内容和背景一起运动,如果取值fixed,背景相对浏览器固定。借用Alloy Team的博文《 视差滚动的爱情故事》的图片和背景,来看看 效果
  1. <div class="article section1">  
  2.      每当我加班凌晨,独自一人走在黑暗的回家路上  
  3. </div>  
  4. <div class="article section2">  
  5.      我会想起那天夕阳下的奔跑  
  6. </div>  
  7. <div class="article section3">  
  8.      那是我逝去的,青春  
  9. </div>  
css非常简单,
  1. /*统一设置背景的background-attchment属性*/  
  2. .article{  
  3.     width960px;  
  4.     margin0 auto;  
  5.     height800px;  
  6.     padding40px;  
  7.     font24px/1.5 'Microsoft YaHei';  
  8.     background-repeatno-repeat;  
  9.     background-attachmentfixed;  
  10.     background-positioncenter center;  
  11.     background-size: cover;  
  12.     line-height:400px;  
  13. }  
  14. /*分别给每个部分设置不同的背景和颜色*/  
  15. .section1{  
  16.        colorwhite;  
  17.        background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section01.jpg);  
  18. }  
  19. .section2{  
  20.        color#FF8500;  
  21.        background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section02.jpg);  
  22. }  
  23. .section3{  
  24.        color#747474;  
  25.        background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section03.jpg);  
  26. }  

4.2 加上动画

上面的效果略显呆板,我们在页面滚动的时候,给文字加点动画, 看效果。我们来侦听一下scroll事件,当页面滚动某个地方时(),我们给元素添加动画。
[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var articleHeight =800;  
  2. var section1 = document.getElementById('section1'),  
  3. section2 = document.getElementById('section2'),  
  4. section3 = document.getElementById('section3');  
  5. window.addEventListener('scroll',scrollHandler);  
  6.   
  7. function scrollHandler(e){  
  8.     var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;  
  9.     if(scrollTop > 0 && scrollTop < articleHeight){  
  10.         section1.classList.add('anim');  
  11.     }else if(scrollTop >= articleHeight && scrollTop < articleHeight*2){  
  12.         section2.classList.add('anim');  
  13.     }else if(scrollTop >= articleHeight*2 && scrollTop < articleHeight*3){  
  14.         section3.classList.add('anim');  
  15.     }  
  16. }  
html和css也要进行一些修改
  1. /*统一设置背景的background-attchment属性*/  
  2. .article{  
  3.     width960px;  
  4.     margin0 auto;  
  5.     height800px;  
  6.     padding40px;  
  7.     background-repeatno-repeat;  
  8.     background-attachmentfixed;  
  9.     background-positioncenter center;  
  10.     background-size: cover;  
  11.         font24px/1.5 'Microsoft YaHei';  
  12.         line-height:400px;   
  13.         text-indent:-25em;  
  14. }  
  15. /*分别给每个部分设置不同的背景和颜色*/  
  16. .section1{  
  17.         colorwhite;  
  18.         background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section01.jpg);  
  19. }  
  20. .section2{   
  21.         color#FF8500;  
  22.         background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section02.jpg);  
  23. }  
  24. .section3{   
  25.         color#747474;  
  26.         background-imageurl( http://www.alloyteam.com/wp-content/uploads/2014/01/section03.jpg);  
  27. }  
  28. .anim{  
  29.        -webkit-transition : all 1s ease-in;  
  30.        -moz-transition : all 1s ease-in;  
  31.        -ms-transition : all 1s ease-in;  
  32.        transition : all 1s ease-in;  
  33.        text-indent:3em;  
  34. }  
  35.       

4.3 背景运动

刚刚两个情况只是背景保持fixed的状态,我们可以给包括背景在内的多层次元素添加运动,从而实现视差滚动。多背景时,需要确保上面的背景是透明的。看看nettuts上的一个 效果,研究研究,看看实现过程。
html文件里面使用了data-speed和data-type向js里传递参数。
  1. <!-- Section #1 -->  
  2. <section id="home" data-speed="10" data-type="background">  
  3.   <article>I am Absolute Positioned</article>  
  4. </section>  
  5. <!-- Section #2 -->  
  6. <section id="about" data-speed="4" data-type="background">  
  7.   <article>Simple Parallax Scroll</article>  
  8. </section>  
CSS文件,
  1. #home {  
  2.     backgroundurl(http://nettuts.s3.amazonaws.com/2138_SimpleParallax/Demo/images/home.jpg) 50% 0 no-repeat fixed;  
  3.     height1000px;  
  4.     margin0 auto;  
  5.     width100%;  
  6.     max-width1920px;  
  7.     positionrelative;  
  8.     box-shadow: 0 0 50px rgba(0000.8);  
  9. }  
  10. #about {  
  11.     backgroundurl(http://nettuts.s3.amazonaws.com/2138_SimpleParallax/Demo/images/about.jpg) 50% 0 no-repeat fixed;  
  12.     height1000px;  
  13.     margin0 auto;  
  14.     width100%;  
  15.     max-width1920px;  
  16.     positionrelative;  
  17.     box-shadow: 0 0 50px rgba(0000.8);  
  18. }  
  19. /* Introduction */  
  20.  #home article {  
  21.     backgroundurl("http://nettuts.s3.amazonaws.com/2138_SimpleParallax/Demo/images/intro.png"no-repeat scroll center top transparent;  
  22.     height458px;  
  23.     positionabsolute;  
  24.     text-indent-9999px;  
  25.     top: 291px;  
  26.     width100%;  
  27. }  
  28. #about article {  
  29.     backgroundurl("http://nettuts.s3.amazonaws.com/2138_SimpleParallax/Demo/images/parallax.png"no-repeat scroll center top transparent;  
  30.     height458px;  
  31.     positionabsolute;  
  32.     text-indent-9999px;  
  33.     top: 291px;  
  34.     width100%;  
  35. }  
javascript,这里用到了jquery
[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $(document).ready(function () {  
  2.     // Cache the Window object  
  3.     $window = $(window);  
  4.    
  5.     $('section[data-type="background"]').each(function () {  
  6.         var $bgobj = $(this); // assigning the object  
  7.    
  8.         $(window).scroll(function () {  
  9.    
  10.             // Scroll the background at var speed  
  11.             // the yPos is a negative value because we're scrolling it UP!                                
  12.             var yPos = -($window.scrollTop() / $bgobj.data('speed'));  
  13.    
  14.             // Put together our final background position  
  15.             var coords = '50% ' + yPos + 'px';  
  16.    
  17.             // Move the background  
  18.             $bgobj.css({  
  19.                 backgroundPosition: coords;  
  20.             });  
  21.         }); // window scroll Ends  
  22.     });  
  23. });  

5.教程、插件、欣赏

请大家参阅一下资源
Alloy Team的《 视差滚动的爱情故事
codrops的《 PARALLAX SLIDER WITH JQUERY


本博客的另一篇文章《 18佳使滚动技术更上层楼的网站
完工!
感谢阅读,希望可以为您带来些许帮助!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值