Отключить анимацию, если выполняется - PullRequest
0 голосов
/ 30 июня 2011

Как сделать так, чтобы этот скрипт отключал анимацию, если она уже анимируется?

<script type="text/javascript">
    $(document).ready(function() {
        $("#nav").hover(
            function() {
                $("#header-wrapper").animate({height:140},500);
                $("#dropdown").animate({height:100},500);
            },
            function() {
                $("#header-wrapper").animate({height:40},500);
                $("#dropdown").animate({height:0},500);
            }
        );
    });
</script>

Я действительно новичок в jQuery, и то, что я нашел с помощью поиска, не очень помогло.

Ответы [ 3 ]

3 голосов
/ 30 июня 2011

Использование .stop()

    $("#nav").hover(
        function() {
            $("#header-wrapper").stop(true,false).animate({height:140},500);
            $("#dropdown").stop(true,false).animate({height:100},500);
        },
        function() {
            $("#header-wrapper").stop(true,false).animate({height:40},500);
            $("#dropdown").stop(true,false).animate({height:0},500);
        }
    );
2 голосов
/ 30 июня 2011

Используйте функцию .stop () jQuery

$("#dropdown").stop().animate({height:0},500);
0 голосов
/ 01 июля 2011

попробуйте это:

$("#nav").hover(function() {
    if (!$("#header-wrapper").is(':animated')) {
        $("#header-wrapper").animate({
            height: 140
        },
        500);
        $("#dropdown").animate({
            height: 100
        },
        500);
    } else {
        $("#header-wrapper").stop(true, false).css('height', $("#header-wrapper").height());
        $("#dropdown").stop(true, false).css('height', $('#dropdown').height());
    }
},
function() {
    if (!$("#header-wrapper").is(':animated')) {
        $("#header-wrapper").animate({
            height: 40
        },
        500);
        $("#dropdown").animate({
            height: 0
        },
        500);
    } else {
        $("#header-wrapper").stop(true, false).css('height', $("#header-wrapper").height());
        $("#dropdown").stop(true, false).css('height', $("#dropdown").height());
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...