【问题标题】:HTML5 transitionsHTML5 过渡
【发布时间】:2012-12-04 09:06:14
【问题描述】:

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

有什么想法吗? (小提琴here)

【问题讨论】:

  • 过渡是 CSS,而不是 HTML5。

标签: javascript html css css-transitions


【解决方案1】:

我不确定在 JavaScript 中更改 CSS 属性是否一定是不好的做法,但也许如果您的用户可能会关闭 JavaScript,您可能不想更改对 JavaScript 运行至关重要的属性网站那样。

您的 JavaScript 可以按如下方式重构,其明显优势是可以轻松添加或删除框(参见小提琴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;
}

【讨论】:

  • 重点是 div 必须包含内容(文本、图像...)。我只是把盒子作为例子。无论如何,我认为我可以对其进行修改,使其适合我的需要。谢谢!
猜你喜欢
  • 2010-09-20
  • 2015-03-24
  • 1970-01-01
  • 2012-04-12
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
相关资源
最近更新 更多