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

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.

HTML

            
              <!-- example1 - 简单的运动 -->
<div class="example" id="example1">
  <p>缓冲运动</p>
  <div id="div1">
    <span>分 享</span>
  </div>
</div>
            
          
!

CSS

            
              * {
    margin: 0;
    padding: 0;
}
#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;
}
            
          
!

JS

            
              // 加载完页面执行函数
window.onload = function() {
  // 1、首先获取要运动的元素
  var oDiv1 = document.getElementById('div1');
  // 2、添加鼠标移入事件
  oDiv1.onmouseover = function() {
    // this 即元素本身,iTarget 为动画属性的最终值
    startAnimate(this, 0);
  };
  // 3、添加鼠标移出事件
  oDiv1.onmouseout = function() {
    // this 即元素本身,iTarget 为动画属性的最终值
    startAnimate(this, -210);
  };
};

var timer; // 声明定时器
// 3、创建动画函数
function startAnimate(obj, iTarget) {
  // 9、当鼠标再次移入时清除上次的定时器,否则定时器会一直叠加
  clearInterval(timer);
  // 4、设定定时器
  timer = setInterval(function() {
    // 5、通过自定义函数返回元素当前的 left 值,若传回的值为字符串需要转型
    var oleft = parseInt(getStyle(obj, 'left'));
    // 6、避免动画速度过快,我们可以降低变化
    var speed = (iTarget - oleft) * 0.1;
    // 7、因为 speed 减速后不为整数,并且会有精度丢失的情况,因此需要通过 Math 函数来取整动画速度。
    // 当速度大于 0 时向上取整,避免速度取整为 0 造成无运动;反之向下取整,避免速度取整为 0 造成无运动。
    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

    console.log(speed);
    // 8、若完成动画效果清除定时器
    if (iTarget == oleft) {
      clearInterval(timer);
    } else {
      // 7、改变元素的属性值
      obj.style.left = oleft + speed + 'px';
    }
  }, 30);
}

// 传回属性值
function getStyle(obj, attr) {
  if (obj.currentStyle) {
    // IE 浏览器
    return obj.currentStyle[attr];
  } else {
    // Firefox、Chrome
    return getComputedStyle(obj, false)[attr];
  }
}
            
          
!
999px

Console