javascript - 带有按钮和渐变的水平滚动区域

标签 javascript jquery css horizontal-scrolling

到目前为止,这是我的代码:

// Show and hide gradients

$(document).ready(function() {
  $(".scroll-area").each(function(index) {
    if ($(this)[0].scrollWidth <= $(this)[0].clientWidth) {
      $(this).closest(".container").find(".left").css("display", "none");
      $(this).closest(".container").find(".right").css("display", "none");
    } else {
      $(this).scroll(function() {
        if ($(this)[0].scrollWidth > $(this)[0].clientWidth) {
          if ($(this).scrollLeft() > 0) {
            $(this).closest(".container").find(".left").css("display", "block");
          }

          if ($(this).scrollLeft() == 0) {
            $(this).closest(".container").find(".left").css("display", "none");
          }

          var fullWidth = $(this)[0].scrollWidth - $(this)[0].offsetWidth - 1;

          if ($(this).scrollLeft() >= fullWidth) {
            $(this).closest(".container").find(".right").css("display", "none");
          }

          if ($(this).scrollLeft() < fullWidth) {
            $(this).closest(".container").find(".right").css("display", "block");
          }
        }
      });
    }
  });
});


// Scroll buttons

let interval;

$('.scroll-btn').on('mousedown', ({
  target
}) => {
  const type = $(target).attr('id');

  interval = setInterval(() => {
    var x = $('#a').scrollLeft();
    $('#a').scrollLeft(type === 'left-arrow' ? x - 10 : x + 10);
  }, 50);
});

$('.scroll-btn').on('mouseup', () => clearInterval(interval));
* {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  font-size: 16px;
}

.container {
  width: 550px;
  height: 80px;
  background-color: grey;
  position: relative;
  margin-bottom: 20px;
}

.scroll-area {
  white-space: nowrap;
  overflow-x: auto;
  height: 100%;
}

.left,
.right {
  width: 50px;
  height: 100%;
  position: absolute;
  pointer-events: none;
  top: 0;
}

.left {
  background: linear-gradient(90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
  left: 0;
  display: none;
}

.right {
  background: linear-gradient(-90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
  right: 0;
}

.arrow {
  display: block;
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 15px;
  cursor: pointer;
}

.left-arrow {
  left: 0;
}

.right-arrow {
  right: 0;
}

.left-arrow div,
.right-arrow div {
  font-size: 40px;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div class="container">

  <div id="x" class="left"></div>
  <div class="right"></div>

  <div class="arrow left-arrow">
    <div class="scroll-btn" id="left-arrow">
      <</div>
    </div>
    <div class="arrow right-arrow">
      <div class="scroll-btn" id="right-arrow">></div>
    </div>

    <div id="a" class="scroll-area">
      <div class="text">Scroll to right. The gradients and arrows should appear and disappear based on the scroll position. It should work with more than one container. Lorem ipsum.</div>
    </div>
  </div>

需求是:

  1. 箭头应该以与渐变相同的方式出现和消失。
  2. 如果没有足够的文本来形成可滚动区域,则不应有渐变和现在的箭头。
  3. 最后应该不止一个容器。

有人可以帮我做吗?我将非常感激!

最佳答案

您可以将箭头放在左/右渐变 div 中。这样,它们将以与渐变相同的方式显示/隐藏。

编辑

我稍微清理了一下代码,因为原来的答案有点乱。 (或者像 mstephen19 所说的那样‘怪异’:))。

// Show gradient and left/right arrows only if scrollable
$(".scroll-area").each((i, el) => {
  $(el).parent().find(".right")[el.scrollWidth > el.clientWidth ? "show" : "hide"]();
});

// Show/hide gradient and arrows on scroll
$('.scroll-area').scroll((e) => {
  const fullWidth = $(e.target)[0].scrollWidth - $(e.target)[0].offsetWidth - 1;
  const left = $(e.target).scrollLeft()

  $(e.target).parent().find(".left, .left-arrow")[left > 0 ? "show" : "hide"]();
  $(e.target).parent().find(".right, .right-arrow")[left < fullWidth ? "show" : "hide"]();
});

// Scroll on left/right arrow mouse down
let intervalId;
$(".left-arrow, .right-arrow").on("mousedown", (e) => {
  const scroll = $(e.target).closest(".container").find(".scroll-area");
  intervalId = setInterval(() => {
    const left = scroll.scrollLeft();
    scroll.scrollLeft(e.target.classList.contains("left-arrow") ? left - 10 : left + 10);
  }, 50);
}).on("mouseup mouseleave", () => {
  clearInterval(intervalId);
});
* {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  font-size: 16px;
}

.container {
  width: 550px;
  height: 80px;
  background-color: grey;
  position: relative;
  margin-bottom: 20px;
  margin-left: 20px;
}

.scroll-area {
  white-space: nowrap;
  overflow-x: auto;
  height: 100%;
}

.left,
.right {
  width: 50px;
  height: 100%;
  position: absolute;
  top: 0;
}

.left {
  background: linear-gradient(90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
  left: 0;
  display: none;
}

.right {
  background: linear-gradient(-90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
  right: 0;
  text-align: right;
}

.left-arrow,
.right-arrow {
  margin: 0 10px;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
  cursor: pointer;
  user-select: none;
  font-size: 40px
}

.left-arrow {
  display: none;
  left: -25px;
}

.right-arrow {
  right: -25px;
}
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
  <div class="container">
    <div class="left"></div>
    <div class="right"></div>
    
    <div class="left-arrow">&lt;</div>
    <div class="right-arrow">&gt;</div>

    <div class="scroll-area">
      <div class="text">Scroll to right. The gradients and arrows should appear and disappear based on the scroll position. It should work with more than one container. Lorem ipsum.</div>
    </div>
  </div>
  
  <div class="container">
    <div class="left"><span class="left-arrow">&lt;</span></div>
    <div class="right"><span class="right-arrow">&gt;</span></div>

    <div class="scroll-area">
      <div class="text">No scroll.</div>
    </div>
  </div>

</body>

</html>

关于javascript - 带有按钮和渐变的水平滚动区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71722861/

相关文章:

css - polymer 1.x : Formatting paper-icon-button-lite

javascript - 当我有嵌套指令时,如何访问特定的父作用域?

javascript - 无法使用 Node js process.kill(pid) 终止 linux 进程

javascript - 第一秒调用函数javascript

jquery - 使用 jquery 动画并将 div 滚动到顶部

jquery - Chrome 中的长 Ajax 请求显示等待

javascript - 在 javascript 而不是 CSS(悬停)中更改链接颜色 --- 在 jsfiddle 而不是浏览器中有效?

javascript - 如何从不知道 Redux 的服务中分派(dispatch)操作?

javascript - 如何将 smooth-scrollbar 与其他插件对齐?

jquery - 向下滚动时出现并缩放