javascript - 有个html轮换样式的需求,不知道怎么用js实现,求解。

 ERIK又 发布于 2022-11-11 08:31

是这样的,我要在li中不停的轮换样式,求实现方法,我不知道怎么处理超过第4个时补到最前面的实现,求指点!
循环开始时 currentIndex = 0 (指针),itemLens = 4(数据条数)
1.第一次循环
if(currentIndex >= itemLens){

currentIndex = 0

}





  • currentIndex++

    2.第二次循环





  • currentIndex++

    3.第三次循环





  • currentIndex++

    4.第三次循环





  • currentIndex++

    补充一下框架代码

    
    
    
        
        
        
        Document
    
    
    

    3 个回答
    • 殊途同归:

      $(function(){
      
          setInterval(function(){
      
              var $items = $('.farm-current li');
          
              $items.last().insertBefore($items.first());
          
          }, 1000);
      
      })
      2022-11-12 15:21 回答
    • <!doctype html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport"
                content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
      </head>
      <style>
          .li-01,.li-02,.li-03,.li-04{ width:100px; height:50px;}
          .li-01{ background:greenyellow;}
          .li-02{ background:deeppink;}
          .li-03{ background:lightsalmon;}
          .li-04{ background:indigo;}
      </style>
      <body>
      <p class="farm-current">
          <ul>
              <li class="li-01"></li>
              <li class="li-02"></li>
              <li class="li-03"></li>
              <li class="li-04"></li>
          </ul>
      </p>
      <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js?1.2.1"></script>
      <script>
          $(function(){
              var timer = null;
              var $items = $('.farm-current').find('li');
              var arr = ['li-01', 'li-02', 'li-03', 'li-04'];
              timer = setInterval(function(){
                  move($items)
              }, 1000);
      
              function move($items){
                  var temp = arr.pop();
                  arr.unshift(temp);
                  $items.each(function(index){
                      $(this).removeClass().addClass(arr[index]);
                  });
              }
          })
      </script>
      </body>
      </html>
      2022-11-12 15:21 回答
    • 我水平不高,代码写的不好,但是我觉这应该是你要效果

      <!doctype html>
      <html lang="en">
      
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
          <style>
              .li-01 {
                  color: red;
              }
              .li-02 {
                  color: green;
              }
              .li-03 {
                  color: blue;
              }
              .li-04 {
                  color: yellow;
              }
      
          </style>
      </head>
      
      <body>
          <p class="farm-current">
              <ul>
                  <li>aaa</li>
                  <li>bbb</li>
                  <li>ccc</li>
                  <li>ddd</li>
              </ul>
          </p>
          <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js?1.2.1"></script>
          <script>
          $(function() {
              var timer = null;
              var $items = $('.farm-current').find('li');
              var itemsLen = $items.length;
              var arr = ['li-01', 'li-02', 'li-03', 'li-04'];
              var currentIndex = 0;
              timer = setInterval(function() {
                  move($items)
              }, 1000);
      
              // init class
              function pushClass() {
                  $.each($items, function(index, ele) {
                      $(this).addClass(arr[index]);
                  }); 
              }
      
              function move() {
                  var temp = arr.pop();
                  arr.unshift(temp);
                  $.each($items, function(index, ele) {
                      $(this).removeClass().addClass(arr[index]);
                  }); 
              }
              pushClass();
          });
          </script>
      </body>
      
      </html>
      
      2022-11-12 15:21 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有