[前端] HTML n种方式实现隔行变色的示例代码

2620 0
黑夜隐士 2022-10-19 15:42:56 | 显示全部楼层 |阅读模式
本文主要介绍了HTML n种方式实现隔行变色的示例代码,分享给大家,具体如下:
n种方式实现隔行变色
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>n种方式实现隔行变色</title>
  8.     <style>
  9.         .box {
  10.             margin: 20px auto;
  11.             width: 300px;
  12.         }
  13.         .box li {
  14.             line-height: 35px;
  15.             border-bottom: 1px dashed #AAA;
  16.             padding: 0 5px;
  17.             text-overflow: ellipsis;
  18.             white-space: nowrap;
  19.             overflow: hidden;
  20.             cursor: pointer;
  21.         }
  22.         /* 1.css3第一种方式 */
  23.         /* .box li:nth-of-type(3n+1){
  24.             background-color: green;
  25.         }
  26.         .box li:nth-of-type(3n+2){
  27.             background-color: red;
  28.         }
  29.         .box li:nth-of-type(3n){
  30.             background-color: blue;
  31.         } */
  32.         /* //=> bgColor与ulList组合2种方式实现 */
  33.         /* .bgColorYellow {
  34.             background-color: yellow;
  35.         }
  36.         .bgColorRed {
  37.             background-color: red;
  38.         }
  39.         .bgColorBlue {
  40.             background-color: blue;
  41.         }  */
  42.         /* //=> bgColor与ulList组合1种方式实现 */
  43.         .bg0 {
  44.             background-color: lightcoral;
  45.         }
  46.         .bg1 {
  47.             background-color: lightgreen;
  48.         }
  49.         .bg2 {
  50.             background-color: lightskyblue;
  51.         }
  52.          #hover {
  53.            background-color: red;
  54.         }
  55.         /* 我们把hover放在bgn的后面,当元素的class=‘bgo’以bgo样式为主 */
  56.         .hover {
  57.            background-color: blueviolet;
  58.         }
  59.     </style>
  60. </head>
  61. <body>
  62.     <ul class="box" id="box">
  63.         <li>上次大家成都你cdsvdvd vax v a 杀虫水</li>
  64.         <li>撒差多少VCD深V上次的深V但是是的深V的深V是DVD深V的深V的深V是大Vsad深V是的v</li>
  65.         <li>大SAV吃撒撒发顺丰</li>
  66.         <li>萨芬从深V撒VCDVD深V是大V撒大V大是发大V是大V是哒但是啥的 </li>
  67.         <li>撒房产税才是</li>
  68.         <li>阿深V大SAV的在v</li>
  69.         <li>上次大家成都你cdsvdvd vax v a 杀虫水</li>
  70.         <!-- /*==利用css优先级搞定:默认背景颜色基于样式类完成,鼠标滑过的样式比样式类优先级高一些(ID
  71.         选择器/行内样式) -->
  72.     </ul>
  73.     <script>
  74.         //=>隔三行变色高亮选中::修改元素的class样式类
  75.             // 样式表: ID选择器
  76.             //        标签选择题
  77.             //        样式类选择器
  78.             //        行内样式
  79.             //        这些方式存在优先级的问题:行内,ID,样式类,标签...
  80.         // 方案:
  81.         //    1.依次遍历每一个li,通过索引除以3取余数,让当前的li有不同的背景色
  82.         //    2.第一种的技巧,告别一个个的判断,直接采用数组或者直接找到对应的样式的方式来完成
  83.         //    3.不是遍历每一个li,而是遍历每一组
  84.         var oBox = document.getElementById('box');
  85.         var ulList = oBox.getElementsByTagName('li');
  86.         //*高亮第3种方式:
  87.         for (var i=0; i<ulList.length; i++){
  88.                         ulList[i].className = 'bg'+ (i%3);
  89.                         //=>鼠标滑过:在原有样式类基础上累加一个hover样式类(由于hover在样式类中靠后,它的样式会覆盖原有的bg中的样式)
  90.                         //=>鼠标离开:把新增的hover样式类移除掉即可
  91.                         ulList[i].onmouseover = function (){
  92.                             this.className += 'hover'
  93.                         }
  94.                         ulList[i].onmouseout = function (){
  95.                            // this.className = 'bg0 hover'- 'hover';这不是字符串相减,这是数学运算结果是(NaN)
  96.                             this.className = this.className.replace('hover','');
  97.                         }
  98.                     }
  99.         //=>2.js第一种方式
  100.                 // for (var i = 0; i < ulList.length; i++) {
  101.                 //     //=> 分析:i=0 第一个li i%3=0
  102.                 //     //=> i=1 第一个li i%3=1
  103.                 //     //=>  i=2 第一个li i%3=2
  104.                 //     //=>  i=3 第一个li i%3=0
  105.                 //     var n=i%3; //当前循环找出来的那个li
  106.                 //     liColor=ulList[i];
  107.                     // if(n===0){
  108.                     //     liColor.style.backgroundColor='red';
  109.                     // }else if(n===1){
  110.                     //     liColor.style.backgroundColor='yellow';
  111.                     // }else {
  112.                     //     liColor.style.backgroundColor='pink';
  113.                     // }
  114.                     // }
  115.             //=>3.js第二种方式
  116.                     //  for (var i=0; i<ulList.length; i++){
  117.                     //      switch ( i % 3) {
  118.                     //           case 0:
  119.                     //            ulList[i].className = "bgColorYellow";
  120.                     //            break;
  121.                     //            case 1:
  122.                     //            ulList[i].className = "bgColorRed";
  123.                     //            break;
  124.                     //            case 2:
  125.                     //            ulList[i].className = "bgColorBlue";
  126.                     //            break;
  127.                     //      }
  128.                     //  }
  129.             //=>4.js第三种方式  colorArray+bgColorYellow...
  130.                //  var colorArray = ["bgColorYellow","bgColorRed", "bgColorBlue"];
  131.                // for (var i=0; i<ulList.length; i++){
  132.                      //=> 分析: i%3=0 "bgColorYellow" colorArray[0]
  133.                      //=>        i%3=1  "bgColorBlue"  colorArray[1]
  134.                      //=>        i%3=2  "bgColorRed"   colorArray[2]
  135.                      //=> i%3的余数是多少?就是我们当前需要到数组中通过此索引找到的样式类,而这个样式类则是当前li需要设置的class
  136.                    //       ulList[i].className = colorArray[i % 3];
  137.                  //   }
  138.             //=>5.js第四种方式
  139.                     // for (var i=0; i<ulList.length; i++){
  140.                     //     ulList[i].className = 'bg'+ (i%3); //=>隔三行变色修改样式类
  141.                     //     //=>在改变之前把原有的样式类信息存储到自定义属性中
  142.                     //     ulList[i].myOldClass= ulList[i].className;
  143.                     //     ulList[i].onmouseover = function () {
  144.                     //         // 高亮选中:
  145.                     //         //this:当前操作的元素
  146.                     //         //=>第一种方法
  147.                     //                 //  this.style.background = 'yellow';
  148.                     //          //=>第二种方法
  149.                     //                 // this.id = 'hover';
  150.                     //         //=>第三种方法
  151.                     //           //=>设置新样式之前把原有的样式保存起来,this:当前操作的元素也是一个元素对象,有很多内置属性,我们设置一个自定义属性,把原有的样式类信息存储进来
  152.                     //           console.dir(this);
  153.                     //                //=>滑过,简单粗暴的让class等于hover
  154.                     //                  this.className = 'hover';
  155.                     //             }
  156.                     //             ulList[i].onmouseout = function() {
  157.                     //                // this.style.background = '';
  158.                     //                //    this.id = '';
  159.                     //                //=>离开:让其还原为原有的样式(原有的样式可能是bg0,bg1,bg2)
  160.                     //                this.className=this.myOldClass;
  161.                     //             }
  162.                     // }
  163.            //=>6.js第五种方式三元运算符三种写法  
  164.                      //=>第一种写法
  165.                         // function changeColor() {
  166.                         //     for (var i = 0 ; i< ulList.length; i++){
  167.                         //         ulList[i].style.backgroundColor = i % 3 == 0 ? 'lightblue': ((i%3 ==1)?'lightgreen':'lightpink');
  168.                         //     }
  169.                         // }
  170.                         // changeColor();
  171.                      //=>第二种写法
  172.                         // for (var i = 0; i < ulList.length; i++) {
  173.                         //     var n = i % 3;
  174.                         //     liColor = ulList[i];
  175.                         //        //=>以下两种都可以
  176.                         //             // n === 0 ? liColor.style.backgroundColor = 'red' : (n === 1 ? liColor.style.backgroundColor = 'yellow' :
  177.                         //           //     liColor.style.backgroundColor = 'pink')
  178.                         //=>第三种写法
  179.                         //     n === 0 ? liColor.style.backgroundColor='red': n === 1 ?liColor.style.backgroundColor='yellow' : liColor.style.backgroundColor='blue';
  180.                         // }
  181.              //=>7.js第六种方式
  182.                  //=>我们每一组循环一次,在循环体中,我们把当前这一组样式都设置好即可(可能出现当前这一组不够3个,就报错了)
  183.                     // var max = ulList.length - 1;
  184.                     // for (var i=0;i<ulList.length;i+=3){
  185.                     //     ulList[i].style.background='bg0';
  186.                     //     i + 1 <= max ? ulList[i+1].style.background='bg1':null;
  187.                     //     i + 2 <= max ? ulList[i+2].style.background='bg2':null;
  188.                     // }
  189.     </script>
  190. </body>
  191. </html>
复制代码
运行效果如下:


到此这篇关于HTML n种方式实现隔行变色的示例代码的文章就介绍到这了,更多相关HTML隔行变色内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行