3

我正在开发一个项目,其中当用户单击图像时图像会放大,而再次单击图像会缩小图像。如何在我的代码中创建缩小功能

function resizeImg (img) {
    var scale = 150/100;
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    $(img).css({
        width: img.width*scale, 
        height: img.height*scale
    });

    container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
    container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}
4

4 回答 4

2

你不需要很多 javascript 这里..只是一些好的 CSS,如下所示:http: //jsfiddle.net/95wqh/10/

JS:

$('.image').on('click', function () {
    $(this).toggleClass('zoom');
});

CSS:

#Container {
    position:relative;
    overflow:auto;
}

.zoom{
    -webkit-transform : scale(1.5);
    transform: scale(1.5);
}
于 2013-08-17T06:51:23.843 回答
2

试试看下面的代码是否对你有帮助。演示

编辑:以防万一您想在同一页面中的多个图像上执行此操作。

var zoom = "in";

$('.image').on('click', function () {
    this.zoom = (this.zoom == "in") ? "out" : "in";
    console.log(this.zoom);
    resizeImg(this);
});

function resizeImg (img) {
    var scale = 150/100;
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    if(img.zoom == "in"){
         $(img).css({
            width: img.width*scale, 
            height: img.height*scale
         });
    }
    else if (img.zoom == "out"){
        $(img).css({
            width: img.width/scale, 
            height: img.height/scale
        });
    }  
    container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
    container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}
于 2013-08-17T06:54:31.643 回答
1

演示

演示适用于多个图像

$('img').attr('zoom', 0);
$('.image').on('click', function () {
    resizeImg(this);
});

function resizeImg(img) {
    var zoom = $(img).attr('zoom');
    if (zoom == 0) {
        var scale = 150 / 100;
        var pos = $(img).offset();
        var clickX = event.pageX - pos.left;
        var clickY = event.pageY - pos.top;
        var container = $(img).parent().get(0);
        $(img).css({
            width: img.width * scale,
            height: img.height * scale
        }).attr('zoom', 1);
        container.scrollLeft = ($(container).width() / -2) + clickX * scale;
        container.scrollTop = ($(container).height() / -2) + clickY * scale;
    }else{
        $(img).attr('zoom',0).attr('style','');
    }
}

为所有图像设置zoom属性 = 0 -->$('img').attr('zoom', 0);

如果zoom属性设置为0我们将zoom生效并设置属性zoom=1

否则zoom属性设置1为已经缩放我们设置属性zoom=0style删除属性以使图像恢复正常。

于 2013-08-17T06:58:32.313 回答
1

如果您有多个图像,这也可能对您有所帮助

JSfiddle:http: //jsfiddle.net/95wqh/14/

JS代码:

$('.image').on('click', function () {
    if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
    this.toggleZoom = !this.toggleZoom;
    resizeImg(this);
});

function resizeImg(img) {
    var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;
    console.log(scale);
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    $(img).css({
        width: img.width * scale,
        height: img.height * scale
    });

    container.scrollLeft = ($(container).width() / -2) + clickX * scale;
    container.scrollTop = ($(container).height() / -2) + clickY * scale;
}

我添加的

    if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
    this.toggleZoom = !this.toggleZoom;

    var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;
于 2013-08-17T07:02:02.460 回答