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


分享一个来自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 };
})();

时间: 2024-08-30 22:32:17

固定背景实现的背景滚动特效示例分享_javascript技巧的相关文章

JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码_javascript技巧

JavaScript实现仿新浪微博大厅和未登录腾讯微博首页滚动效果,貌似这些天有不少朋友需要这功能,前几天已经发了几个了,不过那一个效果是用jquery实现的<jQuery向上循环滚动(仿新浪微博未登录首页滚动微博显示)>,今天发现这款是js制作的好,不敢独享,希望需要的朋友喜欢哦. 效果图展示如下: 查看演示         源码下载 html代码 <div class="wp"> <ul id="slider" class=&quo

js图片轮播特效代码分享_javascript技巧

本文实例讲述了js图片轮播特效,分享给大家供大家参考.具体如下: 这是一款基于javascript实现的图片轮播特效代码,有缩略图和标题,可以自定义标题. 运行效果图:                    -------------------查看效果------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的js图片轮播特效代码如下 <head> <meta http-equiv="Content-Type" co

js带点自动图片轮播幻灯片特效代码分享_javascript技巧

本文实例讲述了javascript带点自动图片轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于javascript实现带点自动图片轮播幻灯片特效代码,实现过程很简单. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. (1)在head区域引入CSS样式: <link rel="stylesheet" href="css/zzsc.css&qu

js实现3D图片逐张轮播幻灯片特效代码分享_javascript技巧

本文实例讲述了javascript实现3D图片逐张轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于javascript实现3D图片逐张轮播幻灯片特效代码,实现过程很简单. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的js实现3D图片逐张轮播幻灯片特效代码如下 <head> <meta http-equiv="Content-Type

js焦点文字滚动效果代码分享_javascript技巧

本文实例讲述了js焦点文字滚动效果.分享给大家供大家参考.具体如下:效果描述:今天推荐的这个又是一个原生的js焦点图效果 默认自动切换,也可以手动切换 javascript代码采用函数式对象编程,也就是javascript编程中的Module模式 基本用法很简单,主要特点有三: 1.模块化,可重用 2.封装了变量和function,和全局的命名空间不接触,不污染全局变量 3.只暴露可用public方法,其他私有方法全部隐藏,确保js相互之间不会冲突 运行效果图:-----------------

js精美的幻灯片画集特效代码分享_javascript技巧

本文实例讲述了js制作精美的幻灯片画集特效.分享给大家供大家参考.具体如下: 这是一款基于javascript制作的精美幻灯片画集特效的插件,跟其他幻灯片有别的图片画廊.为什么说有所区别呢,因为这款插件的图片切换时包含4个方向的,即上下左右切换也是可以的,大家可以在实例中进行使用. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 本实例的关键代码: var o = { init:

js图片卷帘门导航菜单特效代码分享_javascript技巧

本文实例讲述了jQuery超精致图片轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于js实现图片卷帘门导航菜单特效代码,与以往的导航菜单相比,更具有创新性,不止是简单的向用户展示信息,而是更加丰富网站的整体内容. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 在head区域引入CSS样式: <link href="css/style.css" re

可自定义速度的js图片无缝滚动示例分享_javascript技巧

思路: 一组图片 控制它的滚动条进行滚动 且此时对这组图片进行复制并添加进原图片组中,现在就有两组图片了.你可以想象一下,现在滚动条继续滚动,原来那组图片最后一张图片已经滚至顶端且消失,复制的那组图片的第一张跟在原图最后一张图片后出现,此时你就能感觉到无缝滚动了. 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/D

jquery 实现上下滚动效果示例代码_javascript技巧

复制代码 代码如下: <script type="text/javascript" src="script/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { var page = 1; var i = 1; var conheight = $('.con li').height(); var lilen =