更新时间:2023-10-24 23:36:04
尝试这种方式:-
$(function() {
var interval = 10;
var duration= 1000;
var shake= 3;
var vibrateIndex = 0;
var selector = $('.box'); /* Your own container ID*/
$(selector).hover( /* The button ID */
function(){
vibrateIndex = setInterval(vibrate, interval);
},
function(){
clearInterval(vibrateIndex);
$(selector).stop(true,false)
.css({position: 'static', left: '0px', top: '0px'});
}
);
var vibrate = function(){
$(selector).stop(true,false)
.css({position: 'relative',
left: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px',
top: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px'
});
}
});
请参考 演示
Refer this DEMO