[英]How do I temporarily disable a CSS transition for a carousel?
I have an HTML/CSS/JS carousel with the following basic markup:我有一个带有以下基本标记的 HTML/CSS/JS 轮播:
<div class="carousel">
<ul>
<li><img src="img1.png"></li>
<li><img src="img2.png"></li>
<li><img src="img3.png"></li>
<li><img src="img1.png"></li>
</ul>
</div>
And the following basic CSS:以及以下基本 CSS:
.carousel {
overflow: hidden;
white-space: nowrap;
ul {
left: 0;
position: relative;
transition: left 1s;
li {
display: inline-block;
img {
width: 100%;
}
}
}
}
I have the JS set up so that every 3 seconds, I change the left
property of the ul
tag by adding a negative amount of pixels to it equal to the width of the carousel so that it creates the carousel transition effect of sliding from one image to the next.我设置了 JS,以便每 3 秒更改一次ul
标签的left
属性,方法是向其添加等于轮播宽度的负像素数,以便创建从一张图像滑动的轮播过渡效果到下一个。
The problem I'm having though is related to what happens when the carousel reaches the end.我遇到的问题与轮播到达终点时发生的情况有关。 I added a second copy of img1.png
on the end, and when it gets there, I want to set the left
offset back to 0
so that it'll start all over again and loop forever.我在最后添加了img1.png
的第二个副本,当它到达那里时,我想将left
偏移设置回0
以便它会重新开始并永远循环。
The issue specifically is that when the left
property is, for example, -1000px
and I change it to 0
, the carousel suddenly animates all the way back to the first image.具体问题是,例如,当left
属性为-1000px
并且我将其更改为0
,轮播突然动画回到第一张图像。 I want to set the left
property to 0
without the transition animation taking place.我想在不发生过渡动画的情况下将left
属性设置为0
。
I thought that if I temporary set the transition
property to none
, then set left
to 0
, and then set the transition
back to left 1s
, then it would work, but it doesn't.我想如果我暂时将transition
属性设置为none
,然后将left
设置为0
,然后将transition
设置回left 1s
,那么它会起作用,但它不会。 It still animates from the end all the way back to the beginning when I set left
to 0
.当我将left
设置为0
时,它仍然从结尾一直动画到开头。
How can I achieve the effect I want with CSS transitions and the left
property, or is the approach I'm taking fundamentally wrong and need to be completely rethought?我怎样才能用 CSS 过渡和left
属性达到我想要的效果,或者我采取的方法从根本上是错误的,需要完全重新思考? Thank you.谢谢你。
duhaime gave a great suggestion to just have two carousel images at a time and always be swapping out the image paths. duhaime 提出了一个很好的建议,即一次只有两个轮播图像,并且始终交换图像路径。
However, I was still having an issue with the transition
property animating things when I set left: 0
to go back to the start.但是,当我设置left: 0
回到开始时,我仍然遇到transition
属性动画的问题。 In essence, this is originally what I had and hoped would work:从本质上讲,这最初是我所拥有的,并希望能奏效:
// ulElem is equal to the ul element with the following CSS:
// ul {
// left: 0;
// position: relative;
// transition: left 0.5s;
// }
ulElem.style.transition = 'none';
ulElem.style.left = '0';
ulElem.style.transition = 'left 0.5s';
However, that was creating the problem I was seeing.但是,这造成了我所看到的问题。 Since I'm using Vue.js, I thought that putting some of the code within a this.$nextTick
call might do the trick.由于我使用的是 Vue.js,我认为将一些代码放在this.$nextTick
调用中可能会this.$nextTick
。 So I changed it to the following:所以我将其更改为以下内容:
ulElem.style.transition = 'none';
ulElem.style.left = '0';
this.$nextTick(() => {
ulElem.style.transition = 'left 0.5s';
});
However, that still didn't work.然而,这仍然没有奏效。 However, the following did:但是,以下做了:
ulElem.style.transition = 'none';
ulElem.style.left = '0';
setTimeout(() => {
ulElem.style.transition = 'left 0.5s';
}, 0);
I've read stuff before about setting a setTimeout
time of 0
ms and how that places it on the end of the call stack, which can help.我之前阅读过有关设置0
毫秒的setTimeout
时间以及如何将其置于调用堆栈末尾的内容,这会有所帮助。 Seems weird to me, but it works.对我来说似乎很奇怪,但它有效。
For anyone else that has a similar problem, this may be your solution.对于其他有类似问题的人,这可能是您的解决方案。 I'm not entirely sure why $nextTick
didn't work, but I'm not familiar enough with the inner-workings of Vue.js to comment.我不完全确定为什么$nextTick
不起作用,但我对 Vue.js 的内部工作不够熟悉,无法发表评论。 All the same, setTimeout
with a 0
ms setting worked.同样,设置为0
ms 的setTimeout
有效。 Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.