图像缩小功能使用JavaScript

时间:2013-08-17 06:47:20

标签: javascript jquery

我正在开发一个项目,当用户点击图像时图像会放大图像,再次点击图像会缩小图像。如何在代码here

中制作缩小功能
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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:2)

尝试查看以下代码是否对您有所帮助。 Demo

编辑:如果你想在同一页面的多张图片上执行此操作。

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;
}

答案 2 :(得分:1)

DEMO

DEMO Works with multiple images

$('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=0并移除style属性以使图像恢复正常。

答案 3 :(得分: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;