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


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 出生日期和身份证判断大全
Nov 13 Javascript
JavaScript学习点滴 call、apply的区别
Oct 22 Javascript
js 第二代身份证号码的验证机制代码
May 12 Javascript
JS如何将数字类型转化为没3个一个逗号的金钱格式
Jan 27 Javascript
jQuery+CSS3+Html5实现弹出层效果实例代码(附源码下载)
May 16 Javascript
运用js教你轻松制作html音乐播放器
Apr 17 Javascript
基于Three.js插件制作360度全景图
Nov 29 Javascript
JS插件plupload.js实现多图上传并显示进度条
Nov 29 Javascript
jQuery中animate的几种用法与注意事项
Dec 12 Javascript
vuex实现的简单购物车功能示例
Feb 13 Javascript
移动端自适应flexible.js的使用方法(不用三大框架,仅写一个单html页面使用)推荐
Apr 02 Javascript
js实现旋转的星空效果
Nov 01 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
15种PHP Encoder的比较
2007/04/17 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
php页面跳转session cookie丢失导致不能登录等问题的解决方法
2016/12/12 PHP
使用php实现网站验证码功能【推荐】
2017/02/09 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
laravel使用Faker数据填充的实现方法
2019/04/12 PHP
JavaScript 对象、函数和继承
2009/07/07 Javascript
Js event事件在IE、FF兼容性问题
2011/01/01 Javascript
用JS提交参数创建form表单在FireFox中遇到的问题
2013/01/16 Javascript
JavaScript中使用Substring删除字符串最后一个字符
2013/11/03 Javascript
js实现瀑布流的一种简单方法实例分享
2013/11/04 Javascript
JS中关于事件处理函数名后面是否带括号的问题
2016/11/16 Javascript
jquery动态赋值id与动态取id方法示例
2017/08/21 jQuery
vue-router动态设置页面title的实例讲解
2018/08/30 Javascript
layui table数据修改的回显方法
2019/09/04 Javascript
解决vue做详情页跳转的时候使用created方法 数据不会更新问题
2020/07/24 Javascript
python更新列表的方法
2015/07/28 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
2017/05/18 Python
简单实现python进度条脚本
2017/12/18 Python
Python wxPython库消息对话框MessageDialog用法示例
2018/09/03 Python
Django获取该数据的上一条和下一条方法
2019/08/12 Python
html5需遵循的6个设计原则
2016/04/27 HTML / CSS
详解如何获取localStorage最大存储大小的方法
2020/05/21 HTML / CSS
世界上最全面的草药补充剂和顶级品牌维生素网站:HerbsPro
2019/01/20 全球购物
法国面料和小百货在线商店:Mondial Tissus
2019/03/23 全球购物
Looking4Parking美国:全球排名第一的机场停车比较品牌
2019/08/26 全球购物
自荐信如何“自荐”
2013/10/24 职场文书
应届专科生个人的自我评价
2014/01/05 职场文书
2014年学习厉行节约反对浪费思想汇报
2014/09/10 职场文书
自查自纠工作情况报告
2014/10/29 职场文书
2015年党风廉政承诺书
2015/01/22 职场文书
党支部审查意见
2015/06/02 职场文书
2016领导干部廉洁自律心得体会
2016/01/13 职场文书
七年级作文之英语老师
2019/10/28 职场文书
MySQL 服务和数据库管理
2021/11/11 MySQL
vue3种table表格选项个数的控制方法
2022/04/14 Vue.js