自己使用jquery写的一个无缝滚动的插件_jquery
程序员文章站
2022-05-18 23:43:11
...
效果图:
html代码:
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, //滚动条的最大距离
scrollSi* = 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 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;
scrollSi* = scrollSizeMax - liTotalSize;
scrollChange = function () {
scrollSize--;
if (scrollSize 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" });
})
html代码:
复制代码 代码如下:
无缝滚动,向右滚动
无缝滚动,向左滚动
- 111111111111
- 222222222222
- 3333333333333
- 4444444444444
- 5555555555555
- 6666666666666
- 7777777777777
- 8888888888888
- 9999999999999
无缝滚动,向上滚动
- 111111111111
- 222222222222
- 3333333333333
- 4444444444444
- 5555555555555
- 6666666666666
- 7777777777777
- 8888888888888
- 9999999999999
无缝滚动,向下滚动
- 111111111111
- 222222222222
- 3333333333333
- 4444444444444
- 5555555555555
- 6666666666666
- 7777777777777
- 8888888888888
- 9999999999999
无缝滚动,非ul,li标签组合,向右滚动
111111111111
222222222222
3333333333333
4444444444444
5555555555555
6666666666666
7777777777777
8888888888888
9999999999999
不动
- 111111111111
- 222222222222
- 3333333333333
- 4444444444444
- 5555555555555
- 6666666666666
- 7777777777777
- 8888888888888
- 9999999999999
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, //滚动条的最大距离
scrollSi* = 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 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;
scrollSi* = scrollSizeMax - liTotalSize;
scrollChange = function () {
scrollSize--;
if (scrollSize 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" });
})
上一篇: php中实现多线程_PHP教程
推荐阅读
-
jquery滚动条插件jScrollPane的使用介绍
-
使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
-
网上找的代码太多,自己用JQuery写一个横向滑动切换效果,代码超少
-
jQuery的Nicescroll滚动条插件使用方法
-
使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
-
jcarousellite.js 基于Jquery的图片无缝滚动插件_jquery
-
基于jQuery的图片左右无缝滚动插件_jquery
-
一个用jquery写的判断div滚动条到底部的方法
-
自己使用js/jquery写的一个定制对话框控件_javascript技巧
-
一步一步教你写一个jQuery的插件教程(Plugin)_jquery