jQuery动画不能正常工作

时间:2015-08-21 15:43:03

标签: javascript jquery html css

我尝试使用jQuery动画内容。但是工作不正常。

这是一个jsFiddle示例:https://jsfiddle.net/oLt5uwz3/

我不想使用任何use FindBin qw( $RealBin ); use lib "$RealBin/../lib"; use My::Bintest qw( ... ); 或jQuery height。我想在mouseleave时按每个内容高度自动移动内容。在此示例中,当我单击slideUp()/slideDown()并快速mouseleave然后动画不能正常工作。



?

$('.open').click(function(){ 
    $('.lists').slideToggle();  });


$('.next').click(function(){ 
      $('.tip2').fadeIn(); 
      $('.tip1').hide();  });


$('.prev').click(function(){ 
      $('.tip2').hide(); 
      $('.tip1').fadeIn(); });


$(function(){ 
    $('.div').css('bottom','-'+$(".div").outerHeight()+'px');  });


$('.hover, .height').on('mouseenter',function(){ 

    $('.div').stop().animate({bottom:'0px'},'slow');   });



$('.hover, .height').on('mouseleave',function(){
    
    $('.div').stop().animate({bottom:'-'+$(".height").outerHeight()+'px'},'slow');});

.div {background:black;width:350px;position:fixed;bottom:0;right:0}
.hover {padding:2px;text-align:center;border-bottom:1px solid #ccc;font-size:12px;color:white;cursor:pointer;background:black;width:15%;display:inline-block;position:absolute;margin-top:-19px}
.tip1 {background:black;color:white;padding:5px}
.tip2 {display:none;background:black;color:white;padding:5px}
.prev, .next {text-align:center;border:1px solid #ccc;font-size:20px;color:white;cursor:pointer;background:black;width:35%;display:inline-block}
.open {padding:2px;text-align:center;border:1px solid #ccc;font-size:12px;color:white;cursor:pointer;background:black;width:22.8%;display:inline-block}
.lists {display:none;background:black;color:white;padding:5px;}




1 个答案:

答案 0 :(得分:3)

之所以发生这种情况,是因为“lists”div仍然达到其全高,并且之前调用了animate()。您需要考虑该偏移量。

$(function () {
    var initialHeight = $('.lists').outerHeight();

    $('.open').click(function () {
        if ($(this).attr('toggled') == "true")
            $(this).attr('toggled', "false");
        else
            $(this).attr('toggled', "true");

        $('.lists').slideToggle();
    });

    $('.next').click(function () {
        $('.tip2').fadeIn();
        $('.tip1').hide();
    });

    $('.prev').click(function () {
        $('.tip2').hide();
        $('.tip1').fadeIn();
    });

    $('.div').css('bottom', '-' + $(".div").outerHeight() + 'px');

    $('.hover, .height').on('mouseenter', function () {
        $('.div').stop().animate({ bottom: '0px' }, 'slow');
    });

    $('.hover, .height').on('mouseleave', function () {

        if ($('.open').attr('toggled') == "true") {
            remainingHeight = (initialHeight - $(".lists").outerHeight());
            $('.div').stop().animate({ bottom: '-' + ($(".height").outerHeight() + remainingHeight) + 'px' }, 'slow');
        }
        else {
            if ($('.lists').css('display') != 'none')
                remainingHeight = $(".lists").outerHeight();
            else
                remainingHeight = 0;
            $('.div').stop().animate({ bottom: '-' + ($(".height").outerHeight() - remainingHeight) + 'px' }, 'slow');
        }

    });

});
.div {background:black;width:350px;position:fixed;bottom:0;right:0}
.hover {padding:2px;text-align:center;border-bottom:1px solid #ccc;font-size:12px;color:white;cursor:pointer;background:black;width:15%;display:inline-block;position:absolute;margin-top:-19px}
.tip1 {background:black;color:white;padding:5px}
.tip2 {display:none;background:black;color:white;padding:5px}
.prev, .next {text-align:center;border:1px solid #ccc;font-size:20px;color:white;cursor:pointer;background:black;width:35%;display:inline-block}
.open {padding:2px;text-align:center;border:1px solid #ccc;font-size:12px;color:white;cursor:pointer;background:black;width:22.8%;display:inline-block}
.lists {display:none;background:black;color:white;padding:5px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="div">
    <div class="hover">Hover</div>
    <div class="height">
    <div class="tip1">MacBook now comes with 1GB of memory standard and larger hard drives for the entire line perfect for running more of your favorite applications and storing growing media collections.</div>
    <div class="tip2">iPhone is a revolutionary new mobile phone that allows you to make a call by simply tapping a name or number in your address book, a favorites list, or a call log. It also automatically syncs all your contacts from a PC, Mac, or Internet service. And it lets you select and listen to voicemail messages in whatever order you want just like email.</div>
    <div class="prev"><</div>
    <div class="next">></div>
    <div class="open" toggled="false">?</div>
        <div class="lists">
            <ol>
                <li>Product 1 / Price : $10</li>
                <li>Product 2 / Price : $20</li>
                <li>Product 3 / Price : $30</li>
                <li>Product 4 / Price : $40</li>
                <li>Product 5 / Price : $50</li>
            </ol>
        </div>
    </div>
</div>