如何根据我的滚动位置将顶部动画滚动到下一个可见的 div/类?



如果标题有点模糊,请原谅我,因为我不知道如何写下来。

基本上:

我们有一个包含 6 个"场景"的页面。它们都有类.scene和不同的 ID,例如:scene1scene2等......

起初,我们必须动态更改每个场景的颜色——>场景 1 是浅色,最后一个场景是黑色。到目前为止,即使在添加或删除场景时也是如此。

页面顶部和底部都有一个<li>。 它有一个可点击的<a>标签。

当我按nextButton时,我需要根据我在页面上的位置转到下一个可见场景。因此,如果我滚动到场景 2,然后按下按钮,我需要转到场景 3。如果我滚动到场景 5 并按下按钮,我需要转到场景 6,依此类推......另一个按钮previousButton计数相同,但相反(向上(。

此外,如果我以nextButton(场景 6(到达页面末尾,它将返回到场景 1。全部使用scrollTop完成

我一直在谷歌上搜索这个,但我似乎不知道到底该做什么或如何开始。

在下面编写的整段代码$(window).scroll(function)'直到导师向我们提供了each()函数。

var brightness = 90 ;
var $totalScenes = $('.scenes').length;
$('.scenes').each(function() {
$(this).css({"background-color" : "hsl(99, 100%, "+  brightness + "%"   + ")" });
//je hebt 6 scenes en je wilt weten hoe groot de stappen zijn. vanaf de min bereken je hoe groot de stap is. dit doe je 1x (100/totalScenes - 1), dus van 100 naar nul in 5 stappen is 20. Dit herhaalt zich telkens.
brightness = brightness - 100/($totalScenes - 1);
console.log(brightness);
});
$(window).scroll(function() {
// is in viewport function
$.fn.isInViewport = function () {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$('.scenes').each(function(index) {
if ($(this).isInViewport()) {
console.log("dit is " + this.id);
if(index >= $totalScenes / 2) {
$('li').css({"color": "#fff"});
}
else {
$('li').css({"color": "#000"});
}
}
});
});
$('#nextButton').click(function()  {
$("html, body").animate({
scrollTop: $(this).closest('.scenes').next().offset().top });     
//          scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});
CSS
html {
scroll-behavior: smooth;
font-family: 'Roboto', sans-serif;
}
.scenes {
height: 100vh;
max-width: 100vw; 
}
h2 {
text-align: center;
font-size: 10vh;
line-height: 100vh;
}
.circle{     
background: rgba(255, 255, 255, 0.6);
border-radius: 50%;
font-weight: bold;
line-height: 150px;
width: 150px;
display: inline-block;
}
header {
position: fixed;
width: 100vw;
height: 100vh;
}
li {
font-weight: bold;
font-size: 1.4em; 
position: absolute;
}
li a{
cursor: pointer;
text-align: center;
}
#previous {
top: 20px;
left: 50vw;
transform: translate(-50%, 0);
}
#next {
bottom: 20px;
left: 50vw;
transform: translate(-50%, 0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<?php include 'template-parts/head.php'; ?>
</head>
<body>
<header>
<ul>
<li id="previous"><a id="previousButton" href="#">prev &#8593;</a></li>
<li id="next"><a id="nextButton" href="#">next &#8595;</a></li>
</ul>
</header>  
<main>
<div id="wrapper">    
<div id="scene1" class="scenes">
<h2><span class="circle">1</span></h2>
</div>
<div id="scene2" class="scenes">
<h2><span class="circle">2</span></h2>
</div>
<div id="scene3" class="scenes">
<h2><span class="circle">3</span></h2>
</div>
<div id="scene4" class="scenes">
<h2><span class="circle">4</span></h2>
</div>
<div id="scene5" class="scenes">
<h2><span class="circle">5</span></h2>
</div>
<div id="scene6" class="scenes">
<h2><span class="circle">6</span></h2>
</div>    
</div>
</main>

检查这个

var brightness = 90;
var $totalScenes = $(".scenes").length;
var nextElment = "scene1";
$(".scenes").each(function() {
$(this).css({
"background-color": "hsl(99, 100%, " + brightness + "%" + ")"
});
//je hebt 6 scenes en je wilt weten hoe groot de stappen zijn. vanaf de min bereken je hoe groot de stap is. dit doe je 1x (100/totalScenes - 1), dus van 100 naar nul in 5 stappen is 20. Dit herhaalt zich telkens.
brightness = brightness - 100 / ($totalScenes - 1);
console.log(brightness);
});
$(window).scroll(function() {
// is in viewport function
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
$(".scenes").each(function(index) {
if ($(this).isInViewport()) {
console.log("dit is " + this.id);
nextElment = this.id;
if (index >= $totalScenes / 2) {
$("li").css({ color: "#fff" });
} else {
$("li").css({ color: "#000" });
}
}
});
});
$("#nextButton").click(function() {
$("html, body").animate({
scrollTop: $("#" + nextElment)
.next()
.offset().top
});
//          scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});
$("#previousButton").click(function() {
$("html, body").animate({
scrollTop: $("#" + nextElment)
.prev()
.offset().top
});
//          scrollTop: $(this).parent().next().find('.scenes').offset().top } , 100);
});

最新更新