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


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 相关文章推荐
日期处理的js库(迷你版)--自建js库总结
Nov 21 Javascript
jQuery旋转插件—rotate支持(ie/Firefox/SafariOpera/Chrome)
Jan 16 Javascript
JS获取html对象的几种方式介绍
Dec 05 Javascript
jQuery对于显示和隐藏等常用状态的判断方法
Dec 13 Javascript
ECMAScript6中Set/WeakSet详解
Jun 12 Javascript
html5 canvas 详细使用教程
Jan 20 Javascript
jquery.form.js异步提交表单详解
Apr 25 jQuery
详解vee-validate的使用个人小结
Jun 07 Javascript
详解AngularJS 模块化
Jun 14 Javascript
用Vue.js方法创建模板并使用多个模板合成
Jun 28 Javascript
Vue学习之组件用法实例详解
Jan 06 Javascript
浅谈Vue的computed计算属性
Mar 21 Vue.js
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根据日期判断星座的函数分享
2014/02/13 PHP
php根据生日计算年龄的方法
2015/07/13 PHP
用ASP将SQL搜索出来的内容导出为TXT的代码
2007/07/27 Javascript
js获取提交的字符串的字节数
2009/02/09 Javascript
jquery高效反选具体实现
2013/05/05 Javascript
多个datatable共存造成多个表格的checkbox都被选中
2013/07/11 Javascript
在javaScript中关于submit和button的区别介绍
2013/10/20 Javascript
jquery获取颜色在ie和ff下的区别示例介绍
2014/03/28 Javascript
浅谈js和css内联外联注意事项
2016/06/30 Javascript
jQuery学习笔记之回调函数
2016/08/15 Javascript
AngularJS的ng-repeat指令与scope继承关系实例详解
2017/01/21 Javascript
Angular动态添加、删除输入框并计算值实例代码
2017/03/29 Javascript
JavaScript 获取元素在父节点中的下标(推荐)
2017/06/28 Javascript
BootStrap下的弹出框加载select2框架失败的解决方法
2017/08/31 Javascript
JavaScript作用域链实例详解
2019/01/21 Javascript
轻松学习JavaScript函数中的 Rest 参数
2019/05/30 Javascript
如何使用RoughViz可视化Vue.js中的草绘图表
2021/01/30 Vue.js
基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能
2021/02/23 Vue.js
使用python加密自己的密码
2015/08/04 Python
简要讲解Python编程中线程的创建与锁的使用
2016/02/28 Python
python中is与双等于号“==”的区别示例详解
2017/11/21 Python
解决pytorch GPU 计算过程中出现内存耗尽的问题
2019/08/19 Python
详解FireFox下Canvas使用图像合成绘制SVG的Bug
2019/07/10 HTML / CSS
95%的面试官都会问到的50道Java线程题,附答案
2012/08/03 面试题
钳工实习自我鉴定
2013/09/19 职场文书
室内设计自我鉴定
2013/10/15 职场文书
高一自我鉴定
2013/12/17 职场文书
小学标准化建设汇报材料
2014/08/16 职场文书
诉讼授权委托书范本
2014/10/05 职场文书
政风行风整改方案
2014/10/25 职场文书
自荐信格式范文
2015/03/04 职场文书
防暑降温通知书
2015/04/27 职场文书
机器人瓦力观后感
2015/06/12 职场文书
中考百日冲刺决心书
2015/09/22 职场文书
Golang原生rpc(rpc服务端源码解读)
2022/04/07 Golang
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python