简体   繁体   English

javascript 中的图像滑块代码不起作用

[英]imageslider code in javascript is not working

i have done this code of imageslider in javascript that when click on an image its slides to show the next one, but the problem is i want this code to run with out global variables (only closure) but it is not working with me, any help?我已经在 javascript 中完成了此图像滑块代码,当单击图像时,其幻灯片将显示下一个,但问题是我希望此代码在没有全局变量的情况下运行(仅关闭)但它不适用于我,任何帮助? ( ps: i tried this code with closure but it doesn't work with me ) (ps:我用闭包尝试了这段代码,但它不适用于我)

 var liEls = document.querySelectorAll('ul li'); var index = 0; var max = liEls.length - 1; window.show = function(increase) { if (index == max) { index = -1; } index = index + increase; liEls[index].scrollIntoView({ behavior: 'smooth' }); // console.log(index); }
 body { background-color: black; } h1 { text-align: center; color: pink; } ul { display: flex; width: 400px; margin-left: 450px; padding-top: 30px; position: relative; z-index: 1; overflow: hidden; } li { position: relative; display: block; list-style: none; }
 <h1>Image Slide Show</h1> <ul> <li><img class="img2" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> <li><img class="img3" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> <li><img class="img4" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> <li><img class="img2" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> <li><img class="img3" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> <li><img class="img4" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" onclick="show(+1)"></li> </ul>

This is how you can do it without setting global variables.这就是你可以在不设置全局变量的情况下做到这一点的方法。 Only thing you have to do is to define which elements you want to click on, but that can also be put into a wrapper function if you'd like.您只需定义要单击的元素,但如果您愿意,也可以将其放入包装器 function 中。 Here is a good example on how you could do it.这是一个很好的例子,说明如何做到这一点。 Even tough it's jQuery page, the first example doesn't use jQuery, it just shows you how you can wrap your code in your own function.更难的是 jQuery 页面,第一个示例没有使用 jQuery,它只是向您展示了如何将代码包装在您自己的 function 中。

You would then have something like myCarousel.init() on page load.然后你会在页面加载时有类似myCarousel.init()的东西。

 function scrollToNextElement(nextLI) { nextLI.scrollIntoView({ behavior: 'smooth' }); } function scrollToFirstElement() { document.querySelector('.slideshow li').scrollIntoView({ behavior: 'smooth' }); } var lis = document.querySelectorAll('.slideshow li'); lis.forEach( li => { li.addEventListener('click', () => { li.nextElementSibling? scrollToNextElement(li.nextElementSibling): scrollToFirstElement(); }); });
 body { background-color: black; } h1 { text-align: center; color: pink; } ul { display: flex; width: 400px; margin: 0 auto; padding-top: 30px; position: relative; z-index: 1; overflow: hidden; } li { position: relative; display: block; list-style: none; }
 <h1>Image Slide Show</h1> <ul class="slideshow"> <li><img class="img2" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> <li><img class="img3" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> <li><img class="img4" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> <li><img class="img2" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> <li><img class="img3" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> <li><img class="img4" src="https://image.shutterstock.com/image-photo/beautiful-luxury-home-exterior-twilight-600w-639916900.jpg" ></li> </ul>

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

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