Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import or require. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-- example1 - 匀速运动 -->
<div class="example" id="example1">
  <p>匀速运动</p>
  <div id="div1">
    <span>分 享</span>
  </div>
</div>

<!-- example2 缓冲运动 -->
<div class="example" id="example2">
  <p>缓冲运动</p>
  <div id="div2">
    <span>分 享</span>
  </div>
</div>

<!-- example3 多物体运动 -->
<div class="example" id="example3">
  <p>多物体运动</p>
  <div id="div3">
    <ul>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
</div>

<!-- example4 任意值变化 -->
<div class="example" id="example4">
  <p>任意值变化</p>
  <div id="div4">
    <ul>
      <li>高</li>
      <li>宽</li>
      <li>透明度</li>
    </ul>
  </div>
</div>

<!-- example5 链式运动 -->
<div class="example" id="example5">
  <p>链式运动:高->宽->透明度</p>
  <div id="div5">
  </div>
</div>

<!-- example6 多物体同时运动 -->
<div class="example" id="example6">
  <p>多物体同时运动</p>
  <div id="div6">
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </div>
</div>

<!-- example7 实际案例 -->
<div class="example" id="example7">
  <p>模仿案例</p>
  <div id="div7">
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>话费</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>旅游</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>电影</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>娱乐</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>彩票</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>加油</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>酒店</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>火车</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>金钱</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>白条</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>借贷</p>
    </a>
    <a href="#" onclick="return false;"><img src="http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/bg.png">
      <p>旅游</p>
    </a>
  </div>
</div>
            
          
!
            
              * {
  margin: 0;
  padding: 0;
}

.example {
  border-bottom: 1px solid gray;
  width: 100%;
}


/* div1 - 匀速运动 */

#div1 {
  background-color: red;
  width: 200px;
  height: 200px;
  position: relative;
  left: -210px;
  border: 10px solid black;
}

#div1 span {
  background-color: blue;
  position: absolute;
  left: 200px;
  top: 50%;
  width: 15px;
  font-size: 15px;
  padding: 5px 2px;
}


/* div2 - 缓冲运动 */

#div2 {
  background-color: red;
  width: 200px;
  height: 200px;
  position: relative;
  left: -210px;
  border: 10px solid black;
}

#div2 span {
  background-color: blue;
  position: absolute;
  left: 200px;
  top: 50%;
  width: 15px;
  font-size: 15px;
  padding: 5px 2px;
}

#div6 li,
#div4 li,
#div3 li {
  border: 2px solid black;
  list-style: none;
  position: relative;
  background-color: yellow;
  width: 150px;
  height: 50px;
  margin-bottom: 10px;
  opacity: 1;
}

#div5 {
  background-color: red;
  width: 50px;
  height: 50px;
  position: relative;
  border: 5px solid black;
}

#div7 {
  background-color: #eee;
  width: 210px;
  overflow: hidden;
  padding-bottom: 10px;
  padding-left: 10px;
}

#div7 a {
  float: left;
  margin: 10px 10px 0 0;
  overflow: hidden;
  display: block;
  text-decoration: none;
  background-color: #fff;
  color: gray;
  width: 60px;
  text-align: center;
  height: 60px;
}

#div7 a:hover {
  color: orange;
}

#div7 img {
  position: relative;
  margin: 5px;
  width: 25px;
  height: 26px;
  background-image: url("http://7xpjb2.com1.z0.glb.clouddn.com/image/jsAnimateTest/jingdong.png");
  background-repeat: no-repeat;
}
            
          
!
            
              var oDiv1 = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
var oDiv3 = document.getElementById('div3');
var oLiList3 = oDiv3.getElementsByTagName('li');
var oDiv4 = document.getElementById('div4');
var oLiList4 = oDiv4.getElementsByTagName('li');
var oDiv5 = document.getElementById('div5');
var oDiv6 = document.getElementById('div6');
var oLiList6 = oDiv6.getElementsByTagName('li');
var oDiv7 = document.getElementById('div7');
var imgList7 = oDiv7.getElementsByTagName('img');

window.onload = function() {
  oDiv1.onmouseover = function() {
    startAnimate(this, {
      left: 0
    }, null, false);
  }
  oDiv1.onmouseout = function() {
    startAnimate(this, {
      left: -210
    }, null, false);
  }
  oDiv2.onmouseover = function() {
    startAnimate(this, {
      left: 0
    });
  }
  oDiv2.onmouseout = function() {
    startAnimate(this, {
      left: -210
    });
  }
  for (var i = 0; i < oLiList3.length; i++) {
    oLiList3[i].onmouseover = function() {
      startAnimate(this, {
        width: 400
      });
    }
    oLiList3[i].onmouseout = function() {
      startAnimate(this, {
        width: 150
      });
    }
  }

  oLiList4[0].onmouseover = function() {
    startAnimate(this, {
      height: 200
    });
  }
  oLiList4[0].onmouseout = function() {
    startAnimate(this, {
      height: 50
    });
  }
  oLiList4[1].onmouseover = function() {
    startAnimate(this, {
      width: 400
    });
  }
  oLiList4[1].onmouseout = function() {
    startAnimate(this, {
      width: 150
    });
  }
  oLiList4[2].onmouseover = function() {
    startAnimate(this, {
      opacity: 30
    });
  }
  oLiList4[2].onmouseout = function() {
    startAnimate(this, {
      opacity: 100
    }, false);
  }
  oDiv5.onmouseover = function() {
    var mThis = this;
    startAnimate(mThis, {
      height: 100
    }, function() {
      startAnimate(oDiv5, {
        width: 150
      }, function() {
        startAnimate(mThis, {
          opacity: 30
        });
      });
    });
  }
  oDiv5.onmouseout = function() {
    var mThis = this;
    startAnimate(mThis, {
      opacity: 100
    }, function() {
      startAnimate(mThis, {
        width: 50
      }, function() {
        startAnimate(mThis, {
          height: 50
        });
      }, false);
    }, false);
  }

  var json1 = {
    width: 500,
    height: 100,
    opacity: 30
  };
  var json1_r = {
    opacity: 30,
    height: 100,
    width: 500
  };
  for (var i = 0; i < oLiList6.length; i++) {
    oLiList6[i].onmouseover = function() {
      var myThis = this;
      startAnimate(myThis, json1, function() {
        startAnimate(myThis, {
          width: 300,
          height: 200,
          opacity: 50
        });
      });
    }
    oLiList6[i].onmouseout = function() {
      var myThis = this;
      startAnimate(myThis, json1_r, function() {
        startAnimate(myThis, {
          opacity: 100,
          width: 150,
          height: 50
        });
      });
    }
  }

  /* div7 */
  for (var i = 0; i < imgList7.length; i++) {
    imgList7[i].style.backgroundPosition = '0 ' + -25 * i + 'px';
    imgList7[i].onmouseover = function() {
      var myThis = this;
      startAnimate(myThis, {
        top: -20,
        opacity: 0
      }, function() {
        myThis.style.top = 20 + 'px';
        startAnimate(myThis, {
          top: 0,
          opacity: 100
        });
      });
    }
  }
}
var timer, speed, alpha;
/*
obj 目标
json {属性:值}
fn 执行链式运动
change 目标是否变速运动,要注意速度值的设定,目前还未能完全适配
*/
function startAnimate(obj, json, fn, change) {
  // 每次执行都会删除旧定时器
  clearInterval(obj.timer);
  // 简单的深拷贝一份 json 数据
  var jsonTmp = JSON.parse(JSON.stringify(json));
  // 每次开始动画都会创建一个新的定时器
  obj.timer = setInterval(function() {
    for (var attr in json) {
      // icur 当前属性值
      var icur;
      if (attr == 'opacity') {
        // 此处未考虑 IE9 之前的兼容
        icur = getStyle(obj, attr) * 100;
      } else {
        icur = parseInt(getStyle(obj, attr));
      };
      // 判断是否实现缓冲变化
      if (change == false) {
        speed = icur <= json[attr] ? 10 : -10;
      } else {
        speed = (json[attr] - icur) * 0.2;
        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
      }
      if (icur == jsonTmp[attr]) {
        delete jsonTmp[attr];
        var jsonLenght = 0;
        for (var attr in jsonTmp) {
          jsonLenght++;
        }
        if (jsonLenght == 0) {
          clearInterval(obj.timer);
          if (fn) fn();
        }
      } else {
        if (attr == 'opacity') {
          obj.style.opacity = (icur + speed) / 100;
        } else {
          obj.style[attr] = icur + speed + 'px';
        }
      }
    }
  }, 30);
}

function getStyle(obj, attr) {
  if (obj.currentStyle) {
    return obj.currentStyle[attr];
  } else {
    return getComputedStyle(obj, false)[attr];
  }
}
            
          
!
999px

Console