悬停时Jquery动画

时间:2011-10-07 10:09:51

标签: jquery jquery-animate

我正在寻找一个非常基本的功能:当鼠标悬停在窗口的某个区域上时,我想在其位置设置动画。 我已经知道如何以及何时触发动画功能问题,它的时间有限。我正在寻找一种方法来移动div相对于它的位置,同时盘旋。

干杯,janik

编辑:

我创建了一个JDfiddle。之前不知道。 jsfiddle.net/Ru2mZ/7解决我的问题:当鼠标悬停在按钮上时,我想要一个对象的连续移动或动画。因此,像$('#id')。animate({left:100},100)这样的基本动画将无效,因为它仅限于固定的结束位置和固定的时间。

2 个答案:

答案 0 :(得分:2)

        $('#someDiv').bind('mouseenter', function () {
            this.iid = setInterval(function () {
                aniDiv();
            }, 25);
        }).bind('mouseleave', function () {
            this.iid && clearInterval(this.iid);
        });
        function aniDiv() {
            $('#someDiv').animate({ marginLeft: '-=200px' },10);
        };

答案 1 :(得分:1)

以下是悬停功能的文档。 http://api.jquery.com/hover/

基本上,你必须

$("#div-id").hover(
  function () {
    //Do whatever you want to any element when the mouse is on the div with id: div-id. If you want to change anything related to the div being hoved, use the $(this) selector.
  },
  function () {
    //Do whatever you want to any element when the mouse was on the div with id: div-id and leaves it. If you want to change anything related to the div being hoved, use the $(this) selector.
  }
);