在页面加载时进行div抖动?

时间:2011-11-07 20:00:00

标签: javascript jquery html css

有没有在页面加载时使div框抖动?可能只是一两次?

更新:在此网址上,我仍然无法处理页面加载,我做错了什么? http://tinyurl.com/79azbav

我认为我被困在了电网负载上;这种失败可以在这里看到: Get onpage to work correctly

我也尝试使用已经实现的body onLoad

启动动画
<body onLoad="document.emvForm.EMAIL_FIELD.focus(); document.ready.entertext.shake();" >

但仍然像冠军一样失败。

2 个答案:

答案 0 :(得分:25)

尝试这样的事情:

修改
Shake()更改为shake()以与jQuery约定保持一致。

jQuery.fn.shake = function() {
    this.each(function(i) {
        $(this).css({ "position": "relative" });
        for (var x = 1; x <= 3; x++) {
            $(this).animate({ left: -25 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
        }
    });
    return this;
} 

修改
在我的示例中,left位置设置为25,但您可以减少此值以获得更微妙的效果,或者将其增加以获得更明显的效果。

使用shake功能:

$("#div").shake();

这是一个演示它的jsFiddle:http://jsfiddle.net/JppPG/3/

答案 1 :(得分:4)

@ James-Johnson的轻微变化是〜{动态〜absolute定位的元素的优秀答案。此函数抓取元素的当前left位置并相对于此点摇动它。我已经去了一个不太暴力的摇晃,气压标记10。

jQuery.fn.shake = function () {
    this.each(function (i) {
        var currentLeft = parseInt($(this).css("left"));
        for (var x = 1; x <= 8; x++) {            
            $(this).animate({ left: (currentLeft - 10) }, 10).animate({ left: currentLeft }, 50).animate({ left: (currentLeft + 10) }, 10).animate({ left: currentLeft }, 50);
        }
    });
    return this;
}