vue+iview 实现项目中一件切换主题

19 篇文章 0 订阅
<Button @click="changetheme(0,$event)" type="primary">主题</Button>
<Button @click="changetheme(1,$event)" type="primary">主题</Button>
<Button @click="changetheme(2,$event)" type="primary">主题</Button>
const colorList = ['default','red','green']; //全局变量
Window.themeColor=colorList[0];       //写入Window对象
changetheme(colortype,e){
    e.stopPropagation();
    localStorage.setItem('themeColor',colortype);    //保存使用主题色
    document.body.classList.remove(window.cssStyle); //去除已有主题色
    window.cssStyle=colorList[Number(colortype)];    //获取新的主题色
    document.body.classList.add(window.cssStyle);    //body添加主题色的class

    let themeColor = localStorage.getItem('themeColor'); // 判断是否已存在使用的皮肤
    if (themeColor) {
        window.cssStyle=colorList[Number(themeColor)];
    }else {
       localStorage.setItem('themeColor','0');  // 不存在则储存一个默认的主题色
    }
    document.body.classList.add(window.cssStyle);  //body 添加less中主题色的class
},

引入资源

<style lang="less" scoped>

@import '../theme.less';

<style>

主题文件

//theme.less 文件

@theme-color : #226CDC; //默认主题色
@red-theme-color:#fe2419;
@green-theme-color:green;
.header-style(@theme-color:@red-theme-color){  // less函数
  button {
    background: @theme-color;
  }
}
//调用函数生成相应的样式
.header-style();  // 默认主题色
 
.red{   // 这里需要单独添加一个class 用来换肤                           
    .header-style(@red-theme-color); // 红色
    button.ivu-btn-primary{
      background: @theme-color;
    }
  }
 
.green{
    .header-style(@green-theme-color); //绿色
    button.ivu-btn-primary{
      background: @green-theme-color;
    }
  }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值