微微浮动在div框周围移动

时间:2011-12-20 03:20:27

标签: javascript jquery css

我想在我的网站上有三个div框轻微切换并慢慢地“悬停”,直到鼠标悬停在它们上面 - 然后我的自定义悬停效果发生在我已经使用过的地方。 (摇晃/嘎嘎声和不透明度)。但是我很难理解如何让动画稍微动起来。

这就是我跟小提琴的关系 - 我错过了什么?

http://jsfiddle.net/7t5q9/

2 个答案:

答案 0 :(得分:1)

您未在示例中指定持续时间。至于使div缓慢浮动,你可以使用随机的顶部和左侧值重复动画重复动画,如下所示:

$(function() {
    var timeout;
    function moveAimlessly() {
        $(".aimless").each(function() {
            $(this).stop().animate({
                top: Math.floor(Math.random()*2) == 1 ? "+=" + Math.floor(Math.random()*41).toString() : "-=" + Math.floor(Math.random()*41).toString(),
                left: Math.floor(Math.random()*2) == 1 ? "+=" + Math.floor(Math.random()*41).toString() : "-=" + Math.floor(Math.random()*41).toString()
            }, 4000);
        });
        timeout = setTimeout(moveAimlessly, 4000);
    }
    moveAimlessly();

    $(".aimless").on("hover", function() {
        clearTimeout(timeout);
        $(".aimless").stop();
    });
});

链接到示例:http://jsfiddle.net/7zZEG/1/

答案 1 :(得分:0)

未捕获的ReferenceError:未定义持续时间

改变如下:

http://jsfiddle.net/7t5q9/2/

相关问题