整個列表間隔設定的時間向上移動一個item的高度css
html結構:html
<div class="slide-title"> <span>title1</span> <span>title2</span> <span>title3</span> </div> <div class="slide-container"><!--css設置時,注意高度是顯示多少個item,如:item的高度是30px,顯示3個,高度則是 3*30 = 90px --> <ul class="slide-list js-slide-list"> <li class="odd"><span>item1</span><span>item1</span><span>item1</span></li> <li class="even"><span>item2</span><span>item2</span><span>item2</span></li> <li class="even"><span>item2</span><span>item2</span><span>item2</span></li> </ul> </div>
實現思路:
得到js-slide-list下第一個li元素的高度,對它的height或marginTop進行一個從有到無的動畫變化,代碼以下:app
var doscroll = function(){ var $parent = $('.js-slide-list'); var $first = $parent.find('li:first'); var height = $first.height(); $first.animate({ height: 0 //或者改爲: marginTop: -height + 'px' }, 500, function() {// 動畫結束後,把它插到最後,造成無縫 $first.css('height', height).appendTo($parent); // $first.css('marginTop', 0).appendTo($parent); }); }; setInterval(function(){doscroll()}, 2000);