欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中使用Scss實(shí)現(xiàn)配置、切換主題方式

 更新時(shí)間:2024年03月11日 14:47:04   作者:明天也要努力  
這篇文章主要介紹了Vue中使用Scss實(shí)現(xiàn)配置、切換主題方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

樣式文件目錄介紹

本項(xiàng)目中的公共樣式文件均位于 src/assets/css 目錄下,其中 index.scss是總的樣式文件的匯總?cè)肟?nbsp;

common.scss 是供全局使用的一些基本樣式(常量)

  • _theme.scss
  • _handle.scss 兩個(gè)文件是進(jìn)行主題顏色配置的文件

如下圖:

將 index.scss 在 main.js 文件中引入即可全局使用。

主題 scss 文件配置

src/assets/css 目錄下的 _themes.scss,里面可配置不同的主題配色方案

本文配置了兩個(gè)主題顏色:light、dark

@import './common.scss';
$themes: (
  light: (
    bg-color: $white,
    font-color: $regularBlack,
    link-color: $grey,
    icon-home: url('~@/assets/img/icon/lightHomeIcon.svg'),
    icon-filter: url('~@/assets/img/icon/lightFilter.png'),
  ),
  dark: (
    bg-color: $black,
    font-color: $white,
    link-color: $blue,
    icon-home: url('~@/assets/img/icon/darkHomeIcon.svg'),
    icon-filter: url('~@/assets/img/icon/darkFilter.png'),
  )
)

src/assets/css 目錄下的 _handle.scss 用來(lái)操作上述 _themes.scss 中 $theme 的變量

_handle.scss 文件內(nèi)容:

@import "./_themes.scss";

// 從主題色map中取出對(duì)應(yīng)顏色
@function themed($key) {
  @return map-get($theme-map, $key);
}

// 切換主題時(shí) 獲取不同的主題色值
@mixin themeify {
  @each $theme-name,$theme-map in $themes {
    // !global 把局部變量強(qiáng)升為全局變量
    $theme-map: $theme-map !global;
    // 判斷html的data-theme的屬性值  #{}是sass的插值表達(dá)式
    // & sass嵌套里的父容器標(biāo)識(shí)   @content是混合器插槽,像vue的slot
    [data-theme="#{$theme-name}"] & {
      @content;
    }
  }
}


// 獲取背景顏色
@mixin background_color($color) {
  @include themeify {
    background-color: themed($color) !important;
  }
}

// 獲取背景圖片
@mixin background_image($color) {
  @include themeify {
    background-image: themed($color) !important;
  }
}

// 獲取圖片
@mixin content($color) {
  @include themeify {
    content: themed($color) !important;
  }
}

// 獲取字體顏色
@mixin font_color($color) {
  @include themeify {
    color: themed($color) !important;
  }
}

// 獲取邊框顏色
@mixin border_color($color) {
  @include themeify {
    border-color: themed($color) !important;
  }
}

組件中使用

樣式文件都配置完成了,怎么設(shè)置當(dāng)前需要使用的主題呢 ?

此處具體情況具體分析,在合適的頁(yè)面或位置寫入即可,本文是寫在了 App.vue 項(xiàng)目入口文件中,通過(guò)

window.document.documentElement.setAttribute();

方法傳入當(dāng)前想要使用的主題。本文默認(rèn)傳入的 ‘light’,則 vue 頁(yè)面中使用的即為 _theme.scss 中的 light 對(duì)象下配置好的顏色或者其他屬性;

設(shè)置其他主題色(如:dark、blue)同理,前提是 _theme.scss 文件中存在配置好的對(duì)應(yīng)主題樣式;

// App.vue
<template>
  <div id="app">
    <div class="fun">
      <el-switch
        v-model="switchVal"
        active-color="#2c2c2c"
        inactive-color="#e8e4e4"
        @change="switchChange">
      </el-switch>
    </div>
    <el-menu 
      :default-active="activeIndex" 
      mode="horizontal"
      text-color="#fff"
      background-color="#545c64"
      active-text-color="#ffd04b" 
      @select="handleSelect">
      <el-menu-item index="/home">
        主頁(yè)
      </el-menu-item>
      <el-submenu index="1">
        <template slot="title">圖表</template>
        <el-menu-item index="/charts">折線圖</el-menu-item>
      </el-submenu>
      <el-submenu index="2">
        <template slot="title">表格</template>
        <el-menu-item index="/table">普通表格</el-menu-item>
        <el-menu-item index="/dynamicTable">動(dòng)態(tài)表格</el-menu-item>
      </el-submenu>
    </el-menu>

    <router-view/>
  </div>
</template>

<script>
export default {
  data(){
    return {
      switchVal: false,
      activeIndex: '/home',
    }
  },
  methods:{
    switchChange(val){
      if(val){
        window.document.documentElement.setAttribute('data-theme', "dark");
      }else{
        window.document.documentElement.setAttribute('data-theme', "light");
      }
    },
    handleSelect(key, keyPath) {
      this.$router.push(key)
    }
  },
  mounted(){
    this.switchChange(this.switchVal);
  }
}
</script>

<style lang="scss">
// 引入主題配置文件
@import "@/assets/css/_handle.scss";
#app {
  height: 100vh;
  text-align: center;
  @include background_color('bg-color');
  // background_color 為 _handle.scss 文件中配置好的屬性,傳入'bg-color'即可通過(guò)當(dāng)前的主題配置在 _theme.scss 文件中取色。 
  .fun{
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding: 5px;
    box-sizing: border-box;
  }
}
</style>

home.vue 中同理

<style lang="scss" scoped>
@import "@/assets/css/_handle.scss";
.home{
  text-align: center;
  @include font_color('font-color');
  .homeIcon{
    width: 14px;
    height: 14px;
    margin-right: 5px;
    display: inline-block;
    background-size: 100% 100%;
    @include background_image('icon-home');
  }
  a{
    @include font_color('link-color');
  }
}
</style>

效果

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 從vue基礎(chǔ)開始創(chuàng)建一個(gè)簡(jiǎn)單的增刪改查的實(shí)例代碼(推薦)

    從vue基礎(chǔ)開始創(chuàng)建一個(gè)簡(jiǎn)單的增刪改查的實(shí)例代碼(推薦)

    這篇文章主要介紹了從vue基礎(chǔ)開始創(chuàng)建一個(gè)簡(jiǎn)單的增刪改查的實(shí)例代碼,需要的朋友參考下
    2018-02-02
  • vue項(xiàng)目實(shí)戰(zhàn)總結(jié)篇

    vue項(xiàng)目實(shí)戰(zhàn)總結(jié)篇

    離放假還有1天,今天小編抽空給大家分享前端時(shí)間小編做的vue項(xiàng)目,非常完整,需要的朋友參考下
    2018-02-02
  • VUE中如何實(shí)現(xiàn)阻止事件冒泡

    VUE中如何實(shí)現(xiàn)阻止事件冒泡

    這篇文章主要介紹了VUE中如何實(shí)現(xiàn)阻止事件冒泡,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue中跨域及打包部署到nginx跨域設(shè)置方法

    Vue中跨域及打包部署到nginx跨域設(shè)置方法

    這篇文章主要介紹了Vue中跨域以及打包部署到nginx跨域設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • vue.js或js實(shí)現(xiàn)中文A-Z排序的方法

    vue.js或js實(shí)現(xiàn)中文A-Z排序的方法

    下面小編就為大家分享一篇vue.js或js實(shí)現(xiàn)中文A-Z排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Vue3?全局實(shí)例上掛載屬性方法案例講解

    Vue3?全局實(shí)例上掛載屬性方法案例講解

    這篇文章主要介紹了Vue3?全局實(shí)例上掛載屬性方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • mpvue開發(fā)音頻類小程序踩坑和建議詳解

    mpvue開發(fā)音頻類小程序踩坑和建議詳解

    這篇文章主要介紹了mpvue開發(fā)音頻類小程序踩坑和建議詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • vue中axios封裝使用的完整教程

    vue中axios封裝使用的完整教程

    這篇文章主要給大家介紹了關(guān)于vue中axios封裝使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 對(duì)VUE中的對(duì)象添加屬性

    對(duì)VUE中的對(duì)象添加屬性

    今天小編就為大家分享一篇對(duì)VUE中的對(duì)象添加屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vite+vue3搭建的工程實(shí)現(xiàn)批量導(dǎo)入store的module

    vite+vue3搭建的工程實(shí)現(xiàn)批量導(dǎo)入store的module

    這篇文章主要介紹了vite+vue3搭建的工程實(shí)現(xiàn)批量導(dǎo)入store的module方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評(píng)論