有些 landing page 的背景圖會固定在視窗上不動,前景內容捲過去,畫面看起來層次分明。這個效果不需要 JavaScript 幫忙滚,一個 CSS 屬性 background-attachment: fixed 就能做到。本文分享一份來自 Codrops 的完整實作,還附上側邊導航與目前段落的高亮。
<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>
/* 所有父層都設為滿版高度 */
html, body,
.container,
.cbp-fbscroller,
.cbp-fbscroller section {
height: 100%;
}
/* 導航固定在右側,用 translateY 垂直居中
(因為不知道它的高度,所以不能用負 margin 那招) */
.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 就是關鍵 */
.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); }
整份樣式裡真正產生效果的只有 background-attachment: fixed 那一行。其余的固定導航、圓點切換,都是為了讓使用者知道「目前在哪一段」而加的輔助。
/**
* cbpFixedScrollLayout.js v1.0.0
* https://www.codrops.com
* Licensed under the MIT license.
*/
var cbpFixedScrollLayout = (function () {
var config = {
$sections: $('#cbp-fbscroller > section'), // 各段落
$navlinks: $('#cbp-fbscroller > nav:first > a'), // 導航連結
currentLink: 0, // 目前段落的索引
$body: $('html, body'),
animspeed: 650,
animeasing: 'easeInOutExpo'
};
function init() {
// 點導航連結:body 捲動到該段落的起始位置
config.$navlinks.on('click', function () {
scrollAnim(config.$sections.eq($(this).index()).offset().top);
return false;
});
// 兩個 waypoint:
// 向下捲時,段落佔掉超過 30% 視窗就更新導航
// 向上捲時,門檻設為 -30%
config.$sections
.waypoint(function (direction) {
if (direction === 'down') { changeNav($(this)); }
}, { offset: '30%' })
.waypoint(function (direction) {
if (direction === 'up') { changeNav($(this)); }
}, { offset: '-30%' });
// 視窗尺寸改變時,重新對齊目前段落
$(window).on('debouncedresize', function () {
scrollAnim(config.$sections.eq(config.currentLink).offset().top);
});
}
// 更新目前導航樣式
function changeNav($section) {
config.$navlinks.eq(config.currentLink).removeClass('cbp-fbcurrent');
config.currentLink = $section.index('section');
config.$navlinks.eq(config.currentLink).addClass('cbp-fbcurrent');
}
// 捲動 / 動畫 body
function scrollAnim(top) {
config.$body.stop().animate({ scrollTop: top },
config.animspeed, config.animeasing);
}
return { init: init };
})();
上下兩個 waypoint 是為了避免「往回捲時高亮不更新」的問題:只掛一個方向的話,反向捲動時觸發時機會偏掉。offset 設成正負 30% 則決定了「段落要佔多少畫面才算進入視野」的靈敏度。
另外,若不想依賴 waypoint 外掛,用 IntersectionObserver 取代那兩段 waypoint 設定會更輕量,也更符合現在的做法。