【问题标题】:How to create a progress bar inside a sidebar?如何在侧边栏中创建进度条?
【发布时间】:2017-04-23 01:15:59
【问题描述】:

我的问题是关于增加 li 内 div 的宽度。

基本上,当用户滚动时,侧边栏(右侧)li 会将 span红色背景色)从 0 增加到 100。换句话说,如果第一个对应的 div 在视图中,然后对应的 li 元素的 span 将随着用户向下或向上滚动而从 0 增加到 100%。这将指示用户滚动了多少 div。

我有一个示例代码笔,它显示了我正在尝试做的事情 https://codepen.io/hellouniverse/pen/xdVJQp

我的 CSS 看起来像

.sidebar {
position: fixed;
max-width: 300px;
&__list {
    position: relative;
    text-align: center;
    padding: 10px;
    margin: 10px auto;
    border: 2px solid #f3f2f0;
    list-style-type: none;
    display: inline-flex;
    flex-direction: column;
}
// The primary red bar at the bottom that increases in width with scroll
span {
    background-color: red;
    height: 5px;
}

}

问题出在js中 在 JS 中,我猜 1.需要计算对应div的高度 2.根据1计算的高度,按一定比例增加跨度。

我似乎根本无法弄清楚代码。有人可以帮忙吗?

// Checks whether an elem gets into view of the window
function isScrolledIntoView(elem) {
    'use strict';
    if (elem.length > 0) {
        var docViewTop = jQuery(window).scrollTop();
        var docViewBottom = docViewTop + jQuery(window).height();
        var elemTop = jQuery(elem).offset().top;
        return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
    }
}

var lastScrollTop = 0;

$(window).scroll(function() {

    // Increase & Decrease width of the span of li as you scroll

        var height = $('section-just-in').height();
        var incrementBy = 300 / height;


        $('.sidebar__list span').animate({
            // increase width from 0 till 100%
            width : width + incrementBy
        });

})

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    类似这样的:

    https://codepen.io/hdl881127/pen/ZKOaBY

    或这里的jsfiddle:

    https://jsfiddle.net/dalinhuang/Lw72csjd/

    第一次获取每个部分的高度

    第二次滚动,检查每个部分的进度

    最后将进度条应用到每个按钮。

    $(document).ready(function() {
      var theone = $('#section-just-in');
      var thetwo = $('#videos');
      var thethree = $('#aboutteam');
      var theone_h, thetwo_h, thethree_h;
    
      theone_h = theone.innerHeight();
      thetwo_h = thetwo.innerHeight();
        // remove the window height cus
        // we are not able to full scroll down when we hit the bottom of the page
      thethree_h = thethree.innerHeight() - $(window).height();
    
      $(document).scroll(function() {
        var page_height = $("body").scrollTop();
    
        var pos_2 = page_height > theone_h ? page_height - theone_h : 0;
        var pos_3 = page_height > (thetwo_h+theone_h) ? (page_height - (+thetwo_h + +theone_h)) : 0;
        var progress_1 = (page_height / theone_h) >= 1 ? 100 : (page_height / theone_h) * 100;
        var progress_2 = (pos_2 / thetwo_h) >= 1 ? 100 : (pos_2 / thetwo_h) * 100;
        var progress_3 = ((pos_3) / thethree_h) >= 1 ? 100 : ((pos_3) / thethree_h) * 100;
    
        $("ul li:nth-child(1) span").stop().animate({
          "width": progress_1 + '%'
        }, 0);
        $("ul li:nth-child(2) span").stop().animate({
          "width": progress_2 + '%'
        }, 0);
        $("ul li:nth-child(3) span").stop().animate({
          "width": progress_3 + '%'
        }, 0);
      });
    
    
    
    
      // Checks whether an elem gets into view of the window
      function isScrolledIntoView(elem) {
        'use strict';
        if (elem.length > 0) {
          var docViewTop = jQuery(window).scrollTop();
          var docViewBottom = docViewTop + jQuery(window).height();
          var elemTop = jQuery(elem).offset().top;
          return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
        }
      }
    });
    

    【讨论】:

    • 完美。非常感谢
    • 嗨@Daniel H,看起来很完美。只是一个小问题。如果在第一个元素开始之前有很多元素(即 $('#section-just-in') ),则进度条开始得太快。可以在看到刚刚进入的部分时启动吗?
    【解决方案2】:

    给你:

    var height,
        incrementBy,
        visible_height;
    
    $(document).ready(function(){
        visible_height = $('.section-just-in').innerHeight(); 
    });
    
    $(document).scroll(function() {
        var scroll_top = Math.max( $("html").scrollTop(), $("body").scrollTop());
    
        incrementBy = (100 * scroll_top) / visible_height / 1.4;
    
        $('.sidebar__list span').stop().animate({
            "width" : incrementBy
        });
    });
    

    只需替换 isScrolledIntoView 函数之后的所有内容,它应该可以工作。

    【讨论】:

    • 看起来很接近。我认为OP希望跨度一个接一个地增加,相应的部分。
    • 谢谢@Aca85。但是,我真正想要的是增加每个的宽度,即与相应的部分一个接一个地增加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2010-10-09
    • 1970-01-01
    • 2020-10-26
    • 2023-01-29
    • 1970-01-01
    相关资源
    最近更新 更多