我看到的第一個明顯的事情是一個div
作爲ul
的直接子。
即使這可能不會導致任何問題,那肯定是無效的HTML。大多數現代瀏覽器正在修復這......但仍然是一件壞事。
因此<ul id="slide_info">
內部的div
元素必須包含<li>
和</li>
。
然後,關於您的代碼,您使用巧妙的appendTo
技巧來「循環」整個列表項......爲什麼不使用相同的技巧?
現在關於淡入/淡出...有動畫延遲考慮。你可以使用回調函數,這些函數是在完成另一個函數時執行的函數。
$(document).ready(function(){
// Variable declarations
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
// Positionning
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
// ??? Why this "re-ordering" on load?
$('#slider ul li:last-child').prependTo('#slider ul');
// Hide all slide-info item except the first
$("#slide_info li").not("#slide_info li:nth-child(1)").hide();
// Click handler
$('a.control_next').click(function() {
// Animate the slides
$('#slider ul').animate({
left: - slideWidth
}, 300, function() {
$('#slider ul li:first-child').appendTo('#slider ul');
$('#slider ul').css('left', '');
});
// Animate slide infos
// Get the visible one into a variable
var visible = $("#slide_info li:visible");
// Fade it out and fade in the next one
visible.fadeOut(300, function(){
// This is a callback, so after the fadeOut:
visible.next().fadeIn(300,function(){
// This is also a callback...
// "visible" is now faded out, but still in this variable.
// Append it to the end of the list.
visible.appendTo("#slide_info");
});
})
});
});
Your Fiddle updated
這工作得很好,是非常接近我想要去實現,現在我有一個更好的瞭解如何邏輯的。非常感謝你的精力。 – LGWJ