为了寻找HTML5转换的一些例子,我调整了我发现的东西,最终得到了我想要的东西。
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 50px;
height: 50px;
}
div.myDiv {
position: absolute;
-webkit-transition-property: top, opacity;
-webkit-transition-duration: 1s, 1s;
-webkit-transition-timing-function:ease-in-out, ease-in-out;
-moz-transition-property: top, opacity;
-moz-transition-duration: 1s, 1s;
-moz-transition-timing-function:ease-in-out, ease-in-out;
-o-transition-property: top, opacity;
-o-transition-duration: 1s, 1s;
-o-transition-timing-function:ease-in-out, ease-in-out;
transition-property: top, opacity;
transition-duration: 1s, 1s;
transition-timing-function:ease-in-out, ease-in-out;
}
#one {
top: 0px;
background-color: blue;
opacity: 1;
}
#two {
top: 50px;
background-color: green;
opacity: 0;
}
#three {
top: 100px;
background-color: red;
opacity: 0;
}
#container {
width: 50px;
height: 150px;
position: relative;
overflow:hidden;
}
</style>
<script>
function one() {
document.getElementById('one').style.top = "0px";
document.getElementById('two').style.top = "50px";
document.getElementById('three').style.top = "100px";
document.getElementById('one').style.opacity = "1";
document.getElementById('two').style.opacity = "0";
document.getElementById('three').style.opacity = "0";
}
function two() {
document.getElementById('one').style.top = "-50px";
document.getElementById('two').style.top = "0px";
document.getElementById('three').style.top = "50px";
document.getElementById('one').style.opacity = "0";
document.getElementById('two').style.opacity = "1";
document.getElementById('three').style.opacity = "0";
}
function three() {
document.getElementById('one').style.top = "-100px";
document.getElementById('two').style.top = "-50px";
document.getElementById('three').style.top = "0px";
document.getElementById('one').style.opacity = "0";
document.getElementById('two').style.opacity = "0";
document.getElementById('three').style.opacity = "1";
}
</script>
</head>
<body>
<div id='container'>
<div class="myDiv" id='one'>
</div>
<div class="myDiv" id='two'>
</div>
<div class="myDiv" id='three'>
</div>
</div>
<input type='button' onclick='one();' value='one!'></input>
<input type='button' onclick='two();' value='two!'></input>
<input type='button' onclick='three();' value='three!'></input>
</body>
</html>
我在某处读到不建议从 javascript 更改 css 属性。我认为这是一种不好的做法。我相信一定有一种更优雅、更好的方法来做到这一点。
知道吗?(在这里小提琴)
我不确定在 JavaScript 中更改 CSS 属性是否一定是不好的做法,尽管如果您的用户有可能关闭 JavaScript,您可能不想以这种方式更改对网站运行至关重要的属性。
你的 JavaScript 可以按如下方式重构,其明显优势是添加或删除框变得微不足道(请参阅此处的小提琴):
// We can define any number of boxes here
var boxes = ['blue', 'green', 'red'];
var height = 50;
// We'll put the boxes in a div which we will move
// so that we don't have to manage the movement of
// multiple elements
var slider = document.getElementById('slider');
var div;
for (var i = 0; i < boxes.length; i ++) {
// Create a box for each element in array
div = document.createElement('div');
div.setAttribute('id', 'box_' + i);
div.setAttribute('class', 'myDiv');
div.style.background = boxes[i];
div.style.top = (i * height) + "px";
div.style.opacity = (i == 0) ? 1 : 0;
// Now stick it in the slider div
slider.appendChild(div);
}
function slide(index) {
// Calculate new slider position according to supplied index
var top = (index * height) * -1;
document.getElementById('slider').style.top = top + 'px';
toggleVisible(document.getElementById('box_' + index));
}
// Hide non-selected boxes and reveal selected
function toggleVisible(show) {
var div;
for (var i = 0; i < boxes.length; i ++) {
div = document.getElementById('box_' + i);
div.style.opacity = (div === show) ? 1 : 0;
}
}
.HTML:
<div id='container'>
<div id='slider'></div>
</div>
<input type='button' onclick='slide(0);' value='one!'></input>
<input type='button' onclick='slide(1);' value='two!'></input>
<input type='button' onclick='slide(2);' value='three!'></input>
.CSS:
div {
width: 50px;
height: 50px;
}
div.myDiv {
position: absolute;
-webkit-transition-property: opacity;
-webkit-transition-duration: 1s, 1s;
-webkit-transition-timing-function:ease-in-out, ease-in-out;
-moz-transition-property: opacity;
-moz-transition-duration: 1s, 1s;
-moz-transition-timing-function:ease-in-out, ease-in-out;
-o-transition-property: opacity;
-o-transition-duration: 1s, 1s;
-o-transition-timing-function:ease-in-out, ease-in-out;
transition-property: opacity;
transition-duration: 1s, 1s;
transition-timing-function:ease-in-out, ease-in-out;
}
#container {
width: 50px;
height: 150px;
position: relative;
overflow:hidden;
}
#slider {
position: absolute;
-webkit-transition-property: top;
-webkit-transition-duration: 1s, 1s;
-webkit-transition-timing-function:ease-in-out, ease-in-out;
-moz-transition-property: top;
-moz-transition-duration: 1s, 1s;
-moz-transition-timing-function:ease-in-out, ease-in-out;
-o-transition-property: top;
-o-transition-duration: 1s, 1s;
-o-transition-timing-function:ease-in-out, ease-in-out;
transition-property: top;
transition-duration: 1s, 1s;
transition-timing-function:ease-in-out, ease-in-out;
}