我试图在div中向左或向右滚动,仅使用javascript而没有jquery用于某个目的。我用jquery实现了它,但我需要单独使用javascript来实现它。代码: -
我希望这些按钮能够帮助向左或向右水平滚动div。如何做到这一点。我用这种方式使用jquery实现了它: -
pip install pylint

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

.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;
}

答案 0 :(得分:1)
您只需使用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" + ")";
});

.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;
}

<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>
&#13;
答案 1 :(得分:0)
使用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);
}
});
.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;
}
<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>
事实证明,MS Edge和IE不支持scrollTo()
方法。
答案 2 :(得分:0)
您可以将其用于滚动,但动画将是独立的。
document.getElementById("right-button").addEventListener("click", function()
{
$('#content').animate({ scrollLeft: "+=200px" }, "slow");
});
document.getElementById("left-button").addEventListener("click", function(){
$('#content').animate({ scrollLeft: "-=200px" }, "slow");
});
答案 3 :(得分:0)
这是使用核心javascript进行自定义水平滚动的解决方案
在.html
中 <div class="pull-left mt-sm">
<i class="icon-arrow-left pointer" onclick="scrollLeft()"></i>
</div>
<div #widgetsContent class="custom-slider-main">
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
<div class="info-box">
<img src="file.jpg" class="info-box">
</div>
</div>
<div class="pull-right mt-sm">
<i class="icon-arrow-right pointer" onclick="scrollRight()"></i>
</div>
在.css中
.custom-slider-main{
display: flex;
overflow: hidden;
scroll-behavior: smooth;
}
.info-box{
width: 50px;
height:50px
}
在.js
让widgetsContent = document.getElementById(“ widgetsContent”)
然后单击左右按钮,使用以下功能
scrollLeft(){
widgetsContent.nativeElement.scrollLeft -= 150;
}
scrollRight(){
widgetsContent.nativeElement.scrollLeft += 150;
}