代码之家  ›  专栏  ›  技术社区  ›  The Old County

自定义滚动动画-暂停部分和水平滚动元素

  •  -1
  • The Old County  · 技术社区  · 5 年前

    有一些单页应用程序,用户可以在其中进行垂直滚动,但页面看起来暂停并水平滚动,然后清除一个部分。

    我添加了jquerywayward函数来检测何时显示嵌套排列。

    像这样:

    enter image description here

    这里是HTML片段。幻灯片1是一个完整的页面元素,幻灯片5和6也是如此。它们可以是菜单的锚定。我感兴趣的是在这里创建的行为-当用户接近嵌套单元时-它锁定在幻灯片2的顶部,然后转换幻灯片3和4。

    $win.on('scroll', function() {
      var top = $win.scrollTop() / 3;
      console.log("top", top);
      $nested.css('transform', 'translate(' + -top + 'px, 0)');
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="section">slide 1</div>
    <div class="nested">
      <div class="section first">slide 2</div>
      <div class="section second">slide 3</div>
      <div class="section third">slide 4</div>
    </div>
    <div class="section">slide 5</div>
    <div class="section">slide 6</div>

    JSFiddle Example

    0 回复  |  直到 5 年前
        1
  •  7
  •   Munim Munna Gandalf    5 年前

    当幻灯片成为固定元素时,它会保留垂直空间。

    下面的代码可以处理同一页中的多个幻灯片,您还可以实现 一旦你有了这个想法,你自己的。

    $('.nested').each(function() {
      let $window = $(window), $body = $('body');
      let $nested = $(this), $nestedPlaceholder = $nested.parent();
      let verticalScrollRange, upperMargin, lowerMargin;
      $window.resize(function(){
        $nested.removeClass("sticky").css({left: 0});
        let placeholderHeight = $nestedPlaceholder.css({height: ''}).height();
        verticalScrollRange = placeholderHeight - $window.height();
        upperMargin = $nestedPlaceholder.offset().top;
        lowerMargin = upperMargin + verticalScrollRange;
        $nestedPlaceholder.height(placeholderHeight);
      });
      $window.scroll(function() {
        let scrollTop = $window.scrollTop();
        if (scrollTop > upperMargin && scrollTop < lowerMargin) {
          $nested.addClass("sticky");
          let horizontalScrollRange = $nested.width() - $body.width();
          let verticalScrollPosition = scrollTop - upperMargin;
          let horizontalScrollPosition = verticalScrollPosition / verticalScrollRange * horizontalScrollRange;
          $nested.css({left: -horizontalScrollPosition});
        } else {
          $nested.removeClass("sticky");
        }
      });
      $window.resize();
    });
    body {
      background: linear-gradient(to bottom, #ffcb77 0%, #deaaff 100%);
    }
    
    .section {
      height: 100vh;
      font-size: 5em;
      text-align: center;
      position: relative;
      border: 1px solid red;
    }
    
    .nested .section {
      width: 100vw;
    }
    
    .nested-placeholder {
      overflow: hidden;
    }
    
    .sticky {
      position: fixed;
      z-index: 1;
      top: 0;
      height: 100vh;
      white-space: nowrap;
    }
    
    .sticky .section {
      display: inline-block;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="section">Start scrolling down!</div>
    <div class="section">slide 1</div>
    <div class="nested-placeholder">
      <div class="nested">
        <div class="section first">slide 2</div>
        <div class="section second">slide 3</div>
        <div class="section third">slide 4</div>
      </div>
    </div>
    <div class="section">slide 5</div>
    <div class="section">slide 6</div>
        2
  •  0
  •   The Old County    5 年前

    https://jsfiddle.net/zny0c8s6/

    当嵌套的幻灯片到达页面顶部时,会向嵌套中添加一个粘性类。这将固定批次位置,并将嵌套转换为松散的旋转木马。当用户继续向下滚动时,嵌套将水平平移。在最后一个嵌套的幻灯片被清除之后,我已经移除了粘滞类-为了让用户出现在正常的旅程中,我尝试将嵌套固定到幻灯片的高度和计数。尽管需要解决反向问题——如果有多个巢穴的话也是如此。

    $nested.waypoint(function(direction) {    
      var distance = $nested.offset().top;
      var $nestedHeight = $nested.height();
      var $nestedCount = $nested.find(" > .section").length;
      var $nestedSectionWidth = $nested.next(".section").eq(0).width();
    
      var clearNestedWidth = $nestedSectionWidth * $nestedCount;
      var $window = $(window);
    
      $window.scroll(function() {
        if ($window.scrollTop() >= distance) {
          // Your div has reached the top        
          $nested.addClass("sticky");
    
          var $nestedCompactWidth = $nested.width();    
          var $win = $(window);
    
          $win.on('scroll', function() {
            var top = $win.scrollTop() / 3;   
            $nested.css('transform', 'translate(' + -top + 'px, 0)');
    
            if (top >= clearNestedWidth) {
              console.log("cleared slides - now clear sticky")
              $nested.removeClass("sticky");              
              $nested.css('height', $nestedHeight);
              //set scroll to end of slide 2
            }
          });
    
        } else {
          //$nested.removeClass("sticky");
        }
      });
    
    
    }, {
      offset: '50%'
    });