jquery - 如何根据我的滚动位置将 scrollTop 设置为下一个可见 div/类的动画?

标签 jquery html css

如果标题有点含糊,请原谅我,因为我不确定如何写下来。

基本上:

我们有一个包含 6 个“场景”的页面。他们都有类(class) .scene和不同的 ID,例如:scene1 , scene2等等……

起初,我们必须为每个场景动态改变颜色 -> 场景 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);
});

关于jquery - 如何根据我的滚动位置将 scrollTop 设置为下一个可见 div/类的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329915/

相关文章:

jquery-selectors - jquery.live 未调用

javascript - WebGL texImage2D : Conversion requires pixel reformatting

javascript - 溢出 :auto not working in touch devices(iOS)

php - 在 PHP 中解析 CSS

jquery - 根据事件类获取数据属性的值

jquery - 鼠标悬停时打开 div,并在单击同一 div 中的链接时保持其打开状态

javascript - 用户单击使用按钮后更新表

javascript - 多重选择中只有一个值等于并被选中

javascript - JQuery Mobile,将类添加到动态 ID

javascript - addclass 函数在 firefox 和 ie 中不起作用