一个轮播图小demo ---之js定时器的妙用

直接上demo

 html文件

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  <title>huadong</title>
    <link rel="stylesheet" type="text/css" href="style/huadong.css">
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
  <div class="out">
        <div class="wrap" id="wrap">
            <ul class="ye" id="ye">
              <li></li>
              <li></li>
              <li></li>
              <li></li>
            </ul>
        </div> 
        <ul class="dot">
              <li></li>
              <li></li>
              <li></li>
              <li></li>
        </ul>
        <div class="next"></div>
        <div class="pre"></div>   
  </div>


  <script type="text/javascript">
     var  ye =document.getElementById('ye');
     var  num =0;
     //var num1 =num /(-300);
     var  num1 =0;
     var time =null;
     var time_click = null;
    
/*     ye.style.left = 0;
     console.log(ye.style.left);*/
      function move(){
        ye.style.left =num+'px';
        num1 =num /(-300);
        $('.dot li').css('background-color','#999');
        $('.dot li').eq(num1).css('background-color','#000');
          num -= 300;
        if(num <= -1200){
          num =0;
        }
          console.log(num);
        }
        function zidong(){
          time = setInterval('move()',1000);
        }
      
         zidong();
      


      $('.dot li').on('click',function(){
          clearInterval(time);
           clearTimeout(time_click);
          var index =$(this).index();
          ye.style.left = (-300)*index+'px';
          $('.dot li').css('background-color','#999');
        $(this).css('background-color','#000');
         
        time_click= setTimeout('zidong()',0);
      });
      $('.pre').on('click',function(){
        clearInterval(time);
        clearTimeout(time_click);
         num -=300;
         if(num <= -1200){
          num =0;
        }
         ye.style.left = num + "px";
        num1 =num /(-300);
        $('.dot li').css('background-color','#999');
        $('.dot li').eq(num1).css('background-color','#000');
         
        time_click= setTimeout('zidong()',0);
      });
      $('.next').on('click',function(){
        clearInterval(time);
         clearTimeout(time_click);
         num +=300;
         if(num >0){
          num =-900;
        }
         ye.style.left = num + "px";
        num1 =num /(-300);
        $('.dot li').css('background-color','#999');
        $('.dot li').eq(num1).css('background-color','#000');
        
        time_click= setTimeout('zidong()',0);
      });
      $('.ye li').hover(function(){
        clearInterval(time);
      },function(){
       time = setInterval('move()',1000);
      });
  </script>
</body>
</html>

css文件

 /*@charset 'utf-8';*/
@charset "utf-8";


/************* Reset CSS *************/
/* 清除内外边距 */
html, body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, /* structural elements 结构元素 */
dl, dt, dd, ul, ol, li, /* list elements 列表元素 */
pre, /* text formatting elements 文本格式元素 */
form, fieldset,iframe, lengend, label, button, input, textarea, /* form elements 表单元素 */
th, td { /* table elements 表格元素 */
    margin: 0;
    padding: 0;
}


h1 ,h2 ,h3 ,h4, h5, h6 { font-size: 100%; }


address, cite, dfn, var { font-style: normal; } /* 将斜体扶正 */
code, kbd, pre, samp, tt { font-family: "Courier New", Courier, monospace; } /* 统一等宽字体 */
small { font-size: 12px; } /* 小于 12px 的中文很难阅读,让 small 正常化 */


/* 重置列表元素 */
ul, ol { list-style: none; }


/* 重置文本格式元素 */
a { text-decoration: none; }
a:hover { text-decoration: underline; }


/* 重置表单元素 */
legend { color: #000; } /* for ie6 */
fieldset, img, button{ border: none;}
/* 注:optgroup 无法扶正 */
button, input, select, textarea {
    font-size: 100%; /* 使得表单元素在 ie 下能继承字体大小 */
}


img, iframe, table, form{ vertical-align: top;}


/* 重置表格元素 */
table {
border-collapse: collapse;
border-spacing: 0;
}


/* 重置 hr */
hr {
    border: none;
    height: 1px;
}
textarea{resize:none}
button, input, select, textarea, label{outline:none}
/************* Reset CSS *************/




/*正文*****************************************************/
.ye li{
float:left;
width:300px;
height:300px;


}
li:nth-of-type(1){
background-color:yellow;
}
li:nth-of-type(2){
background-color:green;
}
li:nth-of-type(3){
background-color:blue;
}
li:nth-of-type(4){
background-color:red;
}


.wrap{
border:1px solid #999;
width:300px;
height:300px;
/*margin:30px auto 0;*/
overflow:hidden;
}
.ye{
width:1200px;
height:300px;
position:relative;
}
.out{
position:relative;
width:300px;
    margin:30px auto 0;
}
.dot,.next, .pre{
position:absolute;
}
.dot:after{
display: block;
clear:both;
content: '';
height:0px;
overflow:hidden;
}
.dot{
margin: 20px 0px 0 98px;
}
.dot li{
float:left;
height:10px;
width:10px;
border-radius: 50%;
background-color:#999;
margin-left: 5px;


}
.pre{
width: 50px;
    height: 50px;
    background-color: #999;
    top: 50%;
    right: -83px;
}
.next{
width: 50px;
    height: 50px;
    background-color: #999;
    top: 50%;
    left: -83px;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值