锤子JS平移不动的图像

时间:2016-04-25 14:06:58

标签: javascript jquery hammer.js

我已经提出了以下脚本(基于此处和网络上的答案)来捏合/缩放和移动缩放图像。它将进行放大/缩小但不会移动已调整大小的图像。即使代码看起来很好,document.write脚本也不会执行。我错过了一些明显的东西吗?

$(image).hammer().on('pinch', function(event) {
    var photo = $(this);

    newWidth = photo.width() * event.gesture.scale;
    newHeight = photo.height() * event.gesture.scale;

    // Convert from screen to image coordinates
    var x;
    var y;
    x -= offset.left + newX;
    y -= offset.top + newY;                                     

    newX += -x * (newWidth - width) / newWidth;
    newY += -y * (newHeight - height) / newHeight;

    photo.css('transform', "scale3d("+event.gesture.scale+", "+event.gesture.scale+", 1)");      
    wrap.css('transform', "translate3d("+newX+"px, "+newY+"px, 0)");

    width = newWidth;
    height = newHeight;
// Reset pinch
}).on('pinchout',function(event){
    photo.css('transform', "translate3d(0,0,0)");
// Start of moving zooming
}).on('pan panstart', function(event){
    photo.css('transform', "translate3d("+event.gesture.deltaX+"px, "+event.gesture.deltaY+"px, 0) ");                  
    document.write('ending panning 1');
// End of moving
}).on('pan panend', function(event){
    photo.css('transform', "translate3d(0,0,0)");
    panX += event.gesture.deltaX/zoom;
    panY += event.gesture.deltaY/zoom;
    wrap.css({left: panX,top: panY});
    document.write('ending panning 2');
})                          

1 个答案:

答案 0 :(得分:1)

var hammer = new Hammer(/** DOM Element **/); hammer.on(/** Your Script **/);

编辑:Ya实例化Hammer.js错误。请注意,我写了DOM Element,它似乎不适用于JQuery引用的元素。