在線演示:http://demo.Vevb.com/js/2012/myslideLeftRight/
打包下載://m.survivalescaperooms.com/jiaoben/44973.html
核心代碼:
代碼如下:
(function($){
$.fn.extend({
"slidelf":function(value){
value = $.extend({
"prev":"",
"next":"",
"speed":""
},value)
var dom_this = $(this).get(0); //將jquery對象轉換成DOM對象;以便其它函數中調用;
var marginl = parseInt($("ul li:first",this).css("margin-left")); //每個圖片margin的數值
var movew = $("ul li:first",this).outerWidth()+marginl; //需要滑動的數值
//左邊的動畫
function leftani(){
$("ul li:first",dom_this).animate({"margin-left":-movew},value.speed,function(){
$(this).css("margin-left",marginl).appendTo($("ul",dom_this));
});
}
//右邊的動畫
function rightani(){
$("ul li:last",dom_this).prependTo($("ul",dom_this));
$("ul li:first",dom_this).css("margin-left",-movew).animate({"margin-left":marginl},value.speed);
}
//點擊左邊
$("."+value.prev).click(function(){
if(!$("ul li:first",dom_this).is(":animated")){
leftani();
}
});
//點擊左邊
$("."+value.next).click(function(){
if(!$("ul li:first",dom_this).is(":animated")){
rightani();
}
})
}
});
})(jQuery)
思路:
點擊左邊--
1.將第一個LI向左滑動,滑動的數值就是LI的寬度。(這里是用負margin-left來實現移動。)
2.滑動完成后,將這個LI插入到整個LI的最后一個(實現無縫滾動)
點擊右邊--
1.將最后一個LI插入到所有LI的第一個,并將其定位到可見區域之外,(這里用的是margin)
2.再將其滑動到可見區域。
注意:這里的IF判斷語句,是為了防止連續點擊“左”或“右”的銨鈕,而出現的BUG;
這判斷的意思:只有當LI不處于動畫狀態時,才執行移動函數。只要處于動畫狀態,點擊時,任何事都不發生。