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


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 相关文章推荐
学习YUI.Ext第七日-View&amp;JSONView Part Two-一个画室网站的案例
Mar 10 Javascript
javascript继承之为什么要继承
Nov 10 Javascript
js实现连个数字相加而不是拼接的方法
Feb 23 Javascript
JavaScript设计模式之工厂方法模式介绍
Dec 28 Javascript
jquery实现的Accordion折叠面板效果代码
Sep 02 Javascript
详解JavaScript时间格式化
Dec 23 Javascript
详解jQuery lazyload 懒加载
Dec 19 Javascript
jQuery基于ajax操作json数据简单示例
Jan 05 Javascript
深入理解Vue.js源码之事件机制
Sep 27 Javascript
仿京东快报向上滚动的实例
Dec 13 Javascript
JS实现二维数组横纵列转置的方法
Apr 17 Javascript
微信小程序实现的一键连接wifi功能示例
Apr 24 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中养成7个面向对象的好习惯
2010/07/17 PHP
PHP中去除换行解决办法小结(PHP_EOL)
2011/11/27 PHP
PHP实现基于文本的摩斯电码生成器
2016/01/11 PHP
实例详解PHP中html word 互转的方法
2016/01/28 PHP
php array_slice 取出数组中的一段序列实例
2016/11/04 PHP
中文路径导致unitpngfix.js不正常的解决方法
2013/06/26 Javascript
js设置cookie过期当前时间减去一秒相当于立即过期
2014/09/04 Javascript
利用JS实现页面删除并重新排序功能
2016/12/09 Javascript
Javascript 高性能之递归,迭代,查表法详解及实例
2017/01/08 Javascript
Bootstrap弹出框(Popover)被挤压的问题小结
2017/07/11 Javascript
Vue.js实现按钮的动态绑定效果及实现代码
2017/08/21 Javascript
原生JavaScript创建不可变对象的方法简单示例
2020/05/07 Javascript
Vue优化:常见会导致内存泄漏问题及优化详解
2020/08/04 Javascript
[02:51]2018年度DOTA2最佳中单位选手-完美盛典
2018/12/17 DOTA
python抓取豆瓣图片并自动保存示例学习
2014/01/10 Python
python简单实现计算过期时间的方法
2015/06/09 Python
Python使用Scrapy爬虫框架全站爬取图片并保存本地的实现代码
2018/03/04 Python
详解Python中的四种队列
2018/05/21 Python
如何优雅地改进Django中的模板碎片缓存详解
2018/07/04 Python
python添加模块搜索路径和包的导入方法
2019/01/19 Python
django 邮件发送模块smtp使用详解
2019/07/22 Python
洲际酒店集团美国官网:IHG美国
2017/11/16 全球购物
新西兰最大、占有率最高的综合性药房:PharmacyDirect药房中文网
2020/11/03 全球购物
中西医结合临床医学专业大学生自荐信
2013/09/28 职场文书
师范生实习的个人自我鉴定
2013/10/20 职场文书
教师自我鉴定范文
2013/11/10 职场文书
《一件运动衫》教学反思
2014/02/19 职场文书
外语系毕业生求职自荐信
2014/04/12 职场文书
先进员工获奖感言
2014/08/14 职场文书
某集团股份有限公司委托书样本
2014/09/24 职场文书
董事长助理岗位职责
2015/02/11 职场文书
婚宴新郎致辞
2015/07/28 职场文书
领导激励员工的演讲稿,各种会上用得到,建议收藏
2019/08/13 职场文书
英文诗歌翻译方法(赏析)
2019/08/16 职场文书
六年级作文之预言作文
2019/10/25 职场文书
idea 在springboot中使用lombok插件的方法
2021/08/02 Java/Android