gpt4 book ai didi

javascript - HTML5 过渡

转载 作者:行者123 更新时间:2023-11-28 13:29:54 25 4
gpt4 key购买 nike

在寻找 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 属性。我认为这是一种不好的做法。我相信一定有更优雅、更好的方法来做到这一点。

有什么想法吗? ( fiddle here)

最佳答案

我不确定在 JavaScript 中更改 CSS 属性是否一定是不好的做法,尽管如果您的用户有可能关闭 JavaScript,您可能不想更改对 CSS 功能至关重要的属性网站那样。

您的 JavaScript 可以按如下方式重构,其明显的优点是添加或删除框变得微不足道(参见 fiddle here ):

  // 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;
}

关于javascript - HTML5 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13689887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com