DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 自己使用jquery寫的一個無縫滾動的插件
自己使用jquery寫的一個無縫滾動的插件
編輯:JQuery特效代碼     
效果圖:

 

html代碼:
. 代碼如下:
<h1>無縫滾動,向右滾動</h1>
<ul id="guoul1">
<li><img src="img/f1.jpg" alt="f1"/></li>
<li><img src="img/f2.jpg" alt="f2"/></li>
<li><img src="img/f3.jpg" alt="f3"/></li>
<li><img src="img/f4.jpg" alt="f4"/></li>
<li><img src="img/f5.jpg" alt="f5"/></li>
<li><img src="img/f6.jpg" alt="f6"/></li>
<li><img src="img/f7.jpg" alt="f7"/></li>
</ul>

<h1>無縫滾動,向左滾動</h1>
<ul id="guoul2">
<li>111111111111</li>
<li>222222222222</li>
<li>3333333333333</li>
<li>4444444444444</li>
<li>5555555555555</li>
<li>6666666666666</li>
<li>7777777777777</li>
<li>8888888888888</li>
<li>9999999999999</li>
</ul>
<h1>無縫滾動,向上滾動</h1>
<ul id="guoul3">
<li>111111111111</li>
<li>222222222222</li>
<li>3333333333333</li>
<li>4444444444444</li>
<li>5555555555555</li>
<li>6666666666666</li>
<li>7777777777777</li>
<li>8888888888888</li>
<li>9999999999999</li>
</ul>
<h1>無縫滾動,向下滾動</h1>
<ul id="guoul4">
<li>111111111111</li>
<li>222222222222</li>
<li>3333333333333</li>
<li>4444444444444</li>
<li>5555555555555</li>
<li>6666666666666</li>
<li>7777777777777</li>
<li>8888888888888</li>
<li>9999999999999</li>
</ul>
<h1>無縫滾動,非ul,li標簽組合,向右滾動</h1>
<div id="guoul5">
<p>111111111111</p>
<p>222222222222</p>
<p>3333333333333</p>
<p>4444444444444</p>
<p>5555555555555</p>
<p>6666666666666</p>
<p>7777777777777</p>
<p>8888888888888</p>
<p>9999999999999</p>
</div>
<h1>不動</h1>
<ul id="guoul6">
<li>111111111111</li>
<li>222222222222</li>
<li>3333333333333</li>
<li>4444444444444</li>
<li>5555555555555</li>
<li>6666666666666</li>
<li>7777777777777</li>
<li>8888888888888</li>
<li>9999999999999</li>
</ul>

css代碼:
. 代碼如下:
ul, li,h1 { margin: 0; padding: 0; list-style-type:none;}
ul,div { height: 200px; border: 1px solid red; width: 300px; padding: 30px;margin:10px;list-style-type:none;}
li,p { height: 30px; line-height: 30px; margin-top: 10px; background-color: Gray; color: Yellow; margin-left:10px;}
#guoul1{ width:1000px; height:188px;margin: 0; padding: 0;}
#guoul1 li{ width:300px; height:188px;margin: 0; padding: 0; margin-left:10px;}

js插件代碼:
. 代碼如下:
; (function ($) {
var defaults = {
dir: "left", //none:不動,up:上,right:右,down:下,right:左
delay: 30,//執行時間
};
$.fn.gysContentDisplay = function (opt) {
opt = $.extend({}, defaults, opt);

//全局變量區域
var obj = $(this); //當前對象
obj.css({ "overflow": "hidden" }); //初始化元素
if (opt.dir == "none") return;
var objLis = obj.children(); //對象中的子元素
objLis.css({ "overflow": "hidden" });
var objSize = 0; //外框尺寸
var scrollEvent = "scrollLeft"; //滾動條的滾動方向
var liTotalSize = 0, liTotalSizeOther = 0; //每個li元素的尺寸(寬或者高),克隆之後的總尺寸
var scrollSize = 0, //滾動條的實際距離
scrollSizeMax = 0, //滾動條的最大距離
scrollSizeMin = 0; //滾動條的最小距離
var interval = ""; //記錄setInterval

if (opt.dir == "up" || opt.dir == "down") {//上下
objSize = obj.innerHeight();
scrollEvent = "scrollTop";
obj.css({ "padding-top": 0, "padding-bottom": 0 }).height(objSize);
}
else if (opt.dir == "left" || opt.dir == "right") {//左右
objSize = obj.innerWidth();
scrollEvent = "scrollLeft";
obj.css({ "padding-left": 0, "padding-right": 0 }).width(objSize);
}
else {
alert("你的dir參數有誤");
}

var getChildTotalSize = function (dir) {// 定義獲取li總尺寸的方法
if (dir == "left" || dir == "right") {
objLis.css("float", "left");
return function () {
objLis.each(function () {
liTotalSize += $(this).outerWidth(true);
});
}
}
else if (dir == "up" || dir == "down") {
objLis.css("float", "none");
return function () {
objLis.each(function () {
liTotalSize += $(this).outerHeight(true);
});
}
}
} (opt.dir);
getChildTotalSize(); //獲得所有的li的總尺寸,在方法中賦值

(function () {
var cloneCount = Math.ceil(objSize * 2 / liTotalSize); //賦值子元素多少遍
var cloneHtmlNow = "", cloneHtmlStart = obj.html(); //原始的子元素字符串

for (var i = 0; i < cloneCount; i++) {
cloneHtmlNow += cloneHtmlStart;
}
obj.append(cloneHtmlNow);
liTotalSizeOther = (cloneCount + 1) * liTotalSize; //獲取添加了子元素之後的長度
})();


if (opt.dir == "left" || opt.dir == "right") {
obj.css({ "position": "relative", "z-index": 0 });
obj.children().css({ "position": "absolute", "z-index": 1 });
var left = 0;
obj.children().each(function () {
$(this).css({ "left": left + "px", "top": 0 });
left += $(this).outerWidth(true);
});
}


//滾動條的滾動方法
function scrollChange(dir) {
if (dir == "left" || dir == "up") {
obj[scrollEvent](0);
scrollChange = function () {
scrollSize++;
if (scrollSize >= liTotalSize) scrollSize = 0;
obj[scrollEvent](scrollSize);
}
}
else if (dir == "right" || dir == "down") {
scrollSizeMax = liTotalSizeOther - objSize;
obj[scrollEvent](scrollSizeMax);
scrollSize = scrollSizeMax;
scrollSizeMin = scrollSizeMax - liTotalSize;
scrollChange = function () {
scrollSize--;
if (scrollSize <= scrollSizeMin) scrollSize = scrollSizeMax;
obj[scrollEvent](scrollSize);
}
}
};
scrollChange(opt.dir);
interval = setInterval(scrollChange, opt.delay);
obj.children().on("mouseover", function () {
clearInterval(interval);
}).on("mouseleave", function () {
interval = setInterval(scrollChange, opt.delay);
});
}
})(jQuery);

插件的調用:
. 代碼如下:
$(function () {
$("#guoul1").gysContentDisplay({ dir: "right" });
$("#guoul2").gysContentDisplay({ dir: "left" });
$("#guoul3").gysContentDisplay({ dir: "up" });
$("#guoul4").gysContentDisplay({ dir: "down" });
$("#guoul5").gysContentDisplay({ dir: "right" });
$("#guoul6").gysContentDisplay({ dir: "none" });
})
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved