基于jQuery的无缝滚动应用(单行或多行) -
时间:2010-09-03 来源:缘分星空
$(function(){
var _wrap=$('ul.line');//定义滚动区域
var _interval=2000;//定义滚动间隙时间
var _moving;//需要清除的动画
_wrap.hover(function(){
clearInterval(_moving);//当鼠标在滚动区域中时,停止滚动
},function(){
_moving=setInterval(function(){
var _field=_wrap.find('li:first');//此变量不可放置于函数起始处,li:first取值是变化的
var _=_field.height();//取得每次滚动高度(多行滚动情况下,此变量不可置于开始处,否则会有间隔时长延时)
_field.animate({marginTop:-_h+'px'},600,function(){//通过取负margin值,隐藏第一行
_field.css('marginTop',0).appendTo(_wrap);//隐藏后,将该行的margin值置零,并插入到最后,实现无缝滚动
})
},_interval)//滚动间隔时间取决于_interval
}).trigger('mouseleave');//函数载入时,模拟执行mouseleave,即自动滚动
});
演示代码:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name="author" content="Mr.Think青鸟" /> <meta name="keywords" content="Mr.Think,前端开发,WEB前端,前端技术,前端开发,WEB前端开发,用户体验,网站策划,网站优化,青鸟,javascript,jQuery,css,xhtml,html,UE,SEO,Mr.Think的博客,青鸟的博客,PHP爱好者,Bluebirdsky" /> <meta name="description" content="Mr.Think的博客,中文网名青鸟,现专注于WEB前端开发,同时也是一位PHP的爱好者.爱思考,有点代码洁癖,生吃过螃蟹腿,喜好肉食.这里是我记录知识与生活琐事的地方." /> <link rel="pingback" href="http://mrthink.net/xmlrpc.php" /> <link rel="alternate" type="application/rss+xml" title="Mr.Think的博客 RSS Feed" href="http://mrthink.net/feed/" /> <title>基于jQuery的上下无缝滚动应用(单行或多行)@Mr.Think</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <style> /*reset css*/ body{font-size:0.8em;letter-spacing:1px;font-family:"微软雅黑"; line-padding:0} h1{font-size:1em; font-weight:normal;} h1 a{background:#047; padding:2px 3px; color:#fff; text-decoration:none;} h1 a:hover{background:#a40000; color:#fff; text-decoration:underline} h3{color:#888; font-weight:bold;font-size:1em; margin:1em auto; position:relative} /*demo css*/ h2{background:#a40000; font-size:12px; color:#fff; font-weight:normal; text-align:center; width:100px; line- margin:30px 0 0 20px} ul.line,ul.mulitline{width:500px; background:#eee; overflow:hidden;margin-bottom:20px;border:2px solid #a40000} ul.mulitline{height:90px} li{text-indent:15px; font-size:12px; line-list-style:none} </style> <script> /******************************* * @author Mr.Think * @author blog http://mrthink.net/ * @2010.08.08 * @可自由转载及使用,但请注明版权归属 *******************************/ $(function(){ //单行应用@Mr.Think var _wrap=$('ul.line');//定义滚动区域 var _interval=2000;//定义滚动间隙时间 var _moving;//需要清除的动画 _wrap.hover(function(){ clearInterval(_moving);//当鼠标在滚动区域中时,停止滚动 },function(){ _moving=setInterval(function(){ var _field=_wrap.find('li:first');//此变量不可放置于函数起始处,li:first取值是变化的 var _h=_field.height();//取得每次滚动高度 _field.animate({marginTop:-_h+'px'},600,function(){//通过取负margin值,隐藏第一行 _field.css('marginTop',0).appendTo(_wrap);//隐藏后,将该行的margin值置零,并插入到最后,实现无缝滚动 }) },_interval)//滚动间隔时间取决于_interval }).trigger('mouseleave');//函数载入时,模拟执行mouseleave,即自动滚动 }); $(function(){ //多行应用@Mr.Think var _wrap=$('ul.mulitline');//定义滚动区域 var _interval=3000;//定义滚动间隙时间 var _moving;//需要清除的动画 _wrap.hover(function(){ clearInterval(_moving);//当鼠标在滚动区域中时,停止滚动 },function(){ _moving=setInterval(function(){ var _field=_wrap.find('li:first');//此变量不可放置于函数起始处,li:first取值是变化的 var _h=_field.height();//取得每次滚动高度 _field.animate({marginTop:-_h+'px'},600,function(){//通过取负margin值,隐藏第一行 _field.css('marginTop',0).appendTo(_wrap);//隐藏后,将该行的margin值置零,并插入到最后,实现无缝滚动 }) },_interval)//滚动间隔时间取决于_interval }).trigger('mouseleave');//函数载入时,模拟执行mouseleave,即自动滚动 }); </script> </head> <body> <h1><a href="http://www.jzxue.com/">建站学</a> @刚加载完页面后看到不效果,需按F5在刷新下页面即可看到效果.</h1> <h3>返回文章页:<a href="http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/12-4306.html">基于jQuery的上下无缝滚动应用(单行或多行)</a></h3> <!--DEMO start--> <h2>单行应用</h2> <ul class="line"> <li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4287.html'>JavaScript产生随机字符和数字的函数</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4285.html'>jQuery Dialog 弹出层对话框插件</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4284.html'>jquery下操作json数据的代码</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/09-4275.html'>javascript常用正则表达式合集</a></li> </ul> <!--//jQ版本//--> <h2>多行应用</h2> <ul class="mulitline"> <li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/12-4306.html'>基于jQuery的无缝滚动应用(单行或多行)</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/12-4304.html'>javascript图片放大效果代码</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4293.html'>javascript模板系统 </a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4287.html'>JavaScript产生随机字符和数字的函数</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4285.html'>jQuery Dialog 弹出层对话框插件</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/10-4284.html'>jquery下操作json数据的代码</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/09-4275.html'>javascript常用正则表达式合集</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/08-4260.html'>JavaScript实现带自动提示的文本框效果代码</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/08-4259.html'>jquery autocomplete自动完成插件的的使用</a></li><li><a href='http://www.jzxue.com/wangzhankaifa/javascript-ajax/201008/07-4258.html'>基于jquery的商品展示放大镜</a></li> </ul> <!--//多行应用//--> <!--DEMO end--> </body> </html>
[ 提示:你可先修改部分代码,再按Ctrl+A 全部选择 ]
world cruise industry reviewmacco paint costredneck days christmas lyricsg-stardog friendly vacation rentalsfake designer bagsivy league vestqueensland vacationsoceanfront villaspictures of plants and flowersfamous still life paintingbobs furniturebackpack purseswatercolor famous artistsdior couture fall 2009fresh produce bagsvintage dish towelsteacher personalized stampsN52001italian glovesogio laptop bagorganic cotton baggo travel holidayscar paint carecarnival cruise bermudabook a carnival cruisethe catch spring summer autumn wintermonogrammed lunch bagchildren bomber jacketbirthstones beadsreplica handbags reviewstying bowline knotmeditation prayer shawlnorwegian spirit reviews 2008mini backpack pursesvintage leather motorcycle jacketChanel-5619-BLACKdamier papillonoriginal canvas paintingscarnival cruise specialsversace biographycarnival conquest cruisemexican words of the day jokesbeach house motel floridawatercolor painting step by steptrendy luggageautomotive upholstery cleaningncl transatlantic cruisesapartments and homes for rentchristmas vacation tv 2009cruise boat workauthentic christian dior handbagnike travel luggageblack luggagebeach properties of florida real estatebags offerscheap cruise royal caribbeanbest half chapsyamaha motorcycle saddlebagcar back carrieroversized leather hobocheap upssearchlight newspaper st vincent grenadinessouthwest rentalgrenadines resortsdesigner purses coachbahamas fun day cruisehot fashion shoestanning coyote hideskaanapali beach hotelsroberto cavalli 2006 collectionocean beach hotelitalian all inclusive vacationsspring summer autumn winter spring torrentwholesale softball equipmentthings do before cruisemichael kors astor handbagsoversized modern artdisney cruise itinerariessinger serger sewing machinesholland america alaska cruise reviewpablo picasso itemsfamily cruises caribbeanlovie vittonfendi storesM91996holland america world cruise 2008chez damier help myselfbeach front house rentalsladies winter glovesjessica simpson travel bagpersonalized beach accessoriesbest rolling luggageaudi a5 roof rackfake gucci wallets mengucci-189588-03apple vacationnylon pursedesigner handbags replica bagsdecals sticker