在js中动态不同条件引入不同的css样式文件封装方法

一、封装方法

function loadStyles(url) {
       var link = document.createElement("link");
       link.rel = "stylesheet";
       link.type = "text/css";
       link.href = url;
       var head = document.getElementsByTagName("head")[0];
       head.appendChild(link);
}
//调用:
loadStyles("framework/css/blue.css");

二、代码实现 

点击颜色卡确认时候

//主题颜色
let themColor='#0080ff'

// 主题色选择
layui.use('colorpicker', function(){
   var colorpicker = layui.colorpicker;
   //渲染
   colorpicker.render({
      elem: '#test1',  //绑定元素
      predefine: true,
      color:'#3d8ddb',
      colors: ['#0080FF','#ff0000','#333333','#438eb9','#189a5b','#026a52','#ff6a00'],
      done: function(color){
        // chuandiColor(color)
        if(color=='#0080ff'){
          loadStyles('./css/page-inblue.css')
        }
        if(color=='#ff0000'){
          loadStyles('./css/page-red.css')
        }
        if(color=='#333333'){
          loadStyles('./css/page-black.css')
        }
        if(color=="#438eb9"){
          loadStyles('./css/page-elegant.css')
        }
        if(color=="#189a5b"){
          loadStyles('./css/page-green.css')
        }
        if(color=="#026a52"){
          loadStyles('./css/page-ycgreen.css')
        }
        if(color=="#ff6a00"){
          loadStyles('./css/page-orange.css')
        } 
      }
    }); 
});

// 换肤
function loadStyles(url){
        var link = document.createElement("link");
        link.rel = "stylesheet";
        link.type = "text/css";
        link.href = url;
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(link);
  }

效果展示:

 


版权声明:本文为slom_fxt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。