1. 程式人生 > >Js事件案例——上下左右鍵盤控制div移動

Js事件案例——上下左右鍵盤控制div移動

描述:

div通過鍵盤事件上下左右實現div塊的移動

效果:

 

 

實現:

js:

var Method=(function () {
    return {
        EVENT_ID:"event_id",
        loadImage:function (arr) {
            var img=new Image();
            img.arr=arr;
            img.list=[];
            img.num=0;
//            如果DOM物件下的事件偵聽沒有被刪除掉,將會常駐堆中
//            一旦觸發了這個事件需要的條件,就會繼續執行事件函式
            img.addEventListener("load",this.loadHandler);
            img.self=this;
            img.src=arr[img.num];
        },
        loadHandler:function (e) {
            this.list.push(this.cloneNode(false));
            this.num++;
            if(this.num>this.arr.length-1){
                this.removeEventListener("load",this.self.loadHandler);
                var evt=new Event(Method.EVENT_ID);
                evt.list=this.list;
                document.dispatchEvent(evt);
                return;
            }
            this.src=this.arr[this.num];
        },
        $c:function (type,parent,style) {
            var elem=document.createElement(type);
            if(parent) parent.appendChild(elem);
            for(var key in style){
                elem.style[key]=style[key];
            }
            return elem;
        },
        divColor: function () {
            var col="#";//這個字串第一位為# 顏色的格式
            for(var i=0;i<6;i++){
                col+=parseInt(Math.random()*16).toString(16);//rondom*16後的隨機值即為0-1*16==0-16;   toString(16)為轉化為16進位制
            }
            return col;//最後返回一個七位的值 格式即為#nnnnnn 顏色的格式
        },
        random:function (min,max) {
            max=Math.max(min,max);
            min=Math.min(min,max);
            return Math.floor(Math.random()*(max-min)+min);
        },
        dragElem:function (elem) {
            elem.addEventListener("mousedown",this.mouseDragHandler);
            elem.self=this;
        },
        removeDrag:function (elem) {
            elem.removeEventListener("mousedown",this.mouseDragHandler);
        },
        mouseDragHandler:function (e) {
            if(e.type==="mousedown"){
                e.stopPropagation();
                e.preventDefault();
                document.point={x:e.offsetX,y:e.offsetY};
                document.elem=this;
                this.addEventListener("mouseup",this.self.mouseDragHandler);
                document.addEventListener("mousemove",this.self.mouseDragHandler);
            }else if(e.type==="mousemove"){
                this.elem.style.left=e.x-this.point.x+"px";
                this.elem.style.top=e.y-this.point.y+"px";
            }else if(e.type==="mouseup"){
                this.removeEventListener("mouseup",this.self.mouseDragHandler);
                document.removeEventListener("mousemove",this.self.mouseDragHandler);
            }
        }
    }
})();

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/Method.js"></script>
</head>
<body>
<script>
    var div;
    var code=[];
    var speed=2;
    var bool=false;
    init();
    function init() {
        div=Method.$c("div",document.body,{
            width:"50px",
            height:"50px",
            backgroundColor:"red",
            position:"absolute",
            left:0,
            top:0
        });
        window.addEventListener("keydown",keyHandler);
        window.addEventListener("keyup",keyHandler);
        setInterval(animation,16);
    }

    function keyHandler(e) {
        if(e.type==="keydown" && (!bool || code.indexOf(e.keyCode)===-1)){
            bool=true;
            code.push(e.keyCode);
        }else if(e.type==="keyup"){
            bool=false;
            code.length=0;
        }
    }

    function animation() {
        if(!bool)return;
        for(var i=0;i<code.length;i++){
            switch (code[i]){
                case 37:
                    div.style.left=div.offsetLeft-speed+"px";
                    break;
                case 38:
                    div.style.top=div.offsetTop-speed+"px";
                    break;
                case 39:
                    div.style.left=div.offsetLeft+speed+"px";
                    break;
                case 40:
                    div.style.top=div.offsetTop+speed+"px";
                    break;
            }
        }
    }
</script>
</body>
</html>