固定背景实现的背景滚动特效示例分享


Posted in Javascript onMay 19, 2013

固定背景实现的背景滚动特效示例分享
分享一个来自corpse的固定背景滚动特效,使用background-attachment: fixed和导航菜单,页面会非常平滑的滚动。
HTML

<div id="cbp-fbscroller" class="cbp-fbscroller"> 
<nav> 
<a href="#fbsection1" class="cbp-fbcurrent">Section 1</a> 
<a href="#fbsection2">Section 2</a> 
<a href="#fbsection3">Section 3</a> 
<a href="#fbsection4">Section 4</a> 
<a href="#fbsection5">Section 5</a> 
</nav> 
<section id="fbsection1"></section> 
<section id="fbsection2"></section> 
<section id="fbsection3"></section> 
<section id="fbsection4"></section> 
<section id="fbsection5"></section> 
</div>

CSS
/* Set all parents to full height */ 
html, body, 
.container, 
.cbp-fbscroller, 
.cbp-fbscroller section { 
height: 100%; 
} 
/* The nav is fixed on the right side and we center it by translating it 50% 
(we don't know it's height so we can't use the negative margin trick) */ 
.cbp-fbscroller > nav { 
position: fixed; 
z-index: 9999; 
right: 100px; 
top: 50%; 
-webkit-transform: translateY(-50%); 
-moz-transform: translateY(-50%); 
-ms-transform: translateY(-50%); 
transform: translateY(-50%); 
} 
.cbp-fbscroller > nav a { 
display: block; 
position: relative; 
color: transparent; 
height: 50px; 
} 
.cbp-fbscroller > nav a:after { 
content: ''; 
position: absolute; 
width: 24px; 
height: 24px; 
border-radius: 50%; 
border: 4px solid #fff; 
} 
.cbp-fbscroller > nav a:hover:after { 
background: rgba(255,255,255,0.6); 
} 
.cbp-fbscroller > nav a.cbp-fbcurrent:after { 
background: #fff; 
} 
/* background-attachment does the trick */ 
.cbp-fbscroller section { 
position: relative; 
background-position: top center; 
background-repeat: no-repeat; 
background-size: cover; 
background-attachment: fixed; 
} 
#fbsection1 { 
background-image: url(../images/1.jpg); 
} 
#fbsection2 { 
background-image: url(../images/2.jpg); 
} 
#fbsection3 { 
background-image: url(../images/3.jpg); 
} 
#fbsection4 { 
background-image: url(../images/4.jpg); 
} 
#fbsection5 { 
background-image: url(../images/5.jpg); 
}

Javascript
/** 
* cbpFixedScrollLayout.js v1.0.0 
* http://www.codrops.com 
* 
* Licensed under the MIT license. 
* http://www.opensource.org/licenses/mit-license.php 
* 
* Copyright 2013, Codrops 
* http://www.codrops.com 
*/ 
var cbpFixedScrollLayout = (function() { 
// cache and initialize some values 
var config = { 
// the cbp-fbscroller′s sections 
$sections : $( '#cbp-fbscroller > section' ), 
// the navigation links 
$navlinks : $( '#cbp-fbscroller > nav:first > a' ), 
// index of current link / section 
currentLink : 0, 
// the body element 
$body : $( 'html, body' ), 
// the body animation speed 
animspeed : 650, 
// the body animation easing (jquery easing) 
animeasing : 'easeInOutExpo' 
}; 
function init() { 
// click on a navigation link: the body is scrolled to the position of the respective section 
config.$navlinks.on( 'click', function() { 
scrollAnim( config.$sections.eq( $( this ).index() ).offset().top ); 
return false; 
} ); 
// 2 waypoints defined: 
// First one when we scroll down: the current navigation link gets updated. 
// A `new section′ is reached when it occupies more than 70% of the viewport 
// Second one when we scroll up: the current navigation link gets updated. 
// A `new section′ is reached when it occupies more than 70% of the viewport 
config.$sections.waypoint( function( direction ) { 
if( direction === 'down' ) { changeNav( $( this ) ); } 
}, { offset: '30%' } ).waypoint( function( direction ) { 
if( direction === 'up' ) { changeNav( $( this ) ); } 
}, { offset: '-30%' } ); 
// on window resize: the body is scrolled to the position of the current section 
$( window ).on( 'debouncedresize', function() { 
scrollAnim( config.$sections.eq( config.currentLink ).offset().top ); 
} ); 
} 
// update the current navigation link 
function changeNav( $section ) { 
config.$navlinks.eq( config.currentLink ).removeClass( 'cbp-fbcurrent' ); 
config.currentLink = $section.index( 'section' ); 
config.$navlinks.eq( config.currentLink ).addClass( 'cbp-fbcurrent' ); 
} 
// function to scroll / animate the body 
function scrollAnim( top ) { 
config.$body.stop().animate( { scrollTop : top }, config.animspeed, config.animeasing ); 
} 
return { init : init }; 
})();
Javascript 相关文章推荐
JavaScript 特殊字符
Apr 05 Javascript
javascript 实现父窗口引用弹出窗口的值的脚本
Aug 07 Javascript
Knockoutjs的环境搭建教程
Nov 26 Javascript
JS刷新当前页面的几种方法总结
Dec 24 Javascript
Javascript中arguments对象详解
Oct 22 Javascript
Javascript前端UI框架Kit使用指南之Kitjs简介
Nov 28 Javascript
基于Vuejs框架实现翻页组件
Jun 29 Javascript
整理关于Bootstrap导航的慕课笔记
Mar 29 Javascript
jQuery查找dom的几种方法效率详解
May 17 jQuery
ES6的异步操作之promise用法和async函数的具体使用
Dec 06 Javascript
Vue中使用Echarts仪表盘展示实时数据的实现
Nov 01 Javascript
js实现鼠标拖曳效果
Dec 30 Javascript
Jquery实现鼠标移上弹出提示框、移出消失思路及代码
May 19 #Javascript
扩展js对象数组的OrderByAsc和OrderByDesc方法实现思路
May 17 #Javascript
js获取元素到文档区域document的(横向、纵向)坐标的两种方法
May 17 #Javascript
javascript解决innerText浏览器兼容问题思路代码
May 17 #Javascript
div拖拽插件——JQ.MoveBox.js(自制JQ插件)
May 17 #Javascript
文字溢出实现溢出的部分再放入一个新生成的div中具体代码
May 17 #Javascript
JQuery DataTable删除行后的页面更新利用Ajax解决
May 17 #Javascript
You might like
解决163/sohu/sina不能够收到PHP MAIL函数发出邮件的问题
2009/03/13 PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
2010/02/16 PHP
php的zip解压缩类pclzip使用示例
2014/03/14 PHP
php文件系统处理方法小结
2016/05/23 PHP
jquery中常用的SET和GET
2009/01/13 Javascript
innerHTML与jquery里的html()区别介绍
2012/10/12 Javascript
使用JS读秒使用示例
2013/09/21 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
Bootstrap下拉菜单样式
2017/02/07 Javascript
webpack入门+react环境配置
2017/02/08 Javascript
Vue2.0如何发布项目实战
2017/07/27 Javascript
AngularJS的$location使用方法详解
2017/10/19 Javascript
Vue2.2.0+新特性整理及注意事项
2018/08/22 Javascript
Python中的进程分支fork和exec详解
2015/04/11 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
2016/01/16 Python
Python中list初始化方法示例
2016/09/18 Python
Python简单删除列表中相同元素的方法示例
2017/06/12 Python
flask入门之文件上传与邮件发送示例
2018/07/18 Python
原生python实现knn分类算法
2019/10/24 Python
Python之指数与E记法的区别详解
2019/11/21 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
2019/12/10 Python
Python调用接口合并Excel表代码实例
2020/03/31 Python
英国著名的化妆品折扣网站:Allbeauty.com
2016/07/21 全球购物
Shoes For Crews法国官网:美国领先的防滑鞋设计和制造商
2018/01/01 全球购物
物业管理大学生个人的自我评价
2013/10/10 职场文书
软件工程师岗位职责
2013/11/16 职场文书
业务部主管岗位职责
2014/01/29 职场文书
不拖欠农民工工资承诺书
2014/03/31 职场文书
最常使用的求职信
2014/05/25 职场文书
会计师事务所实习证明
2014/11/16 职场文书
课外活动实习计划
2015/01/19 职场文书
世界气象日活动总结
2015/02/27 职场文书
2015年导购员工作总结
2015/04/25 职场文书
运动会开幕式致辞
2015/07/29 职场文书
mysql中DCL常用的用户和权限控制
2022/03/31 MySQL