最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • nginx部署訪問(wèn)vue-cli搭建的項(xiàng)目的方法

    nginx部署訪問(wèn)vue-cli搭建的項(xiàng)目的方法

    本篇文章主要介紹了nginx部署訪問(wèn)vue-cli搭建的項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • vue?+?element-plus自定義表單驗(yàn)證(修改密碼業(yè)務(wù))的示例

    vue?+?element-plus自定義表單驗(yàn)證(修改密碼業(yè)務(wù))的示例

    這篇文章主要介紹了vue?+?element-plus自定義表單驗(yàn)證(修改密碼業(yè)務(wù)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-04-04
  • vue-cli配置文件——config篇

    vue-cli配置文件——config篇

    這篇文章主要介紹了vue-cli中的webpack是如何配置的,本篇文章主要是分析vue中關(guān)于config文件夾中的相關(guān)代碼,config的文件結(jié)構(gòu),感興趣的朋友參考下本文
    2018-01-01
  • Vue.js 點(diǎn)擊按鈕顯示/隱藏內(nèi)容的實(shí)例代碼

    Vue.js 點(diǎn)擊按鈕顯示/隱藏內(nèi)容的實(shí)例代碼

    下面小編就為大家分享一篇Vue.js 點(diǎn)擊按鈕顯示/隱藏內(nèi)容的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Vue中的v-model 和 :value 深度解析

    Vue中的v-model 和 :value 深度解析

    本文詳細(xì)解析了Vue中的v-model和:value兩種綁定方式的區(qū)別、使用場(chǎng)景以及最佳實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2026-01-01
  • Vue?配置代理詳情

    Vue?配置代理詳情

    這篇文章主要介紹了Vue?配置代理詳情,文章圍繞主題的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-04-04
  • 5分鐘學(xué)會(huì)Vue動(dòng)畫效果(小結(jié))

    5分鐘學(xué)會(huì)Vue動(dòng)畫效果(小結(jié))

    這篇文章主要介紹了5分鐘學(xué)會(huì)Vue動(dòng)畫效果(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • vue組件間通信解析

    vue組件間通信解析

    這篇文章主要為大家詳細(xì)介紹了vue組件間通信解析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定

    Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • vue中動(dòng)態(tài)組件使用及傳值方式

    vue中動(dòng)態(tài)組件使用及傳值方式

    這篇文章主要介紹了vue中動(dòng)態(tài)組件使用及傳值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評(píng)論

十堰市| 安达市| 南岸区| 桂平市| 饶河县| 新和县| 都匀市| 汾阳市| 巴林右旗| 南江县| 乌鲁木齐市| 屏东县| 竹溪县| 菏泽市| 临邑县| 镇坪县| 胶南市| 嘉鱼县| 肥东县| 武夷山市| 襄樊市| 荆州市| 龙泉市| 霸州市| 滦平县| 望谟县| 汾阳市| 荔波县| 德惠市| 监利县| 巴彦淖尔市| 新巴尔虎左旗| 登封市| 静安区| 信宜市| 全州县| 南丹县| 如皋市| 定州市| 临颍县| 全州县|