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


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 相关文章推荐
document.getElementById方法在Firefox与IE中的区别
May 18 Javascript
js滚动条回到顶部的代码
Dec 06 Javascript
JavaScript可否多线程? 深入理解JavaScript定时机制
May 23 Javascript
动态的改变IFrame的高度实现IFrame自动伸展适应高度
Dec 28 Javascript
js 走马灯简单实例
Nov 21 Javascript
jQuery中操控hidden、disable等无值属性的方法
Jan 06 Javascript
jquery实现的图片点击滚动效果
Apr 29 Javascript
javascript中alert()与console.log()的区别
Aug 26 Javascript
js案例之鼠标跟随jquery版(实例讲解)
Jul 21 jQuery
vue axios请求频繁时取消上一次请求的方法
Nov 10 Javascript
微信小程序 slot踩坑的解决
Apr 01 Javascript
vue实现购物车功能(商品分类)
Apr 20 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操作MongoDB基础教程(连接、新增、修改、删除、查询)
2014/03/25 PHP
thinkPHP显示不出验证码的原因与解决方法分析
2017/05/20 PHP
用PHP的反射实现委托模式的讲解
2019/03/22 PHP
javascript getElementsByClassName实现代码
2010/10/11 Javascript
DIV随滚动条滚动而滚动的实现代码【推荐】
2016/04/12 Javascript
Chrome浏览器的alert弹窗禁止再次弹出后恢复的方法
2016/12/30 Javascript
Vue 进阶教程之v-model详解
2017/05/06 Javascript
JavaScript之DOM_动力节点Java学院整理
2017/07/03 Javascript
如何使用 vue + d3 画一棵树
2018/12/03 Javascript
vue项目前端错误收集之sentry教程详解
2019/05/27 Javascript
VScode格式化ESlint方法(最全最好用方法)
2019/09/10 Javascript
vue-cli4.0多环境配置变量与模式详解
2020/12/30 Vue.js
python3实现ftp服务功能(客户端)
2017/03/24 Python
Python3 适合初学者学习的银行账户登录系统实例
2017/08/08 Python
python 处理dataframe中的时间字段方法
2018/04/10 Python
Django 实现购物车功能的示例代码
2018/10/08 Python
Python装饰器简单用法实例小结
2018/12/03 Python
在Django model中设置多个字段联合唯一约束的实例
2019/07/17 Python
Python基础之列表常见操作经典实例详解
2020/02/26 Python
Python各种扩展名区别点整理
2020/02/27 Python
Python3.x+pyqtgraph实现数据可视化教程
2020/03/14 Python
Python描述符descriptor使用原理解析
2020/03/21 Python
Java多线程实现四种方式原理详解
2020/06/02 Python
Python调用百度OCR实现图片文字识别的示例代码
2020/07/17 Python
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
Interhome丹麦:在线预订度假屋和公寓
2019/07/18 全球购物
教师评优事迹材料
2014/01/10 职场文书
上学迟到的检讨书
2014/01/11 职场文书
上班离岗检讨书
2014/09/10 职场文书
2014院党委领导班子对照检查材料思想汇报
2014/09/24 职场文书
司法局2014法制宣传日活动总结
2014/11/01 职场文书
2014年全国法制宣传日宣传活动方案
2014/11/02 职场文书
2014年依法行政工作总结
2014/11/19 职场文书
办公室行政主管岗位职责
2015/04/09 职场文书
2017寒假社会实践心得体会范文
2016/01/14 职场文书
python用海龟绘图写贪吃蛇游戏
2021/06/18 Python