如何使用纯javascript函数而不使用jquery在div内向左或向右滚动?

J.D*_*Doe 3 javascript

我正在尝试仅使用 javascript 在 div 内向左或向右滚动,而没有使用 jquery。我已经使用 jquery 实现了它,但我需要单独使用 javascript 来实现它。代码:-

我希望按钮帮助沿 div 向左或向右水平滚动。如何做。我通过这种方式使用 jquery 实现了它:-

$('#right-button').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "+=200px"
  }, "slow");
});

 $('#left-button').click(function() {
  event.preventDefault();
  $('#content').animate({
    scrollLeft: "-=200px"
  }, "slow");
});
Run Code Online (Sandbox Code Playgroud)
.outer {
  display: flex;
  overflow-x: hidden;
  overflow-y: hidden;
}

.inner {
  flex: 0 0 25%;
  height: 100px;
  margin: 10px;
}

.paddle {
  position: absolute;
  top: 50px;
  bottom: 0;
  width: 30px;
  height: 20px;
}

.lefty {
  left: 0;
}

.righty {
  right: 0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer" id="content">
	<button class="lefty paddle" id="left-button"></button>
  <div class="inner" style="background:red"></div>
  <div class="inner" style="background:green"></div>
  <div class="inner" style="background:blue"></div>
  <div class="inner" style="background:yellow"></div>
  <div class="inner" style="background:orange"></div>
	<button class="righty paddle" id="right-button"></button>
</div>
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 12

你只需要使用translateX如下:另外,我强烈建议你不要使用js来制作像这样简单的动画。

const container = document.querySelector(".container");
const lefty = document.querySelector(".lefty");
let translate = 0;

lefty.addEventListener("click", function() {
  	translate += 200;
  	container.style.transform = "translateX(" + translate + "px" + ")";
});

const righty = document.querySelector(".righty");
righty.addEventListener("click", function() {
  	translate -= 200;
  	container.style.transform = "translateX(" + translate + "px" + ")";
});
Run Code Online (Sandbox Code Playgroud)
.outer {
  
  overflow-x: hidden;
	  overflow-y: hidden;

}

.container {
  display: flex;
  transition: transform .4s ease-in;
}

.inner {
  flex: 0 0 25%;
  height: 100px;
	margin:10px;
}
.paddle {
	position: absolute;
	top: 50px;
	bottom: 0;
	width: 30px;
	height:20px;
}
.lefty {
	left: 0;
  z-index:1;
}
.righty{
	right: 0;
   z-index:1;
}
Run Code Online (Sandbox Code Playgroud)
<button class="lefty paddle" id="left-button"></button>
<div class="outer" id="content">
  <div class="container">
    <div class="inner" style="background:red"></div>
    <div class="inner" style="background:green"></div>
    <div class="inner" style="background:blue"></div>
    <div class="inner" style="background:yellow"></div>
    <div class="inner" style="background:orange"></div>
  </div>
</div>
<button class="righty paddle" id="right-button"></button>
Run Code Online (Sandbox Code Playgroud)

  • 请不要将您的答案代码发布到第 3 方网站,因为这些链接会随着时间的推移而消失,然后您的答案对于找到它的人来说毫无意义。编辑您的问题并将您的代码插入到“代码片段”中。 (3认同)
  • 如何停止滚动到末尾? (2认同)

Don*_*own 7

用于scrollTo滚动元素:

var content = document.getElementById('content'),
    scrollStep = 200;

document.getElementById('right-button').addEventListener('click', function(e) {
  e.preventDefault();
  let sl = content.scrollLeft,
      cw = content.scrollWidth;
	
  if ((sl + scrollStep) >= cw) {
    content.scrollTo(cw, 0);
  } else {
    content.scrollTo((sl + scrollStep), 0);
  }
});

document.getElementById('left-button').addEventListener('click', function(e) {
  e.preventDefault();
  let sl = content.scrollLeft;
	
  if ((sl - scrollStep) <= 0) {
    content.scrollTo(0, 0);
  } else {
    content.scrollTo((sl - scrollStep), 0);
  }
});
Run Code Online (Sandbox Code Playgroud)
.outer {
  display: flex;
  overflow-x: hidden;
  overflow-y: hidden;
}

.inner {
  flex: 0 0 25%;
  height: 100px;
  margin:10px;
}
.paddle {
  position: absolute;
  top: 50px;
  bottom: 0;
  width: 30px;
  height:20px;
}
.lefty {
  left: 0;
}
.righty{
  right: 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="outer" id="content">
  <button class="lefty paddle" id="left-button"></button>
  <div class="inner" style="background:red"></div>
  <div class="inner" style="background:green"></div>
  <div class="inner" style="background:blue"></div>
  <div class="inner" style="background:yellow"></div>
  <div class="inner" style="background:orange"></div>
  <button class="righty paddle" id="right-button"></button>
</div>
Run Code Online (Sandbox Code Playgroud)

原来MS Edge和IE不支持scrollTo()方法。