使用Javascript在Ipad上平移缩放图像

时间:2013-06-17 11:01:06

标签: jquery ipad pinchzoom pan

我正在编写一个jquery插件,允许用户捏合和缩放图像。然后,用户可以使用一次触摸平移图像。

我已经完成了所有工作但是图像周围的平移非常快,我需要放慢速度。我希望有人可能有一些建议。不幸的是,我不允许使用一些库,比如锤子JS。

以下是平移图像的代码。

var pos = $this.position();

var top = (pos.top + (orig.changedTouches[0].pageY - offsetPos.y)),
    left = (pos.left +(orig.changedTouches[0].pageX - offsetPos.x));

    //Apply Limits to keep image in viewport
    var viewWidth =  ($this.parent().width());
    var X_LIMIT = viewWidth - $this.width();
    var viewHeight =  ($this.parent().height());
    var Y_LIMIT = viewHeight - $this.height();

    if (left > viewWidth) { left = viewWidth; }
    if (left < X_LIMIT) { left = X_LIMIT; }
    if (top > viewHeight) { top = viewHeight; }
    if (top < Y_LIMIT) { top = Y_LIMIT; }

    if(top >0){top = 0;}
    if(left >0){left = 0;}

    $this.css({
        top: top,
        left: left
    });

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。如果有人将来需要这样的东西。代码附在下面。

var orig = e.originalEvent;
if(e.type == 'touchstart'){
    if(orig.touches.length == 1){
        touches = 1;
        origX = orig.targetTouches[0].pageX;
        origY = orig.targetTouches[0].pageY;
    }else if(orig.touches.length == 2){
        touches = 2;
    }
}else if(e.type == 'touchmove'){
    e.preventDefault();

    if(touches == 1){
    /*=================================================*\
    Detect Single Touch and pan around image accordingly
    \*=================================================*/
    width = $zoomImg.width();
    height = $zoomImg.height();

    if(width> originalWidth || height> originalHeight){
        e.stopPropagation();

        var delX = (orig.targetTouches[0].pageX - origX)/settings.sensitivity,
            delY = (orig.changedTouches[0].pageY - origY)/settings.sensitivity,
            pos = $zoomImg.position(),
            left = pos.left + delX,
            top = pos.top + delY,
            viewWidth =  ($this.parent().width()),
            X_LIMIT = viewWidth - width,
            viewHeight =  ($this.parent().height()),
            Y_LIMIT = viewHeight - height;


            //Apply Limits to keep image in viewport
            if (left > viewWidth) { left = viewWidth; }
            if (left < X_LIMIT) { left = X_LIMIT; }
            if (top > viewHeight) { top = viewHeight; }
            if (top < Y_LIMIT) { top = Y_LIMIT; }

            if(top >0){top = 0;}
            if(left >0){left = 0;}

            $zoomImg.css({
                top: top,
                left: left
            });
        }
    }
}