簡體   English   中英

Jquery 動畫不流暢

[英]Jquery animate is not smooth

我正在使用 jquery animate 旋轉項目列表,將它們一一顯示。 基本上我試圖讓它們很好地滑入和滑出,但出於某種原因,它們只是立即進入並且沒有過渡。

這是我的代碼

var left_indent = parseInt($('#carousel-'+side+' #slides ul').css('right')) - item_width;
for (i = 0; i < 500; i++) {

        //slide the item
        $('#carousel-'+side+' #slides ul').animate({'right' : left_indent}, 400,"linear", function () {

            current_item = $('#carousel-'+side+' #slides li:nth-child(1)').data('id');

            if (current_item == selected_item)
                passes ++;

            if ( (passes > 1) && (current_item == selected_item) ) {
                if (side == 'creator') {
                    creator_opened = true;
                } else {
                    opponent_opened = true;
                }
                if (creator_opened && opponent_opened) {
                    $('#winner-block').show();                  
                }                   

            } else {
                //move the first item and put it as last item
                $('#carousel-'+side+' #slides li:last').after($('#carousel-'+side+' #slides li:first'));                    

                //set the default item to correct position
                $('#carousel-'+side+' #slides ul').css({'right' : left_value});
            }
        });

    }

有沒有人明白為什么我通過動畫完成的過渡沒有順利進行?

編輯:這是請求的附加 html/css。

<style>
#carousel-creator, #carousel-opponent {
        width:200px;
        height:220px;   
        margin:0 auto;
        margin-bottom:100px;
    }

    #slides {
        display:none;
        overflow:hidden;
        /* fix ie overflow issue */
        position:relative;
        width:200px;
        height:220px;
        border:3px solid #3F3F3F;
        background-color:#2c2c2c;
    }

    #slides ul {
        position:relative;
        left:0;
        top:0;
        list-style:none;
        margin:0;
        padding:0;  
        width:750px;            
    }

    #slides li {
        width:200px;
        height:220px;   
        float:left;
        text-align:center;
        padding:10px;
    }

    #slides li img {
        width:150px;
    }
</style>

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

<div class="col-md-6" style="background-color:none;">
    <div id="carousel-creator">

        <div id="slides"> 
            <ul>
                <li data-id="1">
                    <img src="image1.png" /><br />
                    <span class="item-title">Title 1</span><br />
                </li>
                <li data-id="2">
                    <img src="image2.png" /><br />
                    <span class="item-title">Title 2</span><br />
                </li>           
                <li data-id="3">
                    <img src="image3.png" /><br />
                    <span class="item-title">Title 3</span><br />
                </li>                                                   
            </ul>                                   
        </div>
    </div>
</div>

嘗試將動畫的下一部分放在animate回調中,這樣它只會在animate方法完成后發生,而不是在它開始后立即發生:

 var side = 'creator', item_width = parseInt($('#carousel-' + side + ' #slides li').outerWidth()), first_item, left_indent; function showNextImage(callback, timesLeft) { //slide the item left_indent = parseInt($('#carousel-' + side + ' #slides ul').css('left')) - item_width; $('#carousel-' + side + ' #slides ul').animate({ 'left': left_indent }, 1000, "linear", function() { left_indent += item_width; $($('#carousel-' + side + ' #slides ul').children()[0]).appendTo($('#carousel-' + side + ' #slides ul')); $('#carousel-' + side + ' #slides ul').css({ 'left': left_indent }); if (typeof timesLeft === 'number' && timesLeft > 1 && typeof callback === 'function') { callback(callback, timesLeft - 1); } }); } showNextImage(showNextImage, 500);
 #carousel-creator, #carousel-opponent { width: 200px; height: 220px; margin: 0 auto; margin-bottom: 100px; } #slides { overflow: hidden; /* fix ie overflow issue */ position: relative; width: 200px; height: 220px; border: 3px solid #3F3F3F; background-color: #2c2c2c; } #slides ul { position: relative; left: 0; top: 0; list-style: none; margin: 0; padding: 0; width: 750px; } #slides li { width: 200px; height: 220px; float: left; text-align: center; padding: 10px; } #slides li img { width: 150px; } #slides #li1 { background-color: red; } #slides #li2 { background-color: green; } #slides #li3 { background-color: blue; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="col-md-6" style="background-color:none;"> <div id="carousel-creator"> <div id="slides"> <ul> <li id="li1" data-id="1"> <img src="image1.png" /> <br /> <span class="item-title">Title 1</span> <br /> </li> <li id="li2" data-id="2"> <img src="image2.png" /> <br /> <span class="item-title">Title 2</span> <br /> </li> <li id="li3" data-id="3"> <img src="image3.png" /> <br /> <span class="item-title">Title 3</span> <br /> </li> </ul> </div> </div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM