微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

多种JQuery循环滚动文字图片效果代码

自己模仿JQ插件的写法写了一个循环滚动列表插件支持自定义上、下、左、右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置。JQ里面有些重复的地方,暂时没想到更好的方法去精简。不过效果还是可以的,如下(效果上传后都加速了,实际效果比这个要慢很多):

HTML代码如下:

rush:xhtml;"> <Meta charset="utf-8"> 循环滚动列表
Boxs Box01">
Box" id="autoScroll01">
Boxs Box02">
Box" id="autoScroll02">
Boxs Box03">
Box" id="autoScroll03">
Boxs Box04">
Box" id="autoScroll04">

css样式如下:

rush:css;"> @charset "utf-8"; /* 简单reset */ body,ul,li { margin: 0; padding: 0; } body { font: 14px/24px Microsoft YaHei; color: #333; } ul { list-style: none; } a { color: #333; outline: none; text-decoration: none; } a:hover { color: #2986dd; } img { border: none; } .clearfix:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; } .clear { display: block; clear: both; height: 0; font-size: 0; line-height: 0; content: "."; overflow: hidden; } .wrap { width: 900px; padding-top: 30px; margin: 0 auto; } .Boxs { padding: 15px; margin: 0 auto 30px; background-color: #e4fbff; } .Box01 { width: 870px; } .Box02 { float: left; width: 400px; } .Box03 { float: right; width: 400px; } .Box04 { width: 720px; } /* 具体样式 ---------- */ /* 通用必需样式 */ /* PS:有些重要样式在js里先写好了,下面id容器、ul和li标签的样式比较重要 */ .autoBox { position: relative; margin: 0 auto; overflow: hidden; } .autoBox ul { position: absolute; list-style: none; z-index: 2; } /* 第一、四个列表 */ /* PS:左右滚动型列表需要css定义高度,li标签可以有margin值 */ #autoScroll01,#autoScroll04 { width: auto; height: 174px; } #autoScroll01 ul li,#autoScroll04 ul li { float: left; width: 128px; height: 168px; padding: 3px; margin: 0 5px; _display: inline; } #autoScroll01 li a,#autoScroll04 li a { display: block; padding: 3px; border: 1px solid #dbdbdb; Box-shadow: 0 0 3px rgba(170,223,252,0.5); } #autoScroll01 li a:hover,#autoScroll04 li a:hover { border-color: #71ddff; Box-shadow: 0 0 3px rgba(77,185,245,.90); } #autoScroll01 li img,#autoScroll04 li img { display: block; width: 120px; height: 160px; } /* 第二、三个列表 */ /* PS:上下滚动型列表需要css定义宽度,li标签尽量不要设置margin值 */ #autoScroll02,#autoScroll03 { width: 100%; height: auto; } #autoScroll02 ul li { height: 30px; line-height: 30px; overflow: hidden; } #autoScroll03 ul li { height: 40px; line-height: 40px; overflow: hidden; } #autoScroll02 li a,#autoScroll03 li a { display: block; width: 100%; height: 24px; line-height: 24px; margin: 3px 0; text-overflow: ellipsis; white-space: Nowrap; overflow: hidden; } #autoScroll03 li a { margin: 8px 0; }

js代码如下:

rush:js;"> /** * Name : 循环滚动列表 **/ (function(jQuery){ $.fn.autoScroll = function (o) { o = $.extend({ //设置认参数 direction: 'left',interval: null,speed: null,distance: null,liWidth: null,liHeight: null,showNum: null },o || {}); return this.each(function(){ //回调开始 var $ts = $(this),$ul = $ts.children("ul"),$li = $ul.children("li"),len = $li.length; if (o.direction == 'up' || o.direction == 'down' ){ //根据类型设置css $ts.css({ "width": "100%","height": o.showNum * o.liHeight }); $ul.css({ "width": "100%","height": len * o.liHeight }); $li.css({ "float": "none","width": "100%","height": o.liHeight,"margin": 0,"padding": 0 }); }; if (o.direction == 'left' || o.direction == 'right' ){ //其实也可以在css里写好 $ts.css({ "width": o.showNum * o.liWidth }); $ul.css({ "width": len * o.liWidth }); $li.css({ "float": "left" }); }; switch (o.direction){ //分四种情况,进行事件调用 case 'left': sroLeft(); break; case 'right':sroRight(); break; case 'up': sroUp(); break; case 'down': srodown(); break; }; function sroLeft(){ //向左滚动事件 $ul.css("left",0); var left; function leftMoving(){ var dis = -o.distance,dif = -o.liWidth * (len - o.showNum); left = parseFloat($ul.css("left")); if (left <= dif){ $ul.css("left",0); left = 0; $ul.delay(o.interval); }; var ltdis = left + dis; if(ltdis <= dif){ ltdis = dif; }; $ul.animate({"left":ltdis+"px"},o.speed); }; $ul.hover( //鼠标移上、移下的阻止与恢复滚动 function(){ clearInterval(fnLeft); },function(){ fnLeft = setInterval(function(){leftMoving()},o.interval); } ); fnLeft = setInterval(function(){leftMoving()},o.interval); }; function sroRight(){ //向右滚动事件 $ul.css("right",0); var right; function rightMoving(){ var dis = -o.distance,dif = -o.liWidth * (len - o.showNum); right = parseFloat($ul.css("right")); if (right <= dif){ $ul.css("right",0); right = 0; $ul.delay(o.interval); }; var rtdis = right + dis; if(rtdis <= dif){ rtdis = dif; }; $ul.animate({"right":rtdis+"px"},o.speed); }; $ul.hover( function(){ clearInterval(fnRight); },function(){ fnRight = setInterval(function(){rightMoving()},o.interval); } ); fnRight = setInterval(function(){rightMoving()},o.interval); }; function sroUp(){ //向上滚动事件 $ul.css("top",0); var top; function upMoving(){ var dis = -o.distance,dif = -o.liHeight * (len - o.showNum); top = parseFloat($ul.css("top")); if (top <= dif){ $ul.css("top",0); top = 0; $ul.delay(o.interval); }; var tpdis = top + dis; if(tpdis <= dif){ tpdis = dif; }; $ul.animate({"top":tpdis+"px"},o.speed); }; $ul.hover( function(){ clearInterval(fnUp); },function(){ fnUp = setInterval(function(){upMoving()},o.interval); } ); fnUp = setInterval(function(){upMoving()},o.interval); }; function srodown(){ //向下滚动事件 $ul.css("bottom",0); var bottom; function downMoving(){ var dis = -o.distance,dif = -o.liHeight * (len - o.showNum); bottom = parseFloat($ul.css("bottom")); if (bottom <= dif){ $ul.css("bottom",0); bottom = 0; $ul.delay(o.interval); }; var bmdis = bottom + dis; if(bmdis <= dif){ bmdis = dif; }; $ul.animate({"bottom":bmdis+"px"},o.speed); }; $ul.hover( function(){ clearInterval(fnDown); },function(){ fnDown = setInterval(function(){downMoving()},o.interval); } ); fnDown = setInterval(function(){downMoving()},o.interval); }; }); }; })(jQuery);

兼容到IE6+,jq库用1.7+的都没问题 。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐