首页 > 语言 > JavaScript > 正文

jQuery实现模拟marquee标签效果

2024-05-06 16:23:07
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了jQuery实现模拟marquee标签效果的相关资料,需要的朋友可以参考下

Marquee

模仿IE下面的marquee效果,鼠标移上去暂停。形成环的主要原理在于每张图片一旦判断出了外面的显示窗口就添加到尾部,用append和prepend模拟数组的push()和shift()。

代码如下:

HTML

 

 
  1. <!doctype html> 
  2. <html> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <meta content="" name="keywords" /> 
  5. <meta content="" name="description" /> 
  6. <meta name="author" content="codetker" /> 
  7. <head> 
  8. <title>模拟marquee标签效果的简单实现</title> 
  9. <link href="style/reset.css" rel="stylesheet" type="text/css"> 
  10. <link href="style/style.css" rel="stylesheet" type="text/css"> 
  11. <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> 
  12. <script type="text/javascript" src="js/jquery.codetker.marquee.js"></script> 
  13. </head> 
  14.  
  15. <body> 
  16. <div class="wrap"> 
  17. <div class="marquee"> 
  18. <ul> 
  19. <li> 
  20. <a href="" title="">1 
  21. <img src="images/test.jpg" alt=""> 
  22. </a> 
  23. </li> 
  24. <li> 
  25. <a href="" title="">2 
  26. <img src="images/test.jpg" alt=""> 
  27. </a> 
  28. </li> 
  29. <li> 
  30. <a href="" title="">3 
  31. <img src="images/test.jpg" alt=""> 
  32. </a> 
  33. </li> 
  34. <li> 
  35. <a href="" title="">4 
  36. <img src="images/test.jpg" alt=""> 
  37. </a> 
  38. </li> 
  39. <li> 
  40. <a href="" title="">5 
  41. <img src="images/test.jpg" alt=""> 
  42. </a> 
  43. </li> 
  44. <li> 
  45. <a href="" title="">6 
  46. <img src="images/test.jpg" alt=""> 
  47. </a> 
  48. </li> 
  49. <li> 
  50. <a href="" title="">7 
  51. <img src="images/test.jpg" alt=""> 
  52. </a> 
  53. </li> 
  54. <li> 
  55. <a href="" title="">8 
  56. <img src="images/test.jpg" alt=""> 
  57. </a> 
  58. </li> 
  59. </ul> 
  60. </div> 
  61. </div> 
  62. <script type="text/javascript"> 
  63. $(document).ready(function(){ 
  64. $(".marquee").marquee(); 
  65. }); 
  66. </script> 
  67. </body> 
  68. </html> 

CSS

 

 
  1. @charset "utf-8"; 
  2. /* CSS Document */ 
  3. body{ 
  4. margin:0 0; 
  5. padding:0 0; 
  6. height:100%; 
  7. width:100%; 
  8. } 
  9. .wrap{ 
  10. font-family:"微软雅黑","宋体", Times, "Times New Roman", serif; 
  11. font-size:14px; 
  12. margin:0 0; 
  13. padding:0 0; 
  14. height:100%; 
  15. width:100%; 
  16. overflow:hidden; 
  17. } 
  18. .marquee{ 
  19. margin: 0 auto; 
  20. width: 960px; 
  21. height: 300px; 
  22. overflow: hidden; 
  23. } 
  24. .marquee ul{ 
  25. width: 10000px; 
  26. } 
  27. .marquee ul li{ 
  28. float: left; 
  29. width: 500px; 
  30. text-align: center; 
  31. } 
  32. .marquee ul li a{ 
  33.  
  34. } 
  35. .marquee ul li a:hover{ 
  36. color: red; 
  37. } 

JavaScript

 

 
  1. /* 
  2. * boxScroll 0.1 
  3. * 兼容IE8,FF,Chrome等常见浏览器 
  4. */ 
  5. ;(function($,window,document,undefined){ 
  6. //定义构造函数 
  7. var BoxObj=function(ele,opt){ 
  8. this.$element=ele; //最外层对象 
  9. this.defaults={ 
  10. 'style': 0 ,//滚动样式选择,默认为普通效果 
  11. 'speed': 1 ,//默认为1s 
  12. 'direction': 'left'//默认为向左边滚动 
  13. }, 
  14.  
  15. this.options=$.extend({},this.defaults,opt ); 
  16. //这里可以添加一些通用方法  
  17. } 
  18.  
  19. //给构造函数添加方法 
  20. BoxObj.prototype={ 
  21.  
  22. commonScroll:function(){ 
  23. //接收对象属性 
  24. var obj=this.$element; 
  25. var boxWindow=$(this.$element).children('ul'); 
  26. var speed=this.defaults.speed; 
  27. var style=this.defaults.style; 
  28. var direction=(this.defaults.direction=='left')? 1 : -1; 
  29. var lists=$(boxWindow).children('li'); 
  30. var len=$(lists).length; 
  31. var boxWidth=$(lists[0]).width(); 
  32. var timer; 
  33. var step=(this.defaults.direction=='left')? 0 : boxWidth; 
  34.  
  35. function move(style,speed,direction){ 
  36. if (style==0) { 
  37. if (direction==1) { 
  38. step+=1; 
  39. if(step>boxWidth){ 
  40. step-=boxWidth; 
  41. $(boxWindow).append($(boxWindow).children().eq(0));//将第一项放在最后,相当于push(0),shift() 
  42. }else{ 
  43. $(obj).scrollLeft(step); 
  44. } 
  45. }else if (direction== -1) { 
  46. step-=1; 
  47. if(step<0){ 
  48. step+=boxWidth; 
  49. $(boxWindow).prepend($(boxWindow).children().eq(len-1));//将最后一项放在最前,相当于pop(last),unshift() 
  50. }else{ 
  51. $(obj).scrollLeft(step); 
  52. } 
  53. }else{//不执行之外的数值 
  54.  
  55. } 
  56. }else{//留待扩展,多了改switch 
  57.  
  58. } 
  59. } 
  60.  
  61. timer=setInterval(function(){ 
  62. move(style,speed,direction); 
  63. },10*speed); //由于时间取得小,肉眼就看不出来 
  64.  
  65. $(lists).each(function() {//鼠标移上暂停 
  66. $(this).hover(function() { 
  67. clearInterval(timer); 
  68. }, function() { 
  69. clearInterval(timer); 
  70. timer=setInterval(function(){ 
  71. move(style,speed,direction); 
  72. },10*speed); 
  73. }); 
  74. }); 
  75. } 
  76.  
  77. } 
  78.  
  79. $.fn.marquee=function(options){ 
  80. //创建实体 
  81. var boxObj=new BoxObj(this,options); 
  82. //用尾调的形式调用对象方法 
  83. return boxObj.commonScroll(); 
  84. } 
  85. })(jQuery,window,document); 

详细下载见https://github.com/codetker/myMarquee

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选