javascript - 如何用溢出 :auto with arrows 替换 div 的滚动条

标签 javascript jquery html css

我正在尝试制作一个小的 div,用于在电子商务网站的 jquery 图像缩放插件中的图片之间滚动。

这是我目前所拥有的。

<div style="width:125px;height:300px;overflow:auto;margin:auto;">
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:red;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:blue;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:pink;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:green;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:yellow;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:orange;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:black;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:brown;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:purple;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:grey;"></div>
</div>

这是一个 jsfiddle https://jsfiddle.net/2o482ofp/

我需要隐藏滚动条,而是使用固定的箭头按钮在 div 中上下移动。 我正在尝试模仿可以在 https://www.daraz.pk/ca-sports-black-pu-running-shoes-for-men-6605753.html 上找到的功能

我做了很多谷歌搜索,但找不到任何教程、指南、插件或任何关于我正在尝试这样做的东西,如果有人能提供帮助,我将不胜感激。

最佳答案

var blockHeight = 75;
var marginTop = 10;

$('.up').click(function(){
    
    var $elem = $('div>div:eq(0)');

		var currentOffset = parseInt(window.getComputedStyle($elem[0], null)['marginTop'].split('px')[0], 10);
    
    console.log('currentOffset', currentOffset);
    var result = currentOffset - blockHeight - marginTop;

		$elem.css("margin-top", result + "px");
});

$('.down').click(function(){

     var $elem = $('div>div:eq(0)');

		var currentOffset = parseInt(window.getComputedStyle($elem[0], null)['marginTop'].split('px')[0], 10);
    var result = currentOffset + blockHeight + marginTop;
    
    console.log('.currentOffset', currentOffset)

		$elem.css("margin-top", result + "px");

})
div > div {
  
  transition: margin 1s ease;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br>
<br>
<br>
<br>
<button class="up">up</button>
<button class="down">down</button>
<div style="width:125px;height:300px;overflow:hidden;margin:auto;">
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:red;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:blue;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:pink;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:green;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:yellow;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:orange;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:black;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:brown;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:purple;"></div>
  <div style="width:75px;height:75px;margin:auto;margin-top:10px;margin-bottom:10px;background-color:grey;"></div>
</div>

关于javascript - 如何用溢出 :auto with arrows 替换 div 的滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43978113/

相关文章:

php - 数据未存储在数据库中 [Laravel 5.5]

javascript - HTML - 按钮仅在第二次单击后取消

javascript - 我无法从我网站上的 facebook 登录获取电子邮件范围

javascript - 将 onRightClick 添加到 JavaScript 库 Hypertree

javascript - 将事件监听器添加到 chrome-extension 中的 for 循环中的复选框

javascript - Electron 应用程序移动构建文件夹时出错

javascript - 将日期对象转换为日期字符串

javascript - 如何停止 Razors 自动编码\n?

jquery选择器如何获取父元素的内容

html - 如何删除包装 img 的 div 中不需要的空间?