Fork me on GitHub

移动端拖拽

var move=document.getElementsByClassName("page1_2")[0];
    var startX=0;
    var startY=0;
    var x=0;
    var y=0;
    var off = 0;

    move.addEventListener("touchstart", function (e) {
        var e = e || event;
        off=1;
        startX = this.offsetLeft;
        startY = this.offsetTop;
        x=e.touches[0].pageX-startX;
        y=e.touches[0].pageY-startY;

    });

    document.addEventListener("touchmove", function (e) {
        var e = e || event;
        if (!off)return;
        var x1=e.touches[0].pageX-x;
        var y1=e.touches[0].pageY-y;
        if(x1<0){
            x1=0
        }
        if(y1<0){
            y1=0
        }
        if(x1>window.innerWidth-move.offsetWidth){
            x1=window.innerWidth-move.offsetWidth
        }
        if(y1>window.innerHeight-move.offsetHeight){
            y1=window.innerHeight-move.offsetHeight
        }
        move.style.left=x1+'px';
        move.style.top=y1+"px";
});

        document.addEventListener('touchend', function (e) {
            off = 0;
        });

 

posted @ 2017-09-06 18:28  小白不白10  阅读(127)  评论(0编辑  收藏  举报