Home > Web Front-end > JS Tutorial > jQuery implements simulated marquee label effect_jquery

jQuery implements simulated marquee label effect_jquery

WBOY
Release: 2016-05-16 15:50:20
Original
1223 people have browsed it

Marquee

Imitate the marquee effect under IE, move the mouse up to pause. The main principle of forming a ring is that once each picture is judged to be outside the display window, it is added to the end, and append and prepend are used to simulate the push() and shift() of the array.

The code is as follows:

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>

Copy after login

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;
}

Copy after login

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')&#63; 1 : -1;
       var lists=$(boxWindow).children('li');
       var len=$(lists).length;
       var boxWidth=$(lists[0]).width();
       var timer;
       var step=(this.defaults.direction=='left')&#63; 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);

Copy after login

For detailed download, please see https://github.com/codetker/myMarquee

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template