如何使用javaScript使用下一个和以前的按钮,一一显示列表项目,请不要jQuery
我正在学习JavaScript,并希望制作一个非常简单的滑块,以了解事物如何工作并在我的项目中使用它。
这是Codepen中的代码,此处是
所需的结果:
- DIV1默认为第一幻灯片。
- 用户按下下一步,将隐藏Div1,然后显示Div2,依此类推。
- 用户按上一个按钮将相反。
- 用户在末尾或上一个阵列开始时按下一个将循环循环。
感谢您的帮助。
https://codepen.io/willpower_7/pen/pen/govjale
var listItems = document.getElementById("slides-container-li").children;
function clickNext() {
listItems[0].style.display = "none";
listItems[1].style.display = "inline";
}
function clickPrevious() {
listItems[0].style.display = "inline";
listItems[1].style.display = "none";
}
.container {
background: gray;
width: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
.slides-container-li {
width: 90%;
height: 90%;
background: lightblue;
display: flex;
text-align: center;
}
.div1 {
width: 90%;
height: 100%;
background: orange;
margin: auto;
display: inline;
font-size: 100px;
}
.div2 {
width: 90%;
height: 100%;
background: red;
margin: auto;
display: none;
font-size: 100px;
}
.div3 {
width: 90%;
height: 100%;
background: yellow;
margin: auto;
display: none;
font-size: 100px;
}
.btn-container {
width: 100%;
display: flex;
background: lightgray;
justify-content: center;
}
<div class="container">
<li class="slides-container-li" id="slides-container-li">
<div class="div1">div1</div>
<div class="div2">div2</div>
<div class="div3">div3</div>
</li>
</div>
<div class="btn-container">
<button class="Previous-btn" id="Previous-btn" onclick="clickPrevious()">Previous</button>
<button class="slide-btn" id="slide-btn" onclick="clickNext()">next</button>
</div>
I'm learning javascript and want to make a very simple slider to understand how things work and use it in my project.
here is the code in codepen and here too
desired result:
- div1 is inline by default as first slide.
- user press next, will hide div1 then show div2 and so on.
- user press previous button, will do the opposite.
- user press next at the end or previous at the start of array will loop.
thanks for any help.
https://codepen.io/Willpower_7/pen/gOvJaLe
var listItems = document.getElementById("slides-container-li").children;
function clickNext() {
listItems[0].style.display = "none";
listItems[1].style.display = "inline";
}
function clickPrevious() {
listItems[0].style.display = "inline";
listItems[1].style.display = "none";
}
.container {
background: gray;
width: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
.slides-container-li {
width: 90%;
height: 90%;
background: lightblue;
display: flex;
text-align: center;
}
.div1 {
width: 90%;
height: 100%;
background: orange;
margin: auto;
display: inline;
font-size: 100px;
}
.div2 {
width: 90%;
height: 100%;
background: red;
margin: auto;
display: none;
font-size: 100px;
}
.div3 {
width: 90%;
height: 100%;
background: yellow;
margin: auto;
display: none;
font-size: 100px;
}
.btn-container {
width: 100%;
display: flex;
background: lightgray;
justify-content: center;
}
<div class="container">
<li class="slides-container-li" id="slides-container-li">
<div class="div1">div1</div>
<div class="div2">div2</div>
<div class="div3">div3</div>
</li>
</div>
<div class="btn-container">
<button class="Previous-btn" id="Previous-btn" onclick="clickPrevious()">Previous</button>
<button class="slide-btn" id="slide-btn" onclick="clickNext()">next</button>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个无穷循环滑块,无论滑块的数量如何,请检查。
This is an infinity looping slider and will work whatever the count of sliders, please check.
您可以这样做,您必须进行一个运行时变量&amp;更改其对OnClick功能的价值
you can do like this , you have to take one runtime variable & change it's value on onclick function