jquery - 中心效应

标签 jquery html css

我有以下代码:

http://jsfiddle.net/G8Ste/577/

但我希望效果是全方位的,而不是仅仅向右和底部。

我已经尝试过 margin: 0、auto 和其他 css 和 html 样式,将其与中心对齐,但它不起作用。

谢谢

最佳答案

$('#myimage').mousedown(function() {
    var img = $(this);
    img.stop().animate({
        left: ['30px', 'swing'],   // left + (width - new width)/2
        top: ['60px', 'swing'],    // top + (height - new height)/2
        width: ['80px', 'swing'],
        height: ['80px', 'swing'],
    }, 50, 'swing');
    $(document).one("mouseup", function(){
        img.stop().animate({
            left: ['-5px', 'swing'], // left - (new width - width)/2
            top: ['25px', 'swing'],  // top - (new height - height)/2
            width: ['150px', 'swing'],
            height: ['150px', 'swing'],
        }, 150, 'swing', function() {
            $(this).animate({
                left: ['20px', 'swing'],
                top: ['50px', 'swing'],
                width: ['100px', 'swing'],
                height: ['100px', 'swing'],
            }, 1000, 'swing');
        });
    });
});

http://jsfiddle.net/diode/G8Ste/598/

编辑:

要使其适用于类为 myimage 的所有图像,请执行以下操作 一开始通过运行一次存储所有图像的初始属性

$(".myimage").each(function(i, img){ 

    $(img).data("width", $(img).width()); 
    $(img).data("height", $(img).height());
    $(img).data("left", parseInt($(img).css("left"),10)); 
    $(img).data("top", parseInt($(img).css("top"),10));

});

然后在 mousedown 处理程序中

var img = $(this);

var ww = img.data("width"); 
var hh = img.data("height");
var left = img.data("left"); 
var top = img.data("top");

// then same code as last jsfiddle

.

关于jquery - 中心效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484254/

相关文章:

javascript - jQuery tablesorter - 从排序器/过滤器中隐藏特定的表格内容

html - nth-of-type 选择具有特定类别的前 3 个元素

c# - 如何在 Razor mvc5 中每第 3 列创建一行?

javascript - 使用 Javascript 强制图像动态调整为容器/浏览器高度

javascript - Firefox 给出了错误的图像宽度和高度值

javascript - javascript数组和对象有固定顺序吗?

javascript - 未捕获的类型错误 : undefined is not a function

jQuery 菜单,单击时展开和缩回菜单

javascript - 组件输入错误

html - Bootstrap 折叠跳跃过渡,无填充/边距