var theArray = ["1","2","3","4","5"];
var dancefloor = document.getElementById("dancefloor");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var current = 0;
// When the transition is done, we want to remove the first
// child, remove the roll class and re-eneable our buttons
dancefloor.addEventListener('transitionend', function(e) {
dancefloor.removeChild(dancefloor.children[0]);
prev.disabled = false;
next.disabled = false;
dancefloor.classList.remove('roll');
}, false);
function justDance(x) {
var el = document.createElement('p');
el.innerText = theArray[x];
dancefloor.appendChild(el);
// When we have more than one child, we want to "roll"
// Simply add the classname and let the CSS do the work.
// We also want to disable our buttons until the
// animation is done.
if (dancefloor.children.length === 2) {
prev.disabled = true;
next.disabled = true;
setTimeout(function() {
dancefloor.classList.add('roll');
}, 0);
}
}
function nextNum() {
current++;
if (current === theArray.length) {
current = 0;
}
justDance(current);
}
function prevNum() {
current--;
if (current < 0) {
current = theArray.length - 1;
}
justDance(current);
}
justDance(current);
body {background:#F0F0F0; text-align:center;}
#dancefloor {
position: relative;
width: 50px;
height: 30px;
overflow: hidden;
/* Style Fluff */
border: 2px solid #444;
background: #FFF;
background-image: -webkit-linear-gradient(top, rgba(0,0,0,0.4) 0%, rgba(0,0,0,0.25) 10%, rgba(0,0,0,0.001) 25%, rgba(0,0,0,0.001) 75%, rgba(0,0,0,0.25) 90%, rgba(0,0,0,0.4) 100%);
box-shadow: 0px 1px 3px rgba(0,0,0,0.4);
margin: 10px auto;
}
#dancefloor > * {
display: block;
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
text-align: center;
font-size: 28px;
font-weight: bold;
line-height: 1;
transform: translateY(0px);
-webkit-transform: translateY(0px);
}
#dancefloor.roll > * {
transition: all 0.2s ease-in-out;
-webkit-transition: all 0.2s ease-in-out;
transform: translateY(-30px);
-webkit-transform: translateY(-30px);
}
<div id="dancefloor"></div>
<button id="prev" onclick="prevNum()">Previous</button>
<button id="next" onclick="nextNum()">Next</button>
过渡什么你想补充的吗?你有没有考虑过jQuery动画?顺便说一句,这里没有转换......你只是覆盖了一个div的内容。转换通常涉及可以数学表示的东西(如从50px到75px的高度)。 – 2015-02-24 03:47:21
是的,这是问题,我只是覆盖div的内容。我正在寻求一种纯粹的JS解决方案。感谢您的澄清。 – bruh 2015-02-24 05:54:24