vue实现换肤功能

elementui+less实现换肤功能效果图

iview+scss实现换肤功能效果图 

一:用elementui+less实现换肤功能

        1.新建color.less文件和theme.less文件,放置到assets/less文件夹中        

        

        2.color.less文件文件代码如下:        

@import url('./theme.less');
.themea {
    .theme();//默认的样式
}
.themeb {
    .theme(#409EFF,#fff);
}
.themec {
    .theme(#909399,#fff);
}

         3.theme.less文件文件代码如下:

.theme(@backcolor:#EEA2AD,@fcolor:#fff) {
    .header {
        color: @fcolor;
        background: @backcolor;
        width: 100%;
        height: 50px;
        position: relative;
        h4 {
            width: 100%;
            text-align: center;
            line-height: 50px;
            font-size: 15px;
        }
    }
}

        4.将color.less文件引入到main.js文件中

import "./assets/less/color.less";

        5.页面代码如下:

<template>
  <div class="skin-change">
    <div id="app">
      <div class="header">
        <h4>我是Header</h4>
      </div>
    </div>
    <div class="skin-button">
      <el-dropdown class="colorBtn" trigger="click" @command="changeColor">
        <span class="el-dropdown-link">换肤</span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="themea" @click="change_type(themea)">
            梦幻粉
          </el-dropdown-item>
          <el-dropdown-item command="themeb" @click="change_type(themeb)">
            天空蓝
          </el-dropdown-item>
          <el-dropdown-item command="themec" @click="change_type(themec)">
            雾霾灰
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  data() {
    return {};
  },
  created() {},
  mounted() {
    if(localStorage.getItem('app')) {
      this.changeColor(localStorage.getItem('app'));
    } else {
      this.changeColor("themea");
    }
  },
  methods: {
    changeColor(command) {
      document.getElementById('app').className = command;
      this.localStorageDate();
    },
    localStorageDate(){
      localStorage.setItem('app',document.getElementById('app').className)
    }
  },
};
</script>

<style lang="less" scoped>
.skin-change {
  width: 100%;
  position: relative;
  .skin-button {
    position: absolute;
    right: 15px;
    top: 15px;
    .el-dropdown-link {
      display: inline-block;
      width: 50px;
      background: #fff;
    }
  }
}
</style>

二:用iview+scss实现换肤功能

        1.新建theme.scss文件,放置到assets/scss文件夹中

        

        2.theme.scss文件代码如下:

@charset "utf-8";
@mixin theme($backColor:#F56C6C,$fZColor:#fff){
    .commonBackColor {
        background: $backColor;
        color:$fZColor;
    }
}
.themea {
    @include theme(#F56C6C, #fff);
}
.themeb{
    @include theme(#E6A23C, #fff);
}
.themec{
    @include theme(#67C23A, #fff);
}

         3.将theme.scss引入到main.js文件中

import "./assets/scss/theme.scss";

         4.页面代码如下:

<template>
  <div>
    <div id="bookcaselist">
      <Dropdown>
        <a href="javascript:void(0)">
          一键换肤
          <Icon type="arrow-down-b"></Icon>
        </a>
        <DropdownMenu slot="list">
          <DropdownItem @click.native="changeTheme('themea')">
            红色
          </DropdownItem>
          <DropdownItem @click.native="changeTheme('themeb')">
            黄色
          </DropdownItem>
          <DropdownItem @click.native="changeTheme('themec')">
            绿色
          </DropdownItem>
        </DropdownMenu>
      </Dropdown>
    </div>
    <div id="main">
      <div class="commonBackColor header">
        <h4>我是Header</h4>
      </div>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  data() {
    return {
      title: '我的',
      showBack: false
    };
  },
  created() {},
  mounted() {
    this.changeTheme("themea");
  },
  methods: {
    changeTheme (theme) {
      document.getElementById('main').className = theme;
    },
  },
};
</script>

<style scoped>
p {
  cursor: pointer;
}
.header {
  height: 50px;
  line-height: 50px;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值