HTML JS 轮播滑块,但不会滑动

huangapple go评论62阅读模式
英文:

HTML JS Carousel Slider that doesn't slide

问题

@charset "utf-8";
/* CSS Document */
body{
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: 100vh;
	text-align: center;
}

.content{
	height: 400px;
	width: 750px;
	overflow: hidden;
}

.content .images{
	height: 100%;
	width: 100%;
}

.images div{
	height: 100%;
	width: 100%;
}

.btm-sliders{
	position: absolute;
	bottom: 20px;
	left: 50%;
	display: flex;
	transform: translate(-50%);
}

.btm-sliders span{
	height: 15px;
	width: 50px;
	border: 4px solid red;
	margin: 0 3px;
	cursor: pointer;
}

.content .slide{
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	width: 45px;
	border: 2px solid red;
	cursor: pointer;
	background: rgba(255,255,255,0.1);
}

.content .slide:hover{
	background-color:#0d0155;
}

.slide span{
	font-size: 35px;
	color: red;
	line-height: 41px;
}
.content .right{
	right: 5px;
}
.content .left{
	left: 5px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="styles-carousel.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/ad445e50d3.js" crossorigin="anonymous"></script>
</head>

<body>
	<div class="content">
		<div class="images">
			<div class=""></div>You</div>
			<div class=""></div>should</div>
			<div class=""></div>see</div>
			<div class=""></div>this</div>
			<div class=""></div>one at a time</div>
		</div>
		<div onClick="side_slide(-1)" class="slide left">
			<span class="fas fa-angle-left"></span>
		</div>
		<div onClick="side_slide(1)" class="slide right">
			<span class="fas fa-angle-right"></span>
		</div>
		<div class="btm-sliders">
			<span onClick="btm_slide(1)"></span>
			<span onClick="btm_slide(2)"></span>
			<span onClick="btm_slide(3)"></span>
			<span onClick="btm_slide(4)"></span>
			<span onClick="btm_slide(5)"></span>
		</div>
	</div>
	<script>
		var indexValue = 1;
		showDiv(indexValue);
		function btm_slide(e){showDiv(indexValue = e);}
		function side_slide(e){showDiv(indexValue += e);}
		function showImg(e){
			var i;
			const Div = document.querySelectorAll('div');
			const sliders = document.querySelectorAll('.btm-sliders span');
			if(e > Div.length){indexValue = 1}
			if(e < 1){indexValue = Div.length}
			for(i = 0; i < div.length; i++){
				Div[i].style.display = "none";
			}
			for(i = 0; i< Div.length; i++){
				sliders[i].style.background = "rgba(255,255,255,0.1)";
			}
			Div[indexValue-1].style.display = "block";
			sliders[indexValue-1].style.background= "white";
		}
	</script>
</body>
</html>
英文:

OK, I currently have an HTML carousel slider with some JS involved.

In the interests is not only keeping what is left of my sanity in check (I know, "sanity" right? Remined me what that is again?) but also to keep from picking my computer up and throwing it against the wall repeatedly, can any look at the code below and tell me why I cannot get the slides to - well - slide.

Thank you

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@charset &quot;utf-8&quot;;
/* CSS Document */
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
text-align: center;
}
.content{
height: 400px;
width: 750px;
overflow: hidden;
}
.content .images{
height: 100%;
width: 100%;
}
.images div{
height: 100%;
width: 100%;
}
.btm-sliders{
position: absolute;
bottom: 20px;
left: 50%;
display: flex;
transform: translate(-50%);
}
.btm-sliders span{
height: 15px;
width: 50px;
border: 4px solid red;
margin: 0 3px;
cursor: pointer;
}
.content .slide{
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 45px;
/*height: 45px;*/
border: 2px solid red;
cursor: pointer;
background: rgba(255,255,255,0.1);
}
.content .slide:hover{
background-color:#0d0155;
}
.slide span{
font-size: 35px;
color: red;
line-height: 41px;
}
.content .right{
right: 5px;
}
.content .left{
left: 5px;
}

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;link href=&quot;styles-carousel.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;script src=&quot;https://kit.fontawesome.com/ad445e50d3.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;div class=&quot;images&quot;&gt;
&lt;div class=&quot;&quot;&gt;You&lt;/div&gt;
&lt;div class=&quot;&quot;&gt;should&lt;/div&gt;
&lt;div class=&quot;&quot;&gt;see&lt;/div&gt;
&lt;div class=&quot;&quot;&gt;this&lt;/div&gt;
&lt;div class=&quot;&quot;&gt;one at a time&lt;/div&gt;
&lt;/div&gt;
&lt;div onClick = &quot;side_slide(-1)&quot; class=&quot;slide left&quot;&gt;
&lt;span class=&quot;fas fa-angle-left&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div onClick = &quot;side_slide(1)&quot; class=&quot;slide right&quot;&gt;
&lt;span  class=&quot;fas fa-angle-right&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;btm-sliders&quot;&gt;
&lt;span onClick = &quot;btm_slide(1)&quot;&gt;&lt;/span&gt;
&lt;span onClick = &quot;btm_slide(2)&quot;&gt;&lt;/span&gt;
&lt;span onClick = &quot;btm_slide(3)&quot;&gt;&lt;/span&gt;
&lt;span onClick = &quot;btm_slide(4)&quot;&gt;&lt;/span&gt;
&lt;span onClick = &quot;btm_slide(5)&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
var indexValue = 1;
showDiv(indexValue);
function btm_slide(e){showDiv(indexValue = e);}
function side_slide(e){showDiv(indexValue += e);}
function showImg(e){
var i;
const Div = document.querySelectorAll(&#39;div&#39;);
const sliders = document.querySelectorAll(&#39;.btm-sliders span&#39;);
if(e &gt; Div.length){indexValue = 1}
if(e &lt; 1){indexValue = Div.length}
for(i = 0; i &lt; div.length; i++){
Div[i].style.display = &quot;none&quot;;
}
for(i = 0; i&lt; Div.length; i++){
sliders[i].style.background = &quot;rgba(255,255,255,0.1)&quot;;
}
Div[indexValue-1].style.display = &quot;block&quot;;
sliders[indexValue-1].style.background= &quot;white&quot;;
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

尝试这样做。更新你的代码:

var indexValue = 1;
showDiv(indexValue);

function btm_slide(e) {
    showDiv(indexValue = e);
}

function side_slide(e) {
    showDiv(indexValue += e);
}

function showDiv(e) {
    let i;
    let slides = document.getElementsByClassName("mySlides");
    let dots = document.getElementsByClassName("dot");
    if (e > slides.length) {
        indexValue = 1
    }
    if (e < 1) {
        indexValue = slides.length
    }
    for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }
    for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[indexValue - 1].style.display = "block";
    dots[indexValue - 1].className += " active";
}
@charset "utf-8";
/* CSS Document */
body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    text-align: center;
}

.content {
    height: 400px;
    width: 750px;
    overflow: hidden;
}

.content .images {
    height: 100%;
    width: 100%;
}

.images div {
    height: 100%;
    width: 100%;
}

.btm-sliders {
    position: absolute;
    bottom: 20px;
    left: 50%;
    display: flex;
    transform: translate(-50%);
}

.btm-sliders span {
    height: 15px;
    width: 50px;
    border: 4px solid red;
    margin: 0 3px;
    cursor: pointer;
}

.content .slide {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 45px;
    border: 2px solid red;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.1);
}

.content .slide:hover {
    background-color: #0d0155;
}

.slide span {
    font-size: 35px;
    color: red;
    line-height: 41px;
}

.content .right {
    right: 5px;
}

.content .left {
    left: 5px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="styles-carousel.css" rel="stylesheet" type="text/css">
<script src="https://kit.fontawesome.com/ad445e50d3.js" crossorigin="anonymous"></script>
</head>
<body>
    <div class="content">
        <div class="images">
            <div class="mySlides">You</div>
            <div class="mySlides">should</div>
            <div class="mySlides">see</div>
            <div class="mySlides">this</div>
            <div class="mySlides">one at a time</div>
        </div>
        <div onClick="side_slide(-1)" class="slide left">
            <span class="fas fa-angle-left"></span>
        </div>
        <div onClick="side_slide(1)" class="slide right">
            <span class="fas fa-angle-right"></span>
        </div>
        <div class="btm-sliders">
            <span class="dot" onClick="btm_slide(1)"></span>
            <span class="dot" onClick="btm_slide(2)"></span>
            <span class="dot" onClick="btm_slide(3)"></span>
            <span class="dot" onClick="btm_slide(4)"></span>
            <span class="dot" onClick="btm_slide(5)"></span>
        </div>
    </div>
</body>
</html>
英文:

Try this. Updated your code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

        var indexValue = 1;
showDiv(indexValue);
function btm_slide(e){showDiv(indexValue = e);}
function side_slide(e){showDiv(indexValue += e);}
function showDiv(e){
let i;
let slides = document.getElementsByClassName(&quot;mySlides&quot;);
let dots = document.getElementsByClassName(&quot;dot&quot;);
if (e &gt; slides.length) {indexValue = 1}    
if (e &lt; 1) {indexValue = slides.length}
for (i = 0; i &lt; slides.length; i++) {
slides[i].style.display = &quot;none&quot;;  
}
for (i = 0; i &lt; dots.length; i++) {
dots[i].className = dots[i].className.replace(&quot; active&quot;, &quot;&quot;);
}
slides[indexValue-1].style.display = &quot;block&quot;;  
dots[indexValue-1].className += &quot; active&quot;;
}

<!-- language: lang-css -->

@charset &quot;utf-8&quot;;
/* CSS Document */
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
text-align: center;
}
.content{
height: 400px;
width: 750px;
overflow: hidden;
}
.content .images{
height: 100%;
width: 100%;
}
.images div{
height: 100%;
width: 100%;
}
.btm-sliders{
position: absolute;
bottom: 20px;
left: 50%;
display: flex;
transform: translate(-50%);
}
.btm-sliders span{
height: 15px;
width: 50px;
border: 4px solid red;
margin: 0 3px;
cursor: pointer;
}
.content .slide{
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 45px;
/*height: 45px;*/
border: 2px solid red;
cursor: pointer;
background: rgba(255,255,255,0.1);
}
.content .slide:hover{
background-color:#0d0155;
}
.slide span{
font-size: 35px;
color: red;
line-height: 41px;
}
.content .right{
right: 5px;
}
.content .left{
left: 5px;
}

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;link href=&quot;styles-carousel.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;script src=&quot;https://kit.fontawesome.com/ad445e50d3.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;div class=&quot;images&quot;&gt;
&lt;div class=&quot;mySlides&quot;&gt;You&lt;/div&gt;
&lt;div class=&quot;mySlides&quot;&gt;should&lt;/div&gt;
&lt;div class=&quot;mySlides&quot;&gt;see&lt;/div&gt;
&lt;div class=&quot;mySlides&quot;&gt;this&lt;/div&gt;
&lt;div class=&quot;mySlides&quot;&gt;one at a time&lt;/div&gt;
&lt;/div&gt;
&lt;div onClick = &quot;side_slide(-1)&quot; class=&quot;slide left&quot;&gt;
&lt;span class=&quot;fas fa-angle-left&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div onClick = &quot;side_slide(1)&quot; class=&quot;slide right&quot;&gt;
&lt;span  class=&quot;fas fa-angle-right&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;btm-sliders&quot;&gt;
&lt;span class=&quot;dot&quot; onClick = &quot;btm_slide(1)&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;dot&quot; onClick = &quot;btm_slide(2)&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;dot&quot; onClick = &quot;btm_slide(3)&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;dot&quot; onClick = &quot;btm_slide(4)&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;dot&quot; onClick = &quot;btm_slide(5)&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 14:11:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定