如何使用侧边滚动按钮具体滚动每4个格?
在之前的一个问题上,我询问了如何循环和简化每次溢出中多个侧滚动按钮的代码-x:How to loop and simplify code of multiple side scrolling buttons that scrolls specific row in javascript?
现在,Elna Haim回答了这个问题,我向任何熟悉JavaScript的人寻求帮助,了解如何在单击"下一步"和"返回"按钮时特定地滚动每4个分区并特定地停止并将4个框放入其中.
它可以使用data-scroll="1280"滚动,但每次屏幕大小改变时,滚动也会被中断,并且您将能够在下面的代码中看到迪夫被切成两半.
Also there's a problem in the margin not triggering in the code snippet, I don't know why.
const nextbtns = document.querySelectorAll('.next')
const backbtns = document.querySelectorAll('.back')
for (let nxt of nextbtns) {
nxt.addEventListener("click", () => {
const con = nxt.getAttribute("data-con");
const target = nxt.getAttribute("data-scroll");
document.querySelector(`#${con}`).scrollLeft += parseInt(target, 10);
});
}
for (let bck of backbtns) {
bck.addEventListener("click", () => {
const con = bck.getAttribute("data-con");
const target = bck.getAttribute("data-scroll");
document.querySelector(`#${con}`).scrollLeft -= parseInt(target, 10);
});
}
.row {
width: 100%;
height: 270px;
overflow-x: hidden;
-ms-overflow-style: none;
scrollbar-width: none;
}
.container {
overflow-x: hidden;
white-space: nowrap;
-ms-overflow-style: none;
scrollbar-width: none;
scroll-behavior: smooth;
transition: scroll-behavior .5s ease-in-out;
}
.box {
width: 24%;
height: 180px;
background-color: #171717;
border-radius: 20px;
display: inline-block;
margin-right: 14;
}
.btns button {
--color: #202020;
background-color: #202020;
padding: 8px 17.5px 12px 17.5px;
border: 3px solid var(--color);
color: grey;
border-radius: 50px;
font-family: 'Arial Black';
position: relative;
bottom: 0px;
}
<html>
<body>
<div class="row">
<a class="btns">
<button type="button" class="back" data-con="con" data-scroll="1321">‹</button>
<button type="button" class="next" data-con="con" data-scroll="1321">›</button>
</a>
<div class="container" id="con">
<center>
<div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div>
</center>
</div>
</div>
</body>
</html>
对于jsfiddle:https://jsfiddle.net/c0f9da82/ 余量仍然不起作用,我想只有在本地保存代码时它才会起作用.
感谢Elna Haim回答之前的问题,并希望再次回答这个问题!如果其他人可以帮助回答这个问题,我们将不胜感激!
我还有另一个问题:周围有人同时擅长JavaScript和Youtube api吗?,我正在使用lite-youtube js,我在为onStateChange添加事件监听器时感到困惑,我在github上使用pauliirish的Lite youtube js,并询问如何创建事件监听器来获取onStateChange结束参数.提前非常感谢!致所有想要回答我问题的人!