国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ElementUI主題顏色動態(tài)切換并緩存

這篇具有很好參考價值的文章主要介紹了ElementUI主題顏色動態(tài)切換并緩存。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

今天給大家分享一下在Vue中使用ElementUI時,想切換主題顏色的兩種方式。

第一種:靜態(tài)改變項目中的主題顏色

比較簡單,稍微帶過:
項目中創(chuàng)建element-variables.scss文件,編寫內容:

/* 改變主題色變量 */
$--color-primary: teal;

/* 改變 icon 字體路徑變量,必需 */
$--font-path: '~element-ui/lib/theme-chalk/fonts';

@import "~element-ui/packages/theme-chalk/src/index";

之后,在項目的入口文件中,直接引入以上樣式文件即可(無需引入 Element 編譯好的 CSS 文件)文章來源地址http://www.zghlxwxcb.cn/news/detail-632245.html

import Vue from 'vue'
import Element from 'element-ui'
import './element-variables.scss'

Vue.use(Element)

第二種:重點?。。討B(tài)切換主題顏色并做緩存

  1. 創(chuàng)建theme.js文件,作為工具類,并寫入以下內容:
const __DEFUALT__COLOR = "#409EFF"
const version = require('element-ui/package.json').version

class Theme {
  constructor() {
    const color = this._getLocalTheme() || __DEFUALT__COLOR;
    const oldVal = this._getLocalTheme() || __DEFUALT__COLOR;
    this.changeTheme(color, oldVal);
    this.chalk = ''
  }
  async changeTheme($color, oldVal) {
    const themeCluster = this._getThemeCluster($color.replace('#', ''))
    const originalCluster = this._getThemeCluster(oldVal.replace('#', ''))
    const getHandler = (id) => {
      return () => {
        const originalCluster = this._getThemeCluster(__DEFUALT__COLOR.replace('#', ''))
        const newStyle = this._updateStyle(this.chalk, originalCluster, themeCluster)

        let styleTag = document.getElementById(id)
        if (!styleTag) {
          styleTag = document.createElement('style')
          styleTag.setAttribute('id', id)
          document.head.appendChild(styleTag)
        }
        styleTag.innerText = newStyle
      }
    }
    if (!this.chalk) {
      const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
      await this._getCSSString(url, 'chalk')
    }
    const chalkHandler = getHandler('chalk-style')
    chalkHandler()
    const styles = [].slice.call(document.querySelectorAll('style'))
      .filter(style => {
        const text = style.innerText
        return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
      })
    styles.forEach(style => {
      const { innerText } = style
      if (typeof innerText !== 'string') return
      style.innerText = this._updateStyle(innerText, originalCluster, themeCluster)
    })
    this._setLocalTheme("--primary-color", $color)
  }
  _getLocalTheme(key = "--primary-color") {
    return key && localStorage.getItem(key);
  }
  _setLocalTheme(key, value) {
    localStorage[key] = value;
  }
  _getThemeCluster(theme) {
    const tintColor = (color, tint) => {
      let red = parseInt(color.slice(0, 2), 16)
      let green = parseInt(color.slice(2, 4), 16)
      let blue = parseInt(color.slice(4, 6), 16)

      if (tint === 0) { // when primary color is in its rgb space
        return [red, green, blue].join(',')
      } else {
        red += Math.round(tint * (255 - red))
        green += Math.round(tint * (255 - green))
        blue += Math.round(tint * (255 - blue))

        red = red.toString(16)
        green = green.toString(16)
        blue = blue.toString(16)

        return `#${red}${green}${blue}`
      }
    }

    const shadeColor = (color, shade) => {
      let red = parseInt(color.slice(0, 2), 16)
      let green = parseInt(color.slice(2, 4), 16)
      let blue = parseInt(color.slice(4, 6), 16)

      red = Math.round((1 - shade) * red)
      green = Math.round((1 - shade) * green)
      blue = Math.round((1 - shade) * blue)

      red = red.toString(16)
      green = green.toString(16)
      blue = blue.toString(16)

      return `#${red}${green}${blue}`
    }

    const clusters = [theme]
    for (let i = 0; i <= 9; i++) {
      clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
    }
    clusters.push(shadeColor(theme, 0.1))
    return clusters
  }
  _updateStyle(style, oldCluster, newCluster) {
    let newStyle = style
    oldCluster.forEach((color, index) => {
      newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
    })
    return newStyle
  }
  _getCSSString(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest()
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status === 200) {
          this.chalk = xhr.responseText.replace(/@font-face{[^}]+}/, '')
          resolve()
        }
      }
      xhr.open('GET', url)
      xhr.send()
    })
  }
}

export default Theme

  1. 然后在項目入口main.js中引入即可
import Vue from 'vue'
import Theme from "./utils/theme
Vue.prototype.$theme = new Theme()
  1. 在ThemePicker中使用
<template>
  <el-color-picker
    v-model="theme"
    :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
    class="theme-picker"
    popper-class="theme-picker-dropdown"
  />
</template>

<script>

export default {
  data() {
    return {
      theme: ''
    }
  },
  computed: {
    defaultTheme() {
      return this.$store.state.settings.theme
    }
  },
  watch: {
    defaultTheme: {
      handler: function(val, oldVal) {
        this.theme = val
      },
      immediate: true
    },
    theme(val, oldVal) {
      this.$theme.changeTheme(val, oldVal)
      this.$store.dispatch('settings/changeSetting', {
        key: 'theme',
        value: val
      })
    }
  }
}
</script>

<style>
.theme-message,
.theme-picker-dropdown {
  z-index: 99999 !important;
}

.theme-picker .el-color-picker__trigger {
  height: 26px !important;
  width: 26px !important;
  padding: 2px;
}

.theme-picker-dropdown .el-color-dropdown__link-btn {
  display: none;
}
</style>

到了這里,關于ElementUI主題顏色動態(tài)切換并緩存的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • ElementUI Table 翻頁緩存數據

    ElementUI Table 翻頁緩存數據

    Element UI Table 翻頁保存之前的數據,網上找了一些,大部分都是用**:row-key** 和 reserve-selection ,但是我覺得有bug,我明明翻頁了…但是全選的的個框還是勾著的(可能是使用方法不對,要是有好使的…請cute我一下…感謝) 所以自己寫了一個… 手動勾選的時候,將數據保存 查看文檔,發(fā)現

    2024年02月11日
    瀏覽(15)
  • uniapp切換主題顏色(后臺管理系統(tǒng))

    uniapp切換主題顏色(后臺管理系統(tǒng))

    需求:在現有已經做好的后臺管理系統(tǒng)添加一個切換主題顏色的功能 分析:該項目用了很多uniapp的組件,css樣式沒有統(tǒng)一,類名也沒有統(tǒng)一 使用混合mixin.scss,并使用vuex 效果圖 功能:按鈕背景顏色、部分樣式、字體圖標、分頁跟隨主題顏色變化也變化 每一個用戶喜歡的主題

    2024年02月10日
    瀏覽(14)
  • elementui中table表格單元格背景、文字顏色修改(包含鼠標移入移出)

    elementui中table表格單元格背景、文字顏色修改(包含鼠標移入移出)

    一、改變背景顏色 1、在el-table表頭中添加屬性::cell-style=“addClass” (設置表頭背景顏色:header-cell-style=“{ background: ‘#999999’, color: ‘#000’ }”) 2、data模擬假數據: 3、在methods中: 二、鼠標移入改變背景、文字顏色 1、在el-table表頭中添加屬性:@cell-mouse-enter=“cellMouseEn

    2024年02月03日
    瀏覽(27)
  • Vue結合ElementUi修改<el-table>表格的背景顏色和表頭樣式

    修改table的表頭背景 和 字體顏色: 以下是修改el-table表格內容的背景色和邊框樣式:

    2024年02月11日
    瀏覽(99)
  • vue+elementUI 分頁切換時保存勾選框為選中狀態(tài)

    vue+elementUI 分頁切換時保存勾選框為選中狀態(tài)

    1、el-table指定 row-key 屬性為row.id 確保唯一性 2、el-table-column設置 reserve-selection 屬性為true?會在數據更新之后保留之前選中的數據( reserve-selection ? 僅對 type=selection 的列有效 )

    2024年01月19日
    瀏覽(21)
  • ElementUI動態(tài)添加表單項

    昨天感冒發(fā)燒了,腦子不好使。在實現這個動態(tài)表單項時一直報錯腦瓜子嗡嗡的! 不過好在昨天休息好了,今天起來趁腦瓜子好使,一會就弄好了。 這里記錄一下 其實就是利用了vue的v-for循環(huán)渲染。通過添加數組實現動態(tài)添加表單項?

    2024年02月13日
    瀏覽(16)
  • Vue項目如何配置、切換主題顏色(mixin + scss方式,簡單高效)

    Vue項目如何配置、切換主題顏色(mixin + scss方式,簡單高效)

    但圖多 基本樣式: 紅色主題: 藍色主題: 看到這里,是不是有人已經開始安耐不住了 ??? 一. 首先,引入scss依賴(node-sass, sass-loader) 二.項目樣式文件目錄介紹 1.此處我將項目中的公共樣式文件放到了 src/style目錄下,其中 index.scss是以供全局使用的一些基本樣式,在main

    2024年02月04日
    瀏覽(25)
  • elementUI tabs切換 echarts寬度擠壓到一起 由100%變成100px

    elementUI tabs切換 echarts寬度擠壓到一起 由100%變成100px

    被壓縮的圖表: 正常顯示 1.需求:點擊tab切換echarts 2.所用技術:引的vue.js elementUI 切換用的是elementUI中的Tabs標簽頁 3.遇到了幾個問題: 1》報錯:[Vue warn]: Error in mounted hook: “TypeError: Cannot read property ‘getAttribute’ of null” 2》點擊切換 tabs,導致echarts寬擠到一起,只有100px 3》點

    2024年02月04日
    瀏覽(16)
  • ElementUI之動態(tài)樹+數據表格+分頁->動態(tài)樹,動態(tài)表格

    ElementUI之動態(tài)樹+數據表格+分頁->動態(tài)樹,動態(tài)表格

    動態(tài)樹 動態(tài)表格 1.動態(tài)樹 2.動態(tài)表格 ?

    2024年02月07日
    瀏覽(23)
  • elementUI表單驗證之動態(tài)表單驗證

    elementUI表單驗證之動態(tài)表單驗證

    elementUI 中 Form 組件提供了表單驗證的功能,只需要通過? rules ?屬性傳入約定的驗證規(guī)則,并將 Form-Item 的? prop ?屬性設置為需校驗的字段名即可。 (1)常用表單驗證 (2)自定義驗證規(guī)則(:validator) ?有些需要自定義的校驗規(guī)則可以作為變量寫在data中,然后賦值

    2024年02月11日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包