English 中文(简体)
这个jQuery动画代码可以变得更优雅吗?
原标题:Can this jQuery animation code be made more elegant?

对于jQuery来说,这是一个相当新的东西,我已经想出了一些可以工作的动画代码,但它看起来非常难看。

function help() {

    $("#searchBox").click(function(){
        $( #search_text_1 ).stop(true,true).hide();
        $( #search_text_2 ).stop(true,true).hide();
        $( #search_text_3 ).stop(true,true).hide();
        $( #search_text_4 ).stop(true,true).hide();
        $( #search_text_5 ).stop(true,true).hide();
        $( #free_search_help_1 ).stop(true,true).hide();
        $( #free_search_help_2 ).stop(true,true).hide();
        $( #free_search_help_3 ).stop(true,true).hide();
        $( #free_search_help_4 ).stop(true,true).hide();
        $( #free_search_help_5 ).stop(true,true).hide();
  });

    $( #search_text_1 ).delay(1000).fadeIn(500).delay(4000).fadeOut(500, function() {
        $( #search_text_2 ).fadeIn(500).delay(4000).fadeOut(500, function() {
            $( #search_text_3 ).fadeIn(500).delay(4000).fadeOut(500, function() {
                $( #search_text_4 ).fadeIn(500).delay(4000).fadeOut(500, function() {
                    $( #search_text_5 ).fadeIn(500);
                });
            });
        });
    });

    $( #free_search_help_1 ).delay(1000).fadeIn(500).delay(4000).fadeOut(500, function() {
        $( #free_search_help_2 ).fadeIn(500).delay(4000).fadeOut(500, function() {
            $( #free_search_help_3 ).fadeIn(500).delay(4000).fadeOut(500, function() {
                $( #free_search_help_4 ).fadeIn(500).delay(4000).fadeOut(500, function() {
                    $( #free_search_help_5 ).fadeIn(500);
                });
            });
        });
    });

}

我特别关注点击函数来停止动画,这对我来说有点棘手。我确实尝试过对一个分配给所有动画的div的类调用.stop(),但这似乎不起作用(没有错误)。

我还尝试过:

$("div:animated").stop(true,true);

但只有当点击发生在淡入淡出期间,而不是延迟时,这才有效。

最佳回答

您可以使用带选择器的开始^=DOCS获取以id开头的元素列表。然后可以使用for循环和.eq()DOCS方法来获取列表中的各个项目,并相应地执行它们的动画。

下面是一个例子。

function help() {
    var $st  = $( [id^="search_text_"] ),
        $fsh = $( [id^="free_search_help_"] );

    $("#searchBox").click(function() {
        $st.stop(true, true).hide();
        $fsh.stop(true, true).hide();
    });

    for (var i = 0; i < $st.length; i++) {
        var tDelay = 1000 + 5000*i;
        $st.eq(i).delay(tDelay).fadeIn(500).delay(4000).fadeOut(500);
        $fsh.eq(i).delay(tDelay).fadeIn(500).delay(4000).fadeOut(500);
    }
}
问题回答

Can you not add a class to your search divs? That would be for sure the most elegant solution! Then you would do this:

$("#searchBox").click(function(){ $( .search_text ).stop(true,true).hide(); $( .free_search_help ).stop(true,true).hide(); });

向“search_text_?”元素和“free_search_help.?”元素添加一个类,并使用该类来“stop(true,true).hide()”。

或使用:

$( div[id^=search_text]      ).stop(true, true).hide();
$( div[id^=free_search_help] ).stop(true, true).hide();

或:

我会通过首先将变量分配给相关元素,然后使用这些变量来整理和优化代码,而不是每次都让jQuery来定位元素。例如。

var $s[6], $f[6], i = 1;

while (i < 6) {
    $s[i  ] = $( #search_text_       + i);
    $f[i  ] = $( #free_search_help_  + i);
    $f[i  ].stop(true, true) hide();
    $s[i++].stop(true, true).hide();
}

$s[1].delay(....   etc.....
    $s[2].fadeIn(....    etc.....
        $s[3].fadeIn(....    etc.....
            $s[4].fadeIn(....    etc.....
                $s[5].fadeIn(....    etc....

Regards Neil





相关问题
images sliding continuously with <table> and jQuery

I m trying to write a little test page that circulates images through a window (see image). I have colored boxes inside a table (gray border), with each box being a element. <table id="boxes" ...

WPF 3d rotation animations

I have a few 3d rectangles on my screen that I want to pivot around the Y axis. I want to press down with the mouse, and rotate the 3d object to a max rotation, but when the user moves their mouse, ...

Disable Windows 7 touch animation in WPF

In Windows 7 when you touch the screen there is a short animation that occurs at the touch point. In my WPF app I want to display my own touch points, without showing the one supplied by Windows. ...

jQuery block moving

I wanna move a div block to the top, so I coded like this: CSS part: .movingPart{ margin-top:80px; } jQuery part: $(document).ready(function() { $( #btn ).click(function() { $( .movingPart )....

Direct 2D gnuplot PNG animation?

Can anyone please confirm that yes/no Gnuplot 4.5 (on CVS) can output 2D animated PNG files? I have numerous datasets but one line that I d like to show iteratively in 3 different places in my graph. ...

Visual State Manager VS Animations in WPF

There is a lot of talk about the simplicity of Visual States and the transitions between them in WPF/Silverlight. I have the need to generate animations dynamically at runtime, to animate the ...

Create a ImageIcon that is the mirror of another one

I ll like to know if there is a way to create a ImageIcon that is the mirror of another ImageIcon. Searching on Google, I found how to do it by using many AWT libraries. Is there a way to do it with ...

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签