2017-10-06 80 views
1

除標記線外的所有內容均可用。如果該行被註釋的行代替 - 它可以工作。任何幫助?清除間隔和動畫不起作用

var goev = setInterval(fgoev, 2000); 
 

 
function fgoev() { 
 
    $('#eventwrap').animate({ 
 
    bottom: 0 
 
    }, 900).delay(5000).animate({ 
 
    bottom: -10 
 
    }, 100).animate({ 
 
    bottom: 0 
 
    }, 100).animate({ 
 
    bottom: -10 
 
    }, 100).animate({ 
 
    bottom: 0 
 
    }, 100); 
 
} 
 

 
$('#evclose').click(function() { 
 
    clearInterval(goev); 
 
    $('#eventwrap').animate({ 
 
    bottom: -125 
 
    }, 900); // doesn't work 
 
    //$('#eventwrap').hide(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='eventwrap'> 
 
    <div id='evclose'>X</div> 
 
    <a href='event.php' target='_blank' id='evinside'> 
 
    <div id='evmore'>MORE</div> 
 
    <div id='evtitleleft'>Days</div> 
 
    <div id='evtitleright'>Hours</div> 
 
    <div class='clear'></div> 
 
    <div id='evdays'> 
 
     <?php echo $diffa; ?> 
 
    </div> 
 
    <div id='evhours'> 
 
     <?php echo $diffb ?> 
 
    </div> 
 
    <div class='clear'></div> 
 
    </a> 
 
</div>

+1

不工作怎麼樣?根本沒有動畫,或者它在錯誤的位置等等。爲什麼清晰的間隔不工作?我注意到你的函數和間隔id變量具有相同的名稱,可能不是一個好主意。 – xander

+0

@xander,'不起作用'意味着什麼都沒有發生' – bonaca

+0

@xander,我改變函數名稱 - 沒有成功 – bonaca

回答

1

的問題是,因爲你需要清除所有設置在#evclose事件處理程序的新的底部位置之前已經排隊了#eventwrap元素的動畫。要做到這一點,您可以使用stop(true),像這樣:

var goev = setInterval(fgoev, 2000); 
 

 
function fgoev() { 
 
    $('#eventwrap').animate({ bottom: 0 }, 900).delay(5000) 
 
    .animate({ bottom: -10 }, 100) 
 
    .animate({ bottom: 0 }, 100) 
 
    .animate({ bottom: -10 }, 100) 
 
    .animate({ bottom: 0 }, 100); 
 
} 
 

 
$('#evclose').click(function() { 
 
    clearInterval(goev); 
 
    $('#eventwrap').stop(true, true).animate({ 
 
    bottom: -125 
 
    }, 900); 
 
});
#eventwrap { position: absolute; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='eventwrap'> 
 
    <div id='evclose'>X</div> 
 
    <a href='event.php' target='_blank' id='evinside'> 
 
    <div id='evmore'>MORE</div> 
 
    <div id='evtitleleft'>Days</div> 
 
    <div id='evtitleright'>Hours</div> 
 
    <div class='clear'></div> 
 
    <div id='evdays'> 
 
     <?php echo $diffa; ?> 
 
    </div> 
 
    <div id='evhours'> 
 
     <?php echo $diffb ?> 
 
    </div> 
 
    <div class='clear'></div> 
 
    </a> 
 
</div>

我也建議您在看使用CSS關鍵幀動畫移動#eventwrapper,而不是笨重,緩慢的JS動畫jQuery提供的方法。

+0

似乎現在起作用。非常感謝。你能給我任何例子,或相關鏈接到CSS關鍵幀動畫,請問? – bonaca

+0

當然,在這裏你去:https://developer.mozilla.org/en-US/docs/Web/CSS/%40keyframes –

+0

但它可能運行,停止...等這種動畫使用jQuery?正如你所看到的,我需要通過點擊一個div來停止這個動畫。 – bonaca