简体   繁体   English

如何在香草 javascript 中制作我的图像旋转循环?

[英]How can i make my image carousal loop in vanilla javascript?

I have created a simple image carousal but I would like it to go back to the start when the final slide item is reached so it appears to have a loop我创建了一个简单的图像轮播,但我希望 go 在到达最终幻灯片项目时回到开始,因此它似乎有一个循环

My HTML:我的 HTML:

<div class="slider" id="slider">

        <img class="slide" src="/assets/image-slide-1.jpg" alt="">
        <img class="slide" src="/assets/image-slide-2.jpg" alt="">
        <img class="slide" src="/assets/image-slide-3.jpg" alt="">
        <img class="slide" src="/assets/image-slide-4.jpg" alt="">
        <img class="slide" src="/assets/image-slide-5.jpg" alt="">
    </div>
    <div class="buttons">
        <button id="prev">
            <img src="/assets/icon-arrow-left.svg" alt="">
        </button>
        <button id="next">
            <img src="/assets/icon-arrow-right.svg" alt="">
        </button>

My JavaScript:我的 JavaScript:

const slidesContainer = document.getElementById("slider");
const slide = document.querySelector(".slide");
const prevButton = document.getElementById("prev");
const nextButton = document.getElementById("next");



nextButton.addEventListener("click", (event) => {
    const slideWidth = slide.clientWidth;
      slidesContainer.scrollLeft += slideWidth;

  });

  prevButton.addEventListener("click", () => {
    const slideWidth = slide.clientWidth;
    slidesContainer.scrollLeft -= slideWidth;
  });

If width (height, if slider is vertical) of slider is as long as, say, two images, place copies of last two images to the beginning of slider and whenever user reaches the end of slider move it to the very beginning If width (height, if slider is vertical) of slider is as long as, say, two images, place copies of last two images to the beginning of slider and whenever user reaches the end of slider move it to the very beginning

For the nextButton event handler function you could add a conditional:对于nextButton事件处理程序 function 您可以添加一个条件:

if (slidesContainer.scrollLeft >= slideWidth * 4) {
    slidesContainer.scrollLeft = 0;
}

and for the prevButton you would do the opposite:而对于prevButton你会做相反的事情:

if (slidesContainer.scrollLeft <= 0) {
    slidesContainer.scrollLeft = scrollWidth * 4;
}

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

相关问题 如何使我的脚本循环并检查所有处于活动状态的按钮? 香草js - How can I make my script loop and check all buttons that are active? vanilla js 验证失败时如何使我的输入边框颜色变为红色? (仅限普通 JavaScript) - How can I make my input border colors red when they fail validation? (Vanilla JavaScript only) VSCode:如何使Intellisense与普通的Javascript项目一起工作? - VSCode: How can I make Intellisense work with a vanilla Javascript project? 如何用香草制作无限旋转木马? - How can I make Infinite Carusel with Vanilla? 如何将此 JQuery 转换为香草 Javascript? - How can I convert this JQuery into vanilla Javascript? 如何在Vanilla JavaScript中执行$(&#39;。div a&#39;)? - How Can I Perform $('.div a') in Vanilla JavaScript? 如何使我的SlickJS Carousal在3张图像之间循环,并在循环时将它们全部可见? - How can I get my SlickJS Carousal to cycle between 3 images and have them all visible while cycling? 如何使用 vanilla javascript 使表单更新主表单上的文本? - How can I make a form update the text on the main form using vanilla javascript? 我如何在 Vanilla JavaScript class 中使用变量来制作基于 Z84E2C64F55AB78BA3ZEA5C2 的 function - How can i use variable inside Vanilla JavaScript class to make a function based on boolean 如何使此函数与javascript循环? - How can I make this function loop with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM