首頁 > web前端 > js教程 > 固定背景實現的背景滾動特效範例分享_javascript技巧

固定背景實現的背景滾動特效範例分享_javascript技巧

不言
發布: 2018-05-18 17:08:20
原創
1508 人瀏覽過

固定背景實現的背景滾動特效範例分享_javascript技巧
分享一個來自corpse的固定背景滾動特效,使用background-attachment: fixed和導航選單,頁面會非常平滑的滾動。
HTML

複製程式碼 程式碼如下:

程式碼如下:


程式碼如下:

程式碼如下:

/* 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); 
}
登入後複製
程式碼如下:< >

CSS

複製程式碼 程式碼如下:

/** 
* 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 : $( &#39;#cbp-fbscroller > section&#39; ), 
// the navigation links 
$navlinks : $( &#39;#cbp-fbscroller > nav:first > a&#39; ), 
// index of current link / section 
currentLink : 0, 
// the body element 
$body : $( &#39;html, body&#39; ), 
// the body animation speed 
animspeed : 650, 
// the body animation easing (jquery easing) 
animeasing : &#39;easeInOutExpo&#39; 
}; 
function init() { 
// click on a navigation link: the body is scrolled to the position of the respective section 
config.$navlinks.on( &#39;click&#39;, 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 === &#39;down&#39; ) { changeNav( $( this ) ); } 
}, { offset: &#39;30%&#39; } ).waypoint( function( direction ) { 
if( direction === &#39;up&#39; ) { changeNav( $( this ) ); } 
}, { offset: &#39;-30%&#39; } ); 
// on window resize: the body is scrolled to the position of the current section 
$( window ).on( &#39;debouncedresize&#39;, function() { 
scrollAnim( config.$sections.eq( config.currentLink ).offset().top ); 
} ); 
} 
// update the current navigation link 
function changeNav( $section ) { 
config.$navlinks.eq( config.currentLink ).removeClass( &#39;cbp-fbcurrent&#39; ); 
config.currentLink = $section.index( &#39;section&#39; ); 
config.$navlinks.eq( config.currentLink ).addClass( &#39;cbp-fbcurrent&#39; ); 
} 
// function to scroll / animate the body 
function scrollAnim( top ) { 
config.$body.stop().animate( { scrollTop : top }, config.animspeed, config.animeasing ); 
} 
return { init : init }; 
})();
登入後複製
程式碼如下:Java複製程式碼 程式碼如下:
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板