源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] jQuery实现模拟marquee标签效果

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-12-25 05:22:13 | 显示全部楼层 |阅读模式
这篇文章主要介绍了jQuery实现模拟marquee标签效果的相关资料,需要的朋友可以参考下

Marquee

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

      代码如下:

HTML

<!doctype html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="" name="keywords" />
<meta content="" name="description" />
<meta name="author" content="codetker" />
<head>
<title>模拟marquee标签效果的简单实现</title>
<link href="style/reset.css" rel="stylesheet" type="text/css">
<link href="style/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery.codetker.marquee.js"></script>
</head>

<body>
  <div class="wrap">
    <div class="marquee">
      <ul>
        <li>
          <a href="" title="">1
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">2
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">3
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">4
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">5
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">6
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">7
            <img src="images/test.jpg" alt="">
          </a>
        </li>
        <li>
          <a href="" title="">8
            <img src="images/test.jpg" alt="">
          </a>
        </li>
      </ul>
    </div>
  </div>
<script type="text/javascript">
  $(document).ready(function(){
    $(".marquee").marquee();
  });
</script>
</body>
</html>

CSS

@charset "utf-8";
/* CSS Document */
body{
  margin:0 0;
  padding:0 0;
  height:100%;
  width:100%;
}
.wrap{
  font-family:"微软雅黑","宋体", Times, "Times New Roman", serif;
  font-size:14px;
  margin:0 0;
  padding:0 0;
  height:100%;
  width:100%;
  overflow:hidden;
}
.marquee{
  margin: 0 auto;
  width: 960px;
  height: 300px;
  overflow: hidden;
}
.marquee ul{
  width: 10000px;
}
.marquee ul li{
  float: left;
  width: 500px;
  text-align: center;
}
.marquee ul li a{

}
.marquee ul li a:hover{
  color: red;
}

JavaScript

/*
 * boxScroll 0.1
 * 兼容IE8,FF,Chrome等常见浏览器
 */
 ;(function($,window,document,undefined){
   //定义构造函数
   var BoxObj=function(ele,opt){
     this.$element=ele; //最外层对象
     this.defaults={
       'style': 0 ,//滚动样式选择,默认为普通效果
       'speed': 1 ,//默认为1s
       'direction': 'left'//默认为向左边滚动
     },
   
     this.options=$.extend({},this.defaults,opt );
     //这里可以添加一些通用方法  
   }

   //给构造函数添加方法
   BoxObj.prototype={

     commonScroll:function(){
       //接收对象属性
       var obj=this.$element;
       var boxWindow=$(this.$element).children('ul');
       var speed=this.defaults.speed;
       var style=this.defaults.style;
       var direction=(this.defaults.direction=='left')? 1 : -1;
       var lists=$(boxWindow).children('li');
       var len=$(lists).length;
       var boxWidth=$(lists[0]).width();
       var timer;
       var step=(this.defaults.direction=='left')? 0 : boxWidth;

       function move(style,speed,direction){
         if (style==0) {
           if (direction==1) {
             step+=1;
             if(step>boxWidth){
               step-=boxWidth;
               $(boxWindow).append($(boxWindow).children().eq(0));//将第一项放在最后,相当于push(0),shift()
             }else{
               $(obj).scrollLeft(step);
             }
           }else if (direction== -1) {
             step-=1;
             if(step<0){
               step+=boxWidth;
               $(boxWindow).prepend($(boxWindow).children().eq(len-1));//将最后一项放在最前,相当于pop(last),unshift()
             }else{
               $(obj).scrollLeft(step);
             }
           }else{//不执行之外的数值

           }
         }else{//留待扩展,多了改switch

         }
       }

       timer=setInterval(function(){
         move(style,speed,direction);
       },10*speed); //由于时间取得小,肉眼就看不出来

      $(lists).each(function() {//鼠标移上暂停
        $(this).hover(function() {
          clearInterval(timer);
        }, function() {
          clearInterval(timer);
          timer=setInterval(function(){
             move(style,speed,direction);
           },10*speed);
        });
      });
     }
       
   }

   $.fn.marquee=function(options){
     //创建实体
     var boxObj=new BoxObj(this,options);
     //用尾调的形式调用对象方法
     return boxObj.commonScroll();
   }
 })(jQuery,window,document);

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

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

回复

使用道具 举报

13

主题

2万

回帖

97

积分

注册会员

Rank: 2

积分
97
发表于 2022-9-16 00:04:00 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

59

积分

注册会员

Rank: 2

积分
59
发表于 2022-10-15 21:03:13 | 显示全部楼层
这个源码不错啊
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-3-30 19:42:23 | 显示全部楼层
刷刷刷刷刷刷刷刷刷刷刷刷刷刷刷
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2023-6-10 19:12:10 | 显示全部楼层
激动人心,无法言表!
回复 支持 反对

使用道具 举报

4

主题

2万

回帖

107

积分

注册会员

Rank: 2

积分
107
发表于 2023-6-28 02:29:15 | 显示全部楼层
源码源码源码源码源码源码源码源码源码源码源码源码源码
回复 支持 反对

使用道具 举报

13

主题

2万

回帖

85

积分

注册会员

Rank: 2

积分
85
发表于 2023-9-2 11:43:01 | 显示全部楼层
额头额定法国队是范德萨
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

50

积分

注册会员

Rank: 2

积分
50
发表于 2023-11-10 10:16:11 | 显示全部楼层
66666666666666666666
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

67

积分

注册会员

Rank: 2

积分
67
发表于 2023-12-4 18:10:19 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

16

主题

2万

回帖

174

积分

注册会员

Rank: 2

积分
174
发表于 2024-6-5 21:17:55 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-7 20:52 , Processed in 0.074181 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表