简体   繁体   English

JavaScript图像滑块无法正常工作

[英]Javascript image slider not working properly

I'm trying to create an image slider with controls "Next" and "Previous". 我正在尝试使用“下一个”和“上一个”控件创建图像滑块。 Next should slide the image left using a negative margin-left property so as to reveal the second image. 接下来,应使用负的margin-left属性向左滑动图像,以显示第二个图像。 All the list elements holding individual images are set to display: inline-block so each list element can stand next to each other instead of stacking. 所有保存单个图像的列表元素都设置为display: inline-block因此每个列表元素可以彼此相邻站立而不是堆叠。

However, I found out that when the first image slides left it still reveals a little out of it while showing the second image, on and on and on like that, everything slides but never fully. 但是,我发现,当第一张图像左移时,在显示第二张图像时,它仍然会露出一点点,这样一来又一来,一切都不会完全滑动。 the distance they slide is equal to the width of the image. 它们滑动的距离等于图像的宽度。

Also, when I get to the last image and click on next it should go back to the first image, the problem is that it displays each image along the way to the first image. 另外,当我到达最后一张图像并单击下一步时,它应该返回到第一张图像,问题在于它会在显示第一张图像的过程中显示每个图像。

 window.onload = function () { var nextBtn = document.getElementsByClassName("nextBtn")[0], prevBtn = document.getElementsByClassName("prevBtn")[0]; var i = 0; var imgList = document.querySelectorAll(".slide"); var slideLength = imgList.length; var lastchild = imgList[slideLength - 1]; nextBtn.addEventListener("click", Next, false); prevBtn.addEventListener("click", Previous, false); function Next() { console.log(slideLength) imgList[i].style.marginLeft = "-600px"; if (imgList[i].classList.contains("active")) { imgList[i].classList.remove("active"); } if (imgList[i] !== lastchild) { i++; } else if (imgList[i] === lastchild) { i = 0; for (var j = 0; j < slideLength; j++) { imgList[j].style.marginLeft = "0"; } } imgList[i].classList.add("active"); } function Previous(e) { console.log(i); if (i !== 0) { imgList[i - 1].style.marginLeft = "0"; i--; console.log(i); } else { console.log(i); } } } 
 ul{ width: 100%; height: 350px; border: solid 3px white; margin:100px auto; overflow: hidden; } li{ display:inline-block; position: relative; transition: all 1s cubic-bezier(1,-0.01, 0, 1.13); list-style: none; } li img{ width:600px; height:350px } .slide h2{ position: absolute; bottom:80px; text-align: center; margin: 0 auto; left: 250px; color: #fff; font-size: 4em; font-weight: 900; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <ul class="slide-wrapper"> <li class="slide active"> <img src="http://placehold.it/350x150" alt=""> <h2>1</h2> </li> <li class="slide"> <img src="http://placehold.it/350x150" alt=""> <h2>2</h2> </li> <li class="slide"> <img src="http://placehold.it/350x150" alt=""> <h2>3</h2> </li> <li class="slide"> <img src="http://placehold.it/350x150" alt=""> <h2>4</h2> </li> <li class="slide"> <img src="http://placehold.it/350x150" alt=""> <h2>5</h2> </li> </ul> </div> <!--<div class="test"> </div>--> <button class="nextBtn">Next</button> <button class="prevBtn">Previous</button> 

How can I make this work well strictly with vanilla javascript? 我如何才能严格使用香草javascript更好地完成这项工作? Here is the link to the jsfiddle . 这是jsfiddle的链接。 However, it doesn't seem to work at all there. 但是,它似乎根本不起作用。

Reference : https://jsfiddle.net/a2bk4bwu/3/ 参考: https : //jsfiddle.net/a2bk4bwu/3/

Replace your CSS with lines with the for ul & li 用ul和li的行替换CSS

ul{
    width: 100%;
    height: 350px;
    border: solid 3px white;
    margin:100px auto;
    overflow: hidden;
    padding-left: 0;
    font-size: 0px;
}
li{
    font-size : 20px;
    display:inline-block;
    position: relative;
    transition: all 1s cubic-bezier(1,-0.01, 0, 1.13);
    list-style: none;
}

Things to note : 注意事项:

  1. 'Ul' was taking extra padding by default {webkit-padding-start: 40px} 默认情况下,“ Ul”进行了额外填充{webkit-padding-start:40px}
  2. "display : inline-block" items are dependent on "white-space" . “ display:inline-block”项取决于“ white-space” So give "0px" to the parent element to remove that white space between slides. 因此,将“ 0px”分配给父元素,以删除幻灯片之间的空白。

These will fix your CSS issues. 这些将解决您的CSS问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM