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

时间:2019-06-22 07:54:39

标签: javascript horizontal-scrolling vertical-scrolling

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

我不确定他们是如何做到的-我已经看到了变换样式的应用,但是我不确定如果用户正常滚动,本来应该存在的元素会发生什么。

我添加了jquery任性函数来检测何时显示嵌套的跨度。

类似的事情:

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

2 个答案:

答案 0 :(得分:4)

这只是为了让您了解如何实现它。您需要一个占位符容器 当幻灯片变成固定元素时,可以保留垂直空间。

以下代码可以处理同一页面中的多张幻灯片,也可以实现 有了想法就可以自己动手。

$('.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>

答案 1 :(得分:0)

我想出了一种解决方案,当幻灯片到达页面顶部时,该解决方案将激活水平滚动粘滞。

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%'
});