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


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 相关文章推荐
脚本安需导入(装载)的三种模式的对比
Jun 24 Javascript
jQuery ui1.7 dialog只能弹出一次问题
Aug 27 Javascript
JavaScript去掉空格的方法集合
Dec 28 Javascript
『jQuery』取指定url格式及分割函数应用
Apr 22 Javascript
JavaScript中的函数模式详解
Feb 11 Javascript
css如何让浮动元素水平居中
Aug 07 Javascript
基于javascript html5实现3D翻书特效
Mar 14 Javascript
Ajax+FormData+javascript实现无刷新表单信息提交
Oct 24 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
Jun 13 Javascript
webpack开发环境和生产环境的深入理解
Nov 08 Javascript
命令行批量截图Node脚本示例代码
Jan 25 Javascript
微信小程序中显示倒计时代码实例
May 09 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
php 替换文章中的图片路径,下载图片到本地服务器的方法
2018/02/06 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
2019/05/08 PHP
让innerHTML的脚本也可以运行起来
2006/07/01 Javascript
js获取变量
2006/08/24 Javascript
JAVASCRIPT车架号识别/验证函数代码 汽车车架号验证程序
2012/01/08 Javascript
js中cookie的添加、取值、删除示例代码
2013/10/21 Javascript
jQuery中[attribute*=value]选择器用法实例
2014/12/31 Javascript
详解JavaScript ES6中的Generator
2015/07/28 Javascript
使用ajaxfileupload.js实现上传文件功能
2016/08/13 Javascript
JavaScript中Array的实用操作技巧分享
2016/09/11 Javascript
基于JavaScript实现选项卡效果
2017/07/21 Javascript
ES6解构赋值实例详解
2017/10/31 Javascript
AngularJS使用Filter自定义过滤器控制ng-repeat去除重复功能示例
2018/04/21 Javascript
微信小程序template模版的使用方法
2019/04/13 Javascript
微信小程序rich-text富文本用法实例分析
2019/05/20 Javascript
详解Node.js异步处理的各种写法
2019/06/09 Javascript
[43:51]2014 DOTA2国际邀请赛中国区预选赛 Dream Times VS TongFu
2014/05/22 DOTA
python采用getopt解析命令行输入参数实例
2014/09/30 Python
Python中__name__的使用实例
2015/04/14 Python
Python找出最小的K个数实例代码
2018/01/04 Python
Python2.7环境Flask框架安装简明教程【已测试】
2018/07/13 Python
Python模拟简单电梯调度算法示例
2018/08/20 Python
Python函数的参数常见分类与用法实例详解
2019/03/30 Python
python 进程的几种创建方式详解
2019/08/29 Python
wxPython色环电阻计算器
2019/11/18 Python
pytorch如何冻结某层参数的实现
2020/01/10 Python
python实现Pyecharts实现动态地图(Map、Geo)
2020/03/25 Python
解决Pytorch自定义层出现多Variable共享内存错误问题
2020/06/28 Python
CSS3教程(10):CSS3 HSL声明设置颜色
2009/04/02 HTML / CSS
CSS3中的@keyframes关键帧动画的选择器绑定
2016/06/13 HTML / CSS
优秀学生干部事迹材料
2014/12/24 职场文书
幼儿园小班个人工作总结
2015/02/12 职场文书
社区服务活动感想
2015/08/11 职场文书
2016年六一儿童节开幕词
2016/03/04 职场文书
python数据分析之用sklearn预测糖尿病
2021/04/22 Python
oracle删除超过N天数据脚本的方法
2022/02/28 Oracle