触摸扩展

    /**
     * 扩展对象
     */
    var Touch = new Object();
    
    /**
     * 滑动的矩形(起始值)
     */
    Touch.rect = {x:0,y:0,w:0,h:0};
    
    
    /**
     * 滑动的方向:x:0左,1右, y:0上,1下,
     */
    Touch.direction = {x:0,y:0};
    
    
    /**
     * 滑动距离
     */
    Touch.distance = {x:0,y:0};
    
    
    /**
     * 滑动点击(按下和弹起在同一位置)
     */
    Touch.click = false;
    
    
    
    /**
     * 按下
     */
    window.addEventListener('touchstart',function(e){
    
        Touch.rect.x = e.targetTouches[0].screenX;
        Touch.rect.y = e.targetTouches[0].screenY;
    
    },true);
    
    
    /**
     * 移动
     */
    window.addEventListener('touchmove',function(e){
    
        Touch.rect.w     = e.targetTouches[0].screenX;
        Touch.rect.h     = e.targetTouches[0].screenY;
        Touch.distance.x = e.targetTouches[0].screenX-Touch.rect.x;
        Touch.distance.y = e.targetTouches[0].screenY-Touch.rect.y;
    
    },true);
    
    
    /**
     * 弹起
     */
    window.addEventListener('touchend',function(e)
    {
    
        if(Touch.rect.w>Touch.rect.x)
        {
            Touch.direction.x = 1;
        }
        else
        {
            Touch.direction.x = 0;
        }
    
        if(Touch.rect.h>Touch.rect.y)
        {
            Touch.direction.y = 1;
        }
        else
        {
            Touch.direction.y = 0;
        }
    
        if(Touch.rect.w == Touch.rect.x && Touch.rect.h == Touch.rect.y)
        {
            Touch.click = true;
        }
        else
        {
            Touch.click = false;
        }
    
    },true);
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容