简体   繁体   English

如何反转 javascript 图片轮播的方向?

[英]How do I reverse the direction of my javascript image carousel?

I have an image carousel that by default rotates to the right.我有一个图像轮播,默认情况下向右旋转。 using setInterval() and changing the image every few seconds.使用 setInterval() 并每隔几秒更改一次图像。 I created two setInterval functions, one to rotate to the right and one to rotate to the left.我创建了两个 setInterval 函数,一个向右旋转,一个向左旋转。

I then created an html arrow and added an onclick call to it to call the specific setInterval I wanted.然后我创建了一个 html 箭头并添加了一个 onclick 调用以调用我想要的特定 setInterval。 The problem is the previous setIntervals dont turn off when new ones are called.问题是以前的 setIntervals 在调用新的 setIntervals 时不会关闭。 I tried to use clearInterval but it wasnt working.我尝试使用 clearInterval,但没有用。

My code looks like:我的代码看起来像:

const left_arrow = document.querySelector("#arrow");

  var rotateRight = setInterval(()=>{
   // do stuff
}, 3000)
 var rotateLeft = setInterval(()=>{
   // do stuff
}, 3000)

left_arrow.onclick = rotateLeft();
right_arrow.onclick = rotateRight();

I basically want to have the ability to click an arrow and have it rotate the flow of my images by calling different setInterval functions.我基本上希望能够单击箭头并通过调用不同的 setInterval 函数让它旋转我的图像流。 I cant use any frameworks.我不能使用任何框架。 Thank you.谢谢你。

SetInterval only executes an action every x milliseconds , so with your code you are going left and then right every three seconds. SetInterval 仅每 x 毫秒执行一次操作,因此对于您的代码,您每三秒向左然后向右移动。 Here is my code that I think would work这是我认为可行的代码

direction = "right"

setInterval(()=>{
   if(direction === "right") {
      //do stuff to rotate right
   } else {
      //do stuff to rotate left
   }
}, 3000)

left_arrow.onclick = () => {
   direction = "left";
};
right_arrow.onclick = () => {
   direction = "right";
};

So explaining what the code is doing the variable direction stores which direction the carousel should move, the code inside setInterval runs every 3 seconds, if the direction is right you run some code, otherwise you run other code.因此,解释代码在做什么,变量 direction 存储轮播应该移动的方向,setInterval 中的代码每 3 秒运行一次,如果方向正确,则运行一些代码,否则运行其他代码。 When you click the buttons it just sets the directions.当您单击按钮时,它只是设置方向。

I think clearInterval will work on your scenary, may be you clearInterval on wrong time.我认为 clearInterval 会在您的场景中起作用,可能是您在错误的时间使用了 clearInterval。 if you're not sure,please paste more code.如果您不确定,请粘贴更多代码。

secondly, const left_arrow = document.querySelector("#arrow");其次, const left_arrow = document.querySelector("#arrow"); #arrow element are named left_arrow, i don't know what is your element id for right_arrow, may be that's also a problem. #arrow 元素被命名为 left_arrow,我不知道你的 right_arrow 元素 id 是什么,可能这也是一个问题。

As @Dominik comments, the solution to stop the original interval is to use clearInterval正如@Dominik 评论的那样,停止原始间隔的解决方案是使用clearInterval

when you call setInterval it当你调用setInterval

const left_arrow = document.querySelector("#arrow");

let currentInterval = null;

function rotateRight(){
    return setInterval(()=>{ 
       clearInterval(currentInterval)
       // do stuff
    }, 3000)
}

function rotateLeft(){
    return setInterval(()=>{ 
       clearInterval(currentInterval)
       // do stuff
    }, 3000)
}

left_arrow.onclick = ()=> {currentInterval = rotateLeft();}
right_arrow.onclick = ()=> {currentInterval = rotateRight();}

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

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