使用touch移动元素

$.fn.MyDrag = function () {
        this.each(function () {
            var self = $(this);
            self.css("position","absolute")
            var draging = false;
            var clientX,clientY,moveClientX,moveClientY,eleTop,eleLeft;
            self.bind("touchstart", function (e) {
                draging = true;
                var touch = e.touches[0];
                clientX =touch.clientX;
                clientY = touch.clientY;
                eleTop = self.position().top;
                eleLeft = self.position().left;


            });


            self.bind("touchmove", function (e) {
                if(draging){
                    e.preventDefault();
                    var touch = e.touches[0];
                    moveClientX = touch.clientX;
                    moveClientY = touch.clientY;
                   self.css("left",eleLeft+moveClientX-clientX);
                   self.css("top",eleTop+moveClientY-clientY);
                }
            });
            self.bind("touchend", function () {
                draging = false;
            })
        })
    }


    $(".box").MyDrag();



<style type="text/css">


        *{margin:0; padding: 0;}
        img{ border:none;}
        a{ text-decoration: none;color:#fff;}
        li{list-style-type: none;}
        i{ font-style: normal;}
        html{height:100%;font-size:20px;overflow-x: hidden;}
        input,button,select{ -webkit-appearance:none;-webkit-tap-highlight-color:rgba(0,0,0,0);}
        @media screen and (min-width:1000px){html{font-size:40px}}
        @media screen and (max-width:999px){html{font-size:30px}}
        @media screen and (max-width:600px){html{font-size:20px}}
        body{height:100%;font-size:1rem;color:#fff;min-width: 320px;font-family: Helvetica Neue,Microsoft Yahei,Hiragino Sans GB,WenQuanYi Micro Hei,sans-serif;}
        .body-wrap{
            min-width: 320px;
            height: 100%;
            background-color: white;
        }


        .box{
            width: 30%;
            height: 4rem;
            background-color: #0000FF;
        }
    </style>
</head>
<body>
<div class="body-wrap">
    <div class="box">hello</div>
</div>
<script type="text/javascript" src="zepto.js"></script>


效果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值