jQuery-如何根据我的滚动位置将scrollTop设置为下一个可见的div/class的动画? [英] jquery - How do I scrollTop animate to the next visibile div/class depending on my scroll position?
问题描述
如果标题有点含糊,请原谅我,因为我不确定如何将其写下来.
Pardon me if the title is a bit vague, cause I wasn't sure how to write it down.
基本上:
我们有一个包含6个场景"的页面.它们都具有类.scene
和不同的ID,例如:scene1
,scene2
等...
We have a page with 6 'scenes'. They all have the class .scene
and a different ID, for example: scene1
, scene2
etc...
首先,我们必须为每个场景动态更改颜色->场景1是浅色,最后一个场景是黑色.到目前为止,即使在添加或删除场景时也可以使用.
at first, we had to dynamically change the color for each scene -> scene 1 is a light color, and the last scene is black. That works so far, even while adding or removing scenes.
页面的顶部和底部都有一个<li>
.带有可点击的<a>
标签.
There is a <li>
on the top and bottom of the page. Which has a clickable <a>
tag.
当我按下nextButton
时,根据我在页面上的位置,我需要转到下一个可见场景 .因此,如果滚动到场景2,然后按按钮,则需要转到场景3.如果滚动到场景5,然后按按钮,则需要转到场景6,以此类推.按钮previousButton
,但相反(向上).
When I press nextButton
, I need to go to the next visibile scene depending on my position on the page. So if I scroll to scene 2, and I press the button, I need to go to scene 3. If I scroll to scene 5 and press the button, I need to go to scene 6, etcetera... Same counts for the other button previousButton
, but the other way around (upwards).
此外,如果我使用nextButton
(场景6)到达页面的末尾,它将返回到场景1.所有操作均使用scrollTop
Also, if i reach the end of the page with nextButton
(scene 6), it will go back up to scene 1. All done using scrollTop
我一直在搜寻有关此内容的信息,但似乎无法弄清楚该做什么或如何开始.
I have been googling around in regards to this, but i cannot seem to figure out what to do exactly or how to start.
在$(window).scroll(function)
下编写的整个代码,直到each()
函数已经由导师提供给我们.
the whole piece of code that is written undere $(window).scroll(function)
'till the each()
function has been given to us by the tutor.
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 ↑</a></li>
<li id="next"><a id="nextButton" href="#">next ↓</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);
});
这篇关于jQuery-如何根据我的滚动位置将scrollTop设置为下一个可见的div/class的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!