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


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 function代码
May 23 Javascript
两种方法实现文本框输入内容提示消失
Mar 17 Javascript
js实现最短的XML格式化工具实例
Mar 12 Javascript
jQuery实现分章节锚点“回到顶部”动画特效代码
Oct 23 Javascript
JS中多步骤多分步的StepJump组件实例详解
Apr 01 Javascript
seajs模块之间依赖的加载以及模块的执行
Oct 21 Javascript
Easyui使用Dialog行内按钮布局的实例
Jul 27 Javascript
vue.js实现只弹一次弹框
Jan 29 Javascript
vue init webpack myproject构建项目 ip不能访问的解决方法
Mar 20 Javascript
vue.js实现插入数值与表达式的方法分析
Jul 06 Javascript
vue调用本地摄像头实现拍照功能
Aug 14 Javascript
如何封装Vue Element的table表格组件
Feb 06 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 无限级 SelectTree 类
2009/05/19 PHP
PHP 强制性文件下载功能的函数代码(任意文件格式)
2010/05/26 PHP
最准确的php截取字符串长度函数
2015/10/29 PHP
基于JavaScript 类的使用详解
2013/05/07 Javascript
jQuery基于当前元素进行下一步的遍历
2014/05/20 Javascript
jQuery .tmpl() 用法示例介绍
2014/08/21 Javascript
Javascript表单验证要注意的事项
2014/09/29 Javascript
JavaScript中实现最高效的数组乱序方法
2014/10/11 Javascript
js插件YprogressBar实现漂亮的进度条效果
2015/04/20 Javascript
浅谈Javascript实现继承的方法
2015/07/06 Javascript
js游戏人物上下左右跑步效果代码分享
2015/08/28 Javascript
根据user-agent判断蜘蛛代码黑帽跳转代码(js版与php版本)
2015/09/14 Javascript
JavaScript中字符串与Unicode编码互相转换的实现方法
2015/12/18 Javascript
nodeJS模块简单用法示例
2018/04/21 NodeJs
Bootstrap fileinput 上传新文件移除时触发服务器同步删除的配置
2018/10/08 Javascript
微信小程序实现基于三元运算验证手机号/姓名功能示例
2019/01/19 Javascript
服务端预渲染之Nuxt(使用篇)
2019/04/08 Javascript
javascript 易错知识点实例小结
2020/04/25 Javascript
基于Vue sessionStorage实现保留搜索框搜索内容
2020/06/01 Javascript
在Python中使用模块的教程
2015/04/27 Python
python实现下载文件的三种方法
2017/02/09 Python
Python实现Linux中的du命令
2017/06/12 Python
Python 删除连续出现的指定字符的实例
2018/06/29 Python
Caffe均值文件mean.binaryproto转mean.npy的方法
2018/07/09 Python
python 通过麦克风录音 生成wav文件的方法
2019/01/09 Python
python 自动轨迹绘制的实例代码
2019/07/05 Python
用Python做一个久坐提醒小助手的示例代码
2020/02/10 Python
开发人员所需要知道的HTML5性能分析面面观
2012/07/05 HTML / CSS
Lovedrobe官网:英国领先的大码服装品牌
2019/09/19 全球购物
会展中心部门工作职责
2013/11/27 职场文书
个人收入证明范本
2014/01/12 职场文书
有趣的广告词
2014/03/18 职场文书
法定代表人授权委托书
2014/04/04 职场文书
创业计划书之网吧
2019/10/10 职场文书
Python 正则模块详情
2021/11/02 Python
Win11查看设备管理器
2022/04/19 数码科技