七月
17

JavaScript: 支持iphone、ipad 使用两根手指放大图片

昨天写了一篇《 JavaScript: 让拖动支持iphone/ipad触摸 》,今天又无聊,想到了图片放大(zoom),让使用ios设备的的用户可以使用两根手指来缩放图片。想了一下,很容易就实现了。

其实原理也就是在图片接收了两个有效触摸点后,在手指移动过程中检测两点之间的距离的变化,以此来对图片进行放大缩小。

不多说,demo在这里(必须使用ios设备查看)。代码如下:

var getZoomClass=(function(){
var SupportsTouches = ("createTouch" in document),//判断是否支持触摸
    $=function(id){
        return document.getElementById(id);
    },
    preventDefault=function(ev){
        if(ev)ev.preventDefault();
        else window.event.returnValue = false;
    },
    getTwoPointSub=function(ev){
        var x1=x2=y1=y2=0,sub;
        x1=ev.touches.item(0).pageX;x2=ev.touches.item(1).pageX;
        y1=ev.touches.item(0).pageY;y2=ev.touches.item(1).pageY;
        sub=Math.round(Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)));
        return sub;
    },
    getSize=function(img){
        return {width:img.width,height:img.height};
    };
    function _zoom(opt){
        if(!SupportsTouches)return false;
        this.el=typeof opt.el=='string'?$(opt.el):opt.el;
        this.onstart=opt.start || new Function();//
        this.onmove=opt.move || new Function();
        this.onend=opt.end || new Function();
        this.action=false;
        this.init();
    }
    _zoom.prototype={
        init:function(){
            this.el['ontouchstart']=this.bind(function(e){//绑定节点的 [鼠标按下/触摸开始] 事件
                if(this.action || e.touches.length<2)return false;//这里监测了触摸点的数量,小于两个说明不是在进行缩放操作
                else this.action=true;
                preventDefault(e);
                this.startSub=getTwoPointSub(e);
                this.startSize=getSize(this.el);
                this.onstart();
                document.ontouchmove=this.bind(function(e){
                    if(e.touches.length>1){
                        preventDefault(e);//取消文档的默认行为[鼠标移动、触摸移动]
                        this.nowSub=getTwoPointSub(e);
                        this.el.width=this.startSize.width*this.nowSub/this.startSub;
                        this.el.height=this.startSize.height*this.nowSub/this.startSub;
                        this.onmove();
                    }
                },this);
                document.ontouchend=document.ontouchcancel=this.bind(function(){
                    document.ontouchend=document.ontouchcancel=document.ontouchmove=null;
                    this.action=false;
                    this.onend();
                },this);
            },this);
        },
        bind:function(fn,obj){
            return function(){
                fn.apply(obj,arguments);
            }
        },
        tool:null
    }
    return function(opt){
        return new _zoom(opt);
    }
})();

demo在这里(必须使用ios设备查看)。

本文标签: , , , ,

分享

本文短网址: http://qiqi.boy.im/ez

这篇文章已经有 20 条评论

Comments (20) Trackbacks (0)
You can leave a response or Trackback this entry .
  1. 小邪 -#1

    啊啊 啊啊啊 啊啊啊啊啊

  2. Map -#2

    天.. 很好很强大 :arrow:

  3. WayJam -#3

    你还真和IOS干上啦啊。

  4. 刘印 -#4

    都去干ios了.
    哎.我用的塞班没人管了.

  5. mopvhs -#5

    这猫放大以后更可爱了!iPad2 测试完毕!

  6. 权子 -#6

    木有那么高档的设备~~~哈哈~~~

  7. mirror -#7

    评论框的AJAX好牛啊

  8. 人好哇! -#8

    唔。。。我表示现在为IOS做的优化随处可见了呢~~不过还真没想过JS还可以这么搞,哈哈

  9. 孤夜狂狼 -#9

    Android 平板 XOOM 测试成功……
    好东西啊~ 收藏了~ 虽然暂时没有什么用法方面的创意……

  10. 小五 -#10

    没有接触过IP4

  11. 丰禾棋牌 -#11

    电脑这方面不是特别懂 学习了

  12. 晨光 -#12

    这方面懂得还是很少呀

  13. 懒人 -#13

    我还在折腾安卓

  14. 地方 -#14

    对方的身份

  15. 地方 -#15

    gggggggggggg

  1. No trackbacks yet.

Leave a Reply

Hi , say something.

  • :?:
  • :razz:
  • :sad:
  • :evil:
  • :!:
  • :smile:
  • :oops:
  • :grin:
  • :eek:
  • :shock:
  • :???:
  • :cool:
  • :lol:
  • :mad:
  • :twisted:
  • :roll:
  • :wink:
  • :idea:
  • :arrow:
  • :neutral:
  • :cry:
  • :mrgreen: