touchstart ,touchmove, touchend 页面随手指滑动

<pre style="font-family: 'Courier New'; background-color: rgb(255, 255, 255);"><span style="font-size:32px;">js写法</span>

 
</pre><pre code_snippet_id="1717282" snippet_file_name="blog_20160615_3_9095677" name="code" class="html">
//详情页滑出效果
function swipePage(){//页面滑动切换效果
        //$("#goodsdetil").animate({right:-(e.pageX)}/*,400,function(){$("#goodsMealDiv").empty();}*/);
        //$('body').css("overflow","hidden");
        $('body').on('touchmove', function (event) {//禁止浏览器上下滑动
            event.preventDefault();
        });
        var startX, startY, endX, endY;
        var showADID = 1;
        document.getElementById("goodsdetil").addEventListener("touchstart", touchStart, false);
        document.getElementById("goodsdetil").addEventListener("touchmove", touchMove, false);
        document.getElementById("goodsdetil").addEventListener("touchend", touchEnd, false);
        function touchStart(event) {
            var touch = event.touches[0];
            startY = touch.pageY;
            startX = touch.pageX;
        }
        function touchMove(event) {
            var touch = event.touches[0];
            endX = touch.pageX;
            //console.log("X轴移动大小:" + (startX - endX));
            if((startX - endX)<0){
                $("#goodsdetil").animate({right:(startX - endX)},0);
            }
        }
        function touchEnd(event) {
            if((startX - endX)>-300){
                $("#goodsdetil").animate({right:"0px"},300);
            }else if((startX - endX)<-300){
                pageHide();
            }
        }
}



jQuery 写法


function swipePage(){//页面滑动切换效果
        //$("#goodsdetil").animate({right:-(e.pageX)}/*,400,function(){$("#goodsMealDiv").empty();}*/);
        //$('body').css("overflow","hidden");
        $('body').on('touchmove', function (event) {//禁止浏览器上下滑动
            event.preventDefault();
        });
        var startX, startY, endX, endY;
        $("#goodsdetil").bind("touchstart", touchStart);
        $("#goodsdetil").bind("touchmove", touchMove);
        $("#goodsdetil").bind("touchend", touchEnd);
        function touchStart(event) {
            var touch = event.originalEvent.targetTouches[0];
            startY = touch.pageY;
            startX = touch.pageX;
        }
        function touchMove(event) {
            var touch = event.originalEvent.targetTouches[0];
            endX = touch.pageX;
            //console.log("X轴移动大小:" + (startX - endX));
            if((startX - endX)<0){
                $("#goodsdetil").animate({right:(startX - endX)},0);
            }
        }
        function touchEnd(event) {
            if((startX - endX)>-300){
                $("#goodsdetil").animate({right:"0px"},300);
            }else if((startX - endX)<-300){
                pageHide();
            }
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值