[英]Fixing the right button of a vanilla Javascript carousel
Against all reason, I'm trying to create a vanilla JavaScript carousel. 出于所有原因,我试图创建一个普通的JavaScript轮播。 I am having two problems: 1. The images move left at widths of -680px as they should but when I tried to create the same function for the right button, the left value goes to 1370px making the picture off the screen. 我遇到两个问题:1.图像向左移动的宽度应为-680px,但是当我尝试为右按钮创建相同的功能时,左值变为1370px,使图片脱离屏幕。 2. I would like for it to slide left rather jump left (same for right), I managed to get it to do this but it doesn't work on the first slide, only from the second slide. 2.我想让它向左滑动而不是向左跳(与右边相同),我设法做到这一点,但是它在第一张幻灯片上不起作用,仅在第二张幻灯片上起作用。 Here is the HTML code just for the carousel: 这是仅用于轮播的HTML代码:
<div id = "container">
<div id = "carousel">
<div class = "slide"><img class = "slideImage" class = "active" src = "sithCover.png"></div>
<div class = "slide"><img class = "slideImage" src = "darthVader.png"></div>
<div class = "slide"><img class = "slideImage" src = "darthSidious.png"></div>
<div class = "slide"><img class = "slideImage" src = "kyloRen.png"></div>
</div>
</div>
<div id = "left" class = "button"></div>
<div id = "right" class = "button"></div>
Here is the CSS code: 这是CSS代码:
#container {
position: absolute;
top: 200px;
left: 100px;
width: 680px;
height: 360px;
white-space: nowrap;
overflow:hidden;
}
#carousel {
position: absolute;
width: 2740px;
height: 360px;
white-space: nowrap;
overflow: hidden;
transition: left 300ms linear;
}
.slide {
display: inline-block;
height: 360px;
width: 680px;
padding: 0;
margin: 0;
transition: left 300ms linear;
}
.slideImage {
position:relative;
height: 360px;
width: 680px;
float: left;
}
.button {
position: absolute;
top: 340px;
height: 60px;
width: 60px;
border-bottom: 12px solid red;
}
#left {
left: 115px;
border-left: 12px solid red;
transform: rotate(45deg);
}
#right {
left: 693px;
border-right: 12px solid red;
transform: rotate(-45deg);
}
Here is the JavaScript: 这是JavaScript:
var carousel = document.querySelector('#carousel');
var firstVal = 0;
document.querySelector('#left').addEventListener("click", moveLeft);
function moveLeft (){
firstVal +=685;
carousel.style.left = "-"+firstVal+"px";
};
document.querySelector('#right').addEventListener("click", moveRight);
function moveRight() {
firstVal +=685;
carousel.style.left = "+"+firstVal+"px";
};
Here is a JSFiddle so that you can see what I mean: " https://jsfiddle.net/way81/8to1kkyj/ " 这是一个JSFiddle,以便您可以了解我的意思:“ https://jsfiddle.net/way81/8to1kkyj/ ”
I appreciate your time in reading my question and any help would be much appreciated. 感谢您抽出宝贵时间阅读我的问题,我们将不胜感激。
Ofcourse it goes from -685px
on left click and then to +1370px
the next right click; Ofcourse从去-685px
左键单击,然后到+1370px
接下来右键点击; You are always adding 685 to your firstVal
variable. 您总是将685添加到您的firstVal
变量中。
firstVal = 0
//firstVal is worth 0
moveLeft()
//firstVal is now worth 685
moveRight()
//firstVal is now worth 1370.
The problem is that when you apply the firstVal
to your CSS thing in the javascript, you create a string to get your negative value (where you apply the "-" sign infront of firstVal
) 问题是,当您将firstVal
应用于firstVal
中的CSS时,您将创建一个字符串以获取负值(在firstVal
应用“-”号)
Instead, write them like this 而是这样写
function moveLeft (){
firstVal -=685; //note we now subtract, the "-" should appear when the number becomes negative
carousel.style.left = firstVal + "px";
};
function moveRight() {
firstVal +=685;
carousel.style.left = firstVal + "px";
};
var left = document.getElementById("left"); left.addEventListener("click", moveLeft, false); var right = document.getElementById("right"); right.addEventListener("click", moveRight, false); var carousel = document.getElementById("carousel"); var images = document.getElementsByTagName("img"); var position = 0; var interval = 685; var minPos = ("-" + interval) * images.length; var maxPos = interval * images.length; //slide image to the left side <-- function moveRight() { if (position > (minPos + interval)) { position -= interval; carousel.style.left = position + "px"; } if (position === (minPos + interval)) { right.style.display = "none"; } left.style.display = "block"; } //slide image to the right side --> function moveLeft() { if (position < (maxPos - interval) && position < 0) { position += interval; carousel.style.left = position + "px"; } if (position === 0) { left.style.display = "none"; } right.style.display = "block"; }
#container { position: absolute; top: 200px; left: 100px; width: 680px; height: 360px; white-space: nowrap; overflow: hidden; } #carousel { position: absolute; width: 2740px; height: 360px; white-space: nowrap; overflow: hidden; transition: left 300ms linear; } .slide { display: inline-block; height: 360px; width: 680px; padding: 0; margin: 0; transition: left 300ms linear; } .slideImage { position: relative; height: 360px; width: 680px; float: left; } .button { position: absolute; top: 340px; height: 60px; width: 60px; border-bottom: 12px solid red; } #left { left: 115px; border-left: 12px solid red; transform: rotate(45deg); display: none; } #right { left: 693px; border-right: 12px solid red; transform: rotate(-45deg); }
<div id="container"> <div id="carousel"> <div class="slide"> <img class="slideImage" class="active" src="sithCover.png" alt="slide1"> </div> <div class="slide"> <img class="slideImage" src="darthVader.png" alt="slide2"> </div> <div class="slide"> <img class="slideImage" src="darthSidious.png" alt="slide3"> </div> <div class="slide"> <img class="slideImage" src="kyloRen.png" alt="slide4"> </div> </div> </div> <div id="left" class="button"></div> <div id="right" class="button"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.