我发现这个整洁平滑的滚动背景片段,我稍微调整了一下。
我想要完成的是在开始时有这三个滚动背景,然后在最后一个完成之后,页面上的其他div将会显示。在我的例子中,无论你在div的底部或其上方放置什么信息,背景都隐藏了它。
如果有一种更简单的方法来完成这种类型的平滑滚动,那就太好了,但我还没有找到一个像这样的。
https://jsfiddle.net/jzhang172/xwqsxeff/
$(function(){
// ------------- VARIABLES ------------- //
var ticking = false;
var isFirefox = (/Firefox/i.test(navigator.userAgent));
var isIe = (/MSIE/i.test(navigator.userAgent)) || (/Trident.*rv\:11\./i.test(navigator.userAgent));
var scrollSensitivitySetting = 30; //Increase/decrease this number to change sensitivity to trackpad gestures (up = less sensitive; down = more sensitive)
var slideDurationSetting = 600; //Amount of time for which slide is "locked"
var currentSlideNumber = 0;
var totalSlideNumber = $(".background").length;
// ------------- DETERMINE DELTA/SCROLL DIRECTION ------------- //
function parallaxScroll(evt) {
if (isFirefox) {
//Set delta for Firefox
delta = evt.detail * (-120);
} else if (isIe) {
//Set delta for IE
delta = -evt.deltaY;
} else {
//Set delta for all other browsers
delta = evt.wheelDelta;
}
if (ticking != true) {
if (delta <= -scrollSensitivitySetting) {
//Down scroll
ticking = true;
if (currentSlideNumber !== totalSlideNumber - 1) {
currentSlideNumber++;
nextItem();
}
slideDurationTimeout(slideDurationSetting);
}
if (delta >= scrollSensitivitySetting) {
//Up scroll
ticking = true;
if (currentSlideNumber !== 0) {
currentSlideNumber--;
}
previousItem();
slideDurationTimeout(slideDurationSetting);
}
}
}
// ------------- SET TIMEOUT TO TEMPORARILY "LOCK" SLIDES ------------- //
function slideDurationTimeout(slideDuration) {
setTimeout(function() {
ticking = false;
}, slideDuration);
}
// ------------- ADD EVENT LISTENER ------------- //
var mousewheelEvent = isFirefox ? "DOMMouseScroll" : "wheel";
window.addEventListener(mousewheelEvent, _.throttle(parallaxScroll, 60), false);
// ------------- SLIDE MOTION ------------- //
function nextItem() {
var $previousSlide = $(".background").eq(currentSlideNumber - 1);
$previousSlide.removeClass("up-scroll").addClass("down-scroll");
}
function previousItem() {
var $currentSlide = $(".background").eq(currentSlideNumber);
$currentSlide.removeClass("down-scroll").addClass("up-scroll");
}
});
html, body {
overflow: hidden;
}
.background {
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
overflow: hidden;
will-change: transform;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
height: 130vh;
position: fixed;
width: 100%;
-webkit-transform: translateY(30vh);
-ms-transform: translateY(30vh);
transform: translateY(30vh);
-webkit-transition: all 1.2s cubic-bezier(0.22, 0.44, 0, 1);
transition: all 1.2s cubic-bezier(0.22, 0.44, 0, 1);
}
.background:before {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(15, 23, 84, 0.32);
}
.background:first-child {
background-image: url(http://s8.postimg.org/lf2udl5np/4_Aihmii.jpg);
-webkit-transform: translateY(-15vh);
-ms-transform: translateY(-15vh);
transform: translateY(-15vh);
}
.background:first-child .content-wrapper {
-webkit-transform: translateY(15vh);
-ms-transform: translateY(15vh);
transform: translateY(15vh);
}
.background:nth-child(2) {
background-image: url(http://s8.postimg.org/ow4wgk4px/ugqti_Lg.jpg);
}
.background:nth-child(3) {
background-image: url(http://s8.postimg.org/grwsbtiat/x_ZMOBTj.jpg);
}
/* Set stacking context of slides */
.background:nth-child(1) {
z-index: 3;
}
.background:nth-child(2) {
z-index: 2;
}
.background:nth-child(3) {
z-index: 1;
}
.content-wrapper {
height: 100vh;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
text-align: center;
-webkit-flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
color: #fff;
font-family: Montserrat;
font-family: 'Libre Baskerville', serif;
text-transform: uppercase;
-webkit-transform: translateY(40vh);
-ms-transform: translateY(40vh);
transform: translateY(40vh);
will-change: transform;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: all 1.7s cubic-bezier(0.22, 0.44, 0, 1);
transition: all 1.7s cubic-bezier(0.22, 0.44, 0, 1);
}
.content-title {
font-size: 12vh;
line-height: 1.4;
}
.background.up-scroll {
-webkit-transform: translate3d(0, -15vh, 0);
transform: translate3d(0, -15vh, 0);
}
.background.up-scroll .content-wrapper {
-webkit-transform: translateY(15vh);
-ms-transform: translateY(15vh);
transform: translateY(15vh);
}
.background.up-scroll + .background {
-webkit-transform: translate3d(0, 30vh, 0);
transform: translate3d(0, 30vh, 0);
}
.background.up-scroll + .background .content-wrapper {
-webkit-transform: translateY(30vh);
-ms-transform: translateY(30vh);
transform: translateY(30vh);
}
.background.down-scroll {
-webkit-transform: translate3d(0, -130vh, 0);
transform: translate3d(0, -130vh, 0);
}
.background.down-scroll .content-wrapper {
-webkit-transform: translateY(40vh);
-ms-transform: translateY(40vh);
transform: translateY(40vh);
}
.background.down-scroll + .background:not(.down-scroll) {
-webkit-transform: translate3d(0, -15vh, 0);
transform: translate3d(0, -15vh, 0);
}
.background.down-scroll + .background:not(.down-scroll) .content-wrapper {
-webkit-transform: translateY(15vh);
-ms-transform: translateY(15vh);
transform: translateY(15vh);
}
#one{
background:url(http://s8.postimg.org/lf2udl5np/4_Aihmii.jpg);
background-repeat: no-repeat;
background-size: cover;
}
#two{
background:red;
background:url(http://s8.postimg.org/ow4wgk4px/ugqti_Lg.jpg);
background-repeat: no-repeat;
background-size: cover;
}
#three{
background:url(http://s8.postimg.org/lf2udl5np/4_Aihmii.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.content-subtitle{
text-transform:none;
}
#first{
color:black;
}
#second{
color:#0058FF;
}
#third{
color:rgb(236, 230, 216);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<div class="container">
<section class="background" id="one">
<div class="content-wrapper">
<p class="content-title" id="first">Promise</p>
<p class="content-subtitle">Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras ut massa mattis nibh semper pretium.<br />Nullam tristique urna sed tellus ornare congue. Etiam vitae erat at nibh aliquam dapibus. </p>
</div>
</section>
<section class="background" id="two">
<div class="content-wrapper">
<p class="content-title" id="second">Our Goal</p>
<p class="content-subtitle">Blha blah</p>
</div>
</section>
<section class="background" id="three">
<div class="content-wrapper">
<p class="content-title" id="third">Global</p>
<p class="content-subtitle">blah blah</p>
</div>
</section>
</div>
<div>
Show me too and everything after the backgrounds please
</div>
侧注:如果您滚动到页面上的某个位置然后重新加载页面,则滚动停留在当前位置,但初始背景也是如此。知道为什么吗?我假设它与JS有关。
答案 0 :(得分:3)
阻止你的div有两件事。首先是.background
,其位置是固定的,.background:before
是位置绝对的。 (:before
标记基本上将一个元素放在实际的类之前。)
如果您只想在完成滚动背景后显示div,请按照CH的回答并使用您的js进行操作。
但是我假设你的另一个div将继续像往常一样滚动到背景div之下。为了做到这一点,你必须
overflow:hidden
position:fixed
.background
旁注:我已在当地试过你的重装问题了,一切正常
答案 1 :(得分:2)
简单的答案是,3个部分都被操作为position:absolute
并在DOM中进行了z定位,而最后列出的简单div则没有。所以它将永远存在于左上角,0,0和z位置:0。
将你的div设置为能够竞争的东西:
#my-lonely-div{
z-index: 10;
position: absolute;
}