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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <div id="box"><span></span></div>
              
            
!

CSS

              
                *{margin:0; padding:0;}
#box{ width:200px; height:200px; position:relative; top:0; left:0; margin:100px auto; background:#ccc; overflow: hidden;}
#box span{width:100%; height:100%; position:absolute; top:0; left:-200px; background:rgba(255,0,0,1);}
              
            
!

JS

              
                 var oBox=document.getElementById('box');
    var oS = oBox.children[0];
    oBox.onmouseover=function(ev){
        var oEvent = ev||event;
        var oForm = oEvent.formElement||oEvent.relatedTarget;
        if(this.contains(oForm))return;   //判断如果当前触发事件的元素不是box本身则忽略
        var dir = direction(this,oEvent);
       
        oS.style.transition='0s all ease';
        switch(dir){
            case 2:
                //右
                oS.style.left = '200px';
                oS.style.top = 0;
                break;
            case 1:
                //下
                oS.style.left = 0;
                oS.style.top = '200px';
                break;
            case 0:
                //左
                oS.style.left = '-200px';
                oS.style.top = 0;
                break;
            case 3:
                //上
                oS.style.left = 0;
                oS.style.top = '-200px';
                break;
        }
        getTo({top:0,left:0});
    };
    oBox.onmouseout=function(ev){
        
        var oEvent = ev||event;
        var oTo = oEvent.toElement||oEvent.relatedTarget;
        if(this.contains(oTo))return;
        var dir = direction(this,oEvent);
        
        switch(dir){
            case 2:
                //右
                getTo({left:200,top:0});
                break;
            case 1:
                //下
                getTo({left:0,top:200});
                break;
            case 0:
                //左
                getTo({left:-200,top:0});
                break;
            case 3:
                //上
                getTo({left:0,top:-200});
                break;
        }
    };

    function a2d(n){
        return n*180/Math.PI;
    };                                             
    function direction(obj,ev){
        var w = obj.offsetWidth;
        var h = obj.offsetHeight;
        var x = ev.clientX-(obj.offsetLeft+w/2);
        var y = (obj.offsetTop+h/2)-ev.clientY;
        return Math.round((a2d(Math.atan2(y,x))+180)/90)%4;

        /*       
        Math.atan2
        atan2() 方法可返回从 x 轴到点 (x,y) 之间的角度。弧度
        语法  Math.atan2(y,x)

        弧度*180/Math.PI  = 角度
        角度*Math.PI/180 = 弧度

        Math.atan2(y,x)   得到0-180    0-负180
        所以先加上180得到正的  
        Math.atan2(y,x) +180

        之前是  -180-180之间的值    都加上180以后
        能得到  0-360 之间的值

        每个值之间相隔90   所  用值/90     可能除不尽所以四舍五入    再最后%4  因为分成4份
        */

    };
    var timer;
    function getTo(o){
       clearTimeout(timer);
       timer=setTimeout(function(){
            oS.style.transition='0.4s all ease';
            oS.style.left=o.left+'px';
            oS.style.top=o.top+'px';
       },100);
       /*
       因为这里动画偷懒没有引入其它的动画库,所以为了避免一些问题,用了setTimeout
       */
    };
              
            
!
999px

Console