loading加载动画效果js实现

<style>
.box { width: 400px; padding: 20px; border: 40px solid #a0b3d6; background-color: #eee; overflow: hidden; }
.loading { height: 100%; background: url(images/loading.gif) no-repeat center; }
.in { width: 100px; margin: 0 auto; border: 50px solid #beceeb; background-color: #f0f3f9; }
[type=button] { width: 100px; height: 32px; font-size: 100%; }
</style>
</head>

<body>
  <div id="main">
    <div id="effect" class="part">
      <div class="show">
        <div class="demo">
          <div id="box" class="box">
          </div>
          <p>
            <input type="button" id="button" value="点击我">
          </p>
        </div>
      </div>
    </div>
   </div>
</div>

<script>
// 高度无缝动画方法
var funTransitionHeight = function(element, time) { // time, 数值,可缺省
if (typeof window.getComputedStyle == "undefined") return;
var height = window.getComputedStyle(element).height;
element.style.transition = "none"; // 本行2015-05-20新增,mac Safari下,貌似auto也会触发transition, 故要none下~
element.style.height = "auto";
var targetHeight = window.getComputedStyle(element).height;
element.style.height = height;
element.offsetWidth = element.offsetWidth;
if (time) element.style.transition = "height "+ time +"ms";
element.style.height = targetHeight;
};

(function() {
// demo演示脚本
var box = document.getElementById("box"), button = document.getElementById("button");
button.onclick = function() {
// 载入菊花,模拟loading效果
box.innerHTML = '<div class="loading"></div>';
// 随机高度内容
setTimeout(function() {
  box.innerHTML = '<div class="in" style="height:'+ Math.round(400 * Math.random()) +'px;"></div>';
  funTransitionHeight(box, 250);
  }, 1000);
};

// 初始高度
funTransitionHeight(box);
})();
</script>

</body>

posted @ 2017-02-28 15:45  FEDeveloper  阅读(3733)  评论(0编辑  收藏  举报