简体   繁体   English

CSS:数组中元素之间的平滑过渡

[英]CSS: Smooth transition between elements in an array

I have this array:我有这个数组:

myArr = [1, 2, 3, 4]

The first element in this array is displayed on the screen.此数组中的第一个元素显示在屏幕上。

There are two buttons (left and right).有两个按钮(左和右)。 They fire functions that move the elements in the array forward or backward:它们触发将数组中的元素向前或向后移动的函数:

shiftLeft() {
   var firstElement = myArr.shift()
   myArr.push(firstElement)
}

Resulting in: myArr = [2, 3, 4, 1] and now element 2 shows up on the screen.结果: myArr = [2, 3, 4, 1]现在元素2出现在屏幕上。

The unfortunate side affect of this method is that it is difficult to animate the transition for the old element to the new one.这种方法的不幸副作用是很难为旧元素到新元素的过渡设置动画。

I tried to use keyframes but the animation is not as smooth (and I lack experience).我尝试使用keyframes ,但 animation 没有那么流畅(而且我缺乏经验)。

You can find a generic version of my setup here: https://codepen.io/riza-khan/pen/KKVyaaR您可以在此处找到我的设置的通用版本: https://codepen.io/riza-khan/pen/KKVyaaR

Results I am looking for:我正在寻找的结果:

A smooth transition from the first to the second element.从第一个元素到第二个元素的平滑过渡。 Much like any generic carousels from (pick your favorite CSS framework).就像任何通用轮播一样(选择您最喜欢的 CSS 框架)。

Happy to provide additional information should it be required.如果需要,很乐意提供更多信息。

Thank you,谢谢,

You could have an overlay layer that is spawning and removing nodes to trigger the animation, and a setTimer to change the underlay value after the animation completes.您可以有一个覆盖层来生成和删除节点以触发 animation,并使用setTimer在 animation 完成后更改底层值。

Note: Stack Overflow snippets do not currently support scss , instead see my codepen here .注意: Stack Overflow 代码段目前不支持scss ,请参阅我的codepen 此处

 const arr = [1, 2, 3, 4]; const left = document.querySelector(".left"); const right = document.querySelector(".right"); const container = document.querySelector(".container"); const overlay = document.querySelector("#overlay"); let prevEle = document.querySelector(".number"); let disabled = false; left.addEventListener("click", function() { if (disabled) return; movement("left", arr); }); right.addEventListener("click", function() { if (disabled) return; movement("right", arr); }); // document.addEventListener("DOMContentLoaded", function () { // setupUI(); // }); function createNew() { let ele = document.createElement("div"); ele.classList.add("number"); ele.classList.add("fadeIn"); ele.innerText = arr[0]; return ele; } function setupUI() { disabled = true; let newEle = createNew(); overlay.removeChild(overlay.firstChild); overlay.prepend(newEle); setTimeout(() => disabled = false, 350); setTimeout(() => prevEle.innerText = arr[0], 350); } function movement(direction, array) { if (direction === "right") { var element = array.shift(); array.push(element); } else { var element = array.pop(); array.unshift(element); } setupUI(); }
 * { margin: 0; padding: 0; box-sizing: border-box; }.container { height: 100vh; background: rgb(250,175,170); display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 2fr 1fr; .number { grid-column: 1 / -1; cursor: default; }.fadeIn { animation: fadeIn ease.5s; -webkit-animation: fadeIn ease.5s; -moz-animation: fadeIn ease.5s; -o-animation: fadeIn ease.5s; -ms-animation: fadeIn ease.5s; } @keyframes fadeIn { 0% {opacity:0;} 100% {opacity:1;} } }.overlay { position: absolute; width: 100vw; height: 100vh; pointer-events: none; .number { background: rgb(250,175,170); }.container{ background: transparent; } }.number { margin: auto; font-size: 25em; }.btn { margin: auto; border: solid 2px black; padding: 1em 2em; border-radius: 5px; transition: all 0.5s ease; cursor: pointer; &:hover { background: black; color: white; } }
 <div class="overlay"> <div class="overlay container" id="overlay"> </div> </div> <div class="container"> <div class="number">1</div> <button class="btn left">Left</button> <button class="btn right">Right</button> </div>

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

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