- 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
- 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
- QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
- QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
- QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com
原文:http://www.rooufer.cn/blog/article.asp?id=131
jQuery实现的滚屏效果实现方法:
一:有上下按钮控制的:
<html>
<head>
<title>jquery滚屏效果</title>
<style type="text/css">
ul,li{margin:0;padding:0}#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}#scrollDiv li{height:25px;padding-left:10px;}</style>
<script type="text/javascript" src="/js/jquery.js"></script>1:2: <script type="text/javascript">3: (function($){4: $.fn.extend({5: Scroll:function(opt,callback){6: //参数初始化7: if(!opt) var opt={};8: var _btnUp = $("#"+ opt.up);//Shawphy:向上按钮9: var _btnDown = $("#"+ opt.down);//Shawphy:向下按钮10: var timerID;11: var _this=this.eq(0).find("ul:first");12: var lineH=_this.find("li:first").height(), //获取行高13: line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数,默认为一屏,即父容器高度14: speed=opt.speed?parseInt(opt.speed,10):500; //卷动速度,数值越大,速度越慢(毫秒)15: timer=opt.timer //?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒)16: if(line==0) line=1;17: var upHeight=0-line*lineH;18: //滚动函数19: var scrollUp=function(){20: _btnUp.unbind("click",scrollUp); //Shawphy:取消向上按钮的函数绑定21: _this.animate({22: marginTop:upHeight23: },speed,function(){24: for(i=1;i<=line;i++){25: _this.find("li:first").appendTo(_this);26: }27: _this.css({marginTop:0});28: _btnUp.bind("click",scrollUp); //Shawphy:绑定向上按钮的点击事件29: });30:31: }32: //Shawphy:向下翻页函数33: var scrollDown=function(){34: _btnDown.unbind("click",scrollDown);35: for(i=1;i<=line;i++){36: _this.find("li:last").show().prependTo(_this);37: }38: _this.css({marginTop:upHeight});39: _this.animate({40: marginTop:041: },speed,function(){42: _btnDown.bind("click",scrollDown);43: });44: }45: //Shawphy:自动播放46: var autoPlay = function(){47: if(timer)timerID = window.setInterval(scrollUp,timer);48: };49: var autoStop = function(){50: if(timer)window.clearInterval(timerID);51: };52: //鼠标事件绑定53: _this.hover(autoStop,autoPlay).mouseout();54: _btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);//Shawphy:向上向下鼠标事件绑定55: _btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);56:57: }58: })59: })(jQuery);60:61: $(document).ready(function(){62: $("#scrollDiv").Scroll({line:4,speed:500,timer:3000,up:"btn1",down:"btn2"});63: });</script>
</head>
<body>
<p>多行滚动演示:</p>
<div id="scrollDiv">
<ul>
<li>这是公告标题的第一行</li>
<li>这是公告标题的第二行</li>
<li>这是公告标题的第三行</li>
<li>这是公告标题的第四行</li>
<li>这是公告标题的第五行</li>
<li>这是公告标题的第六行</li>
<li>这是公告标题的第七行</li>
<li>这是公告标题的第八行</li>
</ul>
</div>
<button id="btn1">down</button>
<button id="btn2">up</button>
</body>
</html>
二:简洁无控制的
<html>
<head>
<title>多行滚动演示</title>
<style type="text/css">
ul,li{margin:0;padding:0}#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}#scrollDiv li{height:25px;padding-left:10px;}</style>
<script type="text/javascript" src="/js/jquery.js"></script>1:2: <script type="text/javascript">3: (function($){4: $.fn.extend({5: Scroll:function(opt,callback){6: //参数初始化7: if(!opt) var opt={};8: var _this=this.eq(0).find("ul:first");9: var lineH=_this.find("li:first").height(), //获取行高10: line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数,默认为一屏,即父容器高度11: speed=opt.speed?parseInt(opt.speed,10):500, //卷动速度,数值越大,速度越慢(毫秒)12: timer=opt.timer?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒)13: if(line==0) line=1;14: var upHeight=0-line*lineH;15: //滚动函数16: scrollUp=function(){17: _this.animate({18: marginTop:upHeight19: },speed,function(){20: for(i=1;i<=line;i++){21: _this.find("li:first").appendTo(_this);22: }23: _this.css({marginTop:0});24: });25: }26: //鼠标事件绑定27: _this.hover(function(){28: clearInterval(timerID);29: },function(){30: timerID=setInterval("scrollUp()",timer);31: }).mouseout();32: }33: })34: })(jQuery);35:36: $(document).ready(function(){37: $("#scrollDiv").Scroll({line:4,speed:500,timer:3000});38: });</script>
</head>
<body>
<p>多行滚动演示:</p>
<div id="scrollDiv">
<ul>
<li>这是公告标题的第一行</li>
<li>这是公告标题的第二行</li>
<li>这是公告标题的第三行</li>
<li>这是公告标题的第四行</li>
<li>这是公告标题的第五行</li>
<li>这是公告标题的第六行</li>
<li>这是公告标题的第七行</li>
<li>这是公告标题的第八行</li>
</ul>
</div>
</body>
</html>
三:用setInterval函数实现
<ul id="aaa">
<li>这是公告标题的第一行</li>
<li>这是公告标题的第二行</li>
<li>这是公告标题的第三行</li>
<li>这是公告标题的第四行</li>
<li>这是公告标题的第五行</li>
<li>这是公告标题的第六行</li>
<li>这是公告标题的第七行</li>
<li>这是公告标题的第八行</li>
</ul>
<script>1:2:3: setInterval("aaa.appendChild(aaa.firstChild)",500);4:</script>
附注:一个扩展
jQuery.fn.extend({ justUp : function(aSpeed,duration) {this.css({"height":"25px","line-height":"25px"});
this.each(function(){
var _this = this;
var iTimer=0; function scroll(){ iTimer = setInterval(function(){ $(_this).animate({marginTop:"-25px"},aSpeed||500,function(){$(_this).css({marginTop:"0px"}).find("li:first").appendTo(_this);});},duration||3000);
}
scroll();
$(this).hover(function(){this.style.cursor = "pointer";clearInterval(iTimer);},scroll);
});
}
});
水平滚动
// 水平滚动插件(function($){$.fn.extend({ Scroll:function(opt,callback){ //参数初始化if(!opt) var opt={};
var _btnRight = $("#"+ opt.right);//Shawphy:向右按钮
var _btnLeft = $("#"+ opt.left);//Shawphy:向左按钮
var timerID;var _this=this.eq(0).find("ul:first");
var lineW=_this.find("li:first").width(), //获取列表宽度
line=opt.line?parseInt(opt.line,10):parseInt(this.width()/lineW,10), //每次滚动的行数,默认为一屏,即父容器高度
speed=opt.speed?parseInt(opt.speed,10):500; //卷动速度,数值越大,速度越慢(毫秒) timer=opt.timer //?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒) if(line==0) line=1; var rightWidth=0-line*lineW; //滚动函数var scrollRight=function(){
_btnRight.unbind("click",scrollRight); //Shawphy:取消向右按钮的函数绑定
_this.animate({marginLeft:rightWidth
},speed,function(){ for(i=1;i<=line;i++){ _this.find("li:first").appendTo(_this);}
_this.css({marginLeft:0});_btnRight.bind("click",scrollRight); //Shawphy:绑定向右按钮的点击事件
});
}
//Shawphy:向左翻页函数var scrollLeft=function(){
_btnLeft.unbind("click",scrollLeft); for(i=1;i<=line;i++){ _this.find("li:last").show().prependTo(_this);}
_this.css({marginLeft:rightWidth}); _this.animate({marginLeft:0
},speed,function(){ _btnLeft.bind("click",scrollLeft);});
}
//Shawphy:自动播放var autoPlay = function(){