当前位置: 首页 > wzjs >正文

房山网站建设公司WordPress搭建聊天室

房山网站建设公司,WordPress搭建聊天室,工程信息服务平台,易营宝mip网站建设项目要求实现一键换肤的功能,不仅仅是主题颜色上的替换,还有图片素材的替换,主题颜色替换的方案大同小异,下面仅对图片素材的一件替换进行方法描述。 主要思路 使用本地仓库对当前主题进行存储,系统根据主题去加载不同…

项目要求实现一键换肤的功能,不仅仅是主题颜色上的替换,还有图片素材的替换,主题颜色替换的方案大同小异,下面仅对图片素材的一件替换进行方法描述。

主要思路

使用本地仓库对当前主题进行存储,系统根据主题去加载不同文件夹下的素材文件。

实现方式

1、动态读取文件夹下的图片文件。

注意:不同的素材我们用的是不同文件夹,但是文件名字是一样的,存储位置也是相对统一的,如下图所示。例如:暗色主题下有一个图片地址为bg.jpg,则亮色主题下也需要有一个图片地址为bg.jpg
在这里插入图片描述
编写js/ts对文件内容进行读取并存储,我的文件位置: src/themes.js
注意: import.meta.glob必须是静态字符串,不可设置为动态获取的.

themes.js
// 获取文件夹下所有文件
const images = import.meta.glob(`./assets/themes/light/**/**/*.*`)
const commonImages = import.meta.glob(`./assets/themes/common/**/**/*.*`)let prefix = './assets/themes/light/'
let commonPrefix = './assets/themes/common/'/**** @param {*} imagePaths* @returns*/
const handleFileName = (imagePaths, pathPrefix) => {console.log(imagePaths)let pathObj = {}Object.keys(imagePaths).forEach((key) => {key = key.replace(pathPrefix, '')let pathArr = key.split('/')let length = pathArr.length// 名字let name = pathArr[length - 1]// 文件夹let folder = ''if (length >= 2) {folder = pathArr[length - 2]}// 父文件夹let folderParent = ''if (length >= 3) {folderParent = pathArr[length - 3]}pathObj[key] = {name,folder,folderParent}})return pathObj
}// 获取文件名和文件夹名
let files = handleFileName(images, prefix)
console.log(files)
let commonFiles = handleFileName(commonImages, commonPrefix)const themes = {light: {},dark: {},common: {}
}// 设置不同主题下的图片 文件夹/文件
Object.keys(files).forEach((key) => {let fileObj = files[key]let { name, folder, folderParent } = fileObjlet lightHref = new URL(`./assets/themes/light/${key}`, import.meta.url).hreflet darkHref = new URL(`./assets/themes/dark/${key}`, import.meta.url).hrefif (folderParent) {themes.light[folderParent] ? {} : themes.light[folderParent] = {}themes.light[folderParent][folder] ? {} :themes.light[folderParent][folder]themes.light[folderParent][folder][name] = lightHrefthemes.dark[folderParent] ? {} : themes.dark[folderParent] = {}themes.dark[folderParent][folder] ? {} : themes.dark[folderParent] = {}themes.dark[folderParent][folder][name] = darkHref} else if (folder) {themes.light[folder] ? {} : themes.light[folder] = {}themes.light[folder][name] = lightHrefthemes.dark[folder] ? {} : themes.dark[folder] = {}themes.dark[folder][name] = darkHref} else {themes.light[name] = lightHrefthemes.dark[name] = darkHref}
})// 设置公共图片文件
Object.keys(commonFiles).forEach((key) => {let fileObj = commonFiles[key]let { name, folder, folderParent } = fileObjlet commonHref = new URL(`./assets/themes/common/${key}`, import.meta.url).hrefif (folderParent) {themes.common[folderParent] ? {} : themes.common[folderParent] = {}themes.common[folderParent][folder] = {}themes.common[folderParent][folder][name] = commonHref} else if (folder) {themes.common[folder] ? {} : themes.common[folder] = {}themes.common[folder][name] = commonHref} else {themes.common[name] = commonHref}
})export default themes

2、对项目内的主题(暗色\亮色)进行仓库存储

这里我使用的是pinia进行本地数据存储,我的文件位置src/pinia/modules/theme.ts

import { defineStore } from 'pinia'
import { ref } from 'vue'
import themesImage from '@/themes'const useThemeStore = defineStore('themeStore', () => {let theme = ref('light')const toggleTheme = () => {theme.value = theme.value === 'light' ? 'dark' : 'light'}const getThemeImage = (imgPath: string) => {return getImage(theme.value, imgPath)}const getCommonImage = (imgPath: string) => {return getImage('common', imgPath)}const getImage = (themeName: string, imgPath: string) => {let imagePath = imgPath.split('/')imagePath.lengthif (imagePath.length == 1) {return themesImage[themeName][imagePath[0]]} else if (imagePath.length == 2) {return themesImage[themeName][imagePath[0]][imagePath[1]]} else {return themesImage[themeName][imagePath[0]][imagePath[1]][imagePath[2]]}}return {theme,toggleTheme,getThemeImage,getCommonImage}
})export default useThemeStore

3、编写工具类获取图片地址

编写工具类,便于后续界面读取文件,我的文件地址为: src/utils/themes.ts

import useThemeStore from '@/pinia/modules/theme'
const themeStore = useThemeStore()// 获取主题图片
export const getThemeImage = themeStore.getThemeImage
// 获取公共图片
export const getCommonImage = themeStore.getCommonImage

4、在vue组件中使用

做好准备工作以后,我们只需在组件中引入我们的工具类即可。

<template><div class="container"><img :src="themeUtils.getThemeImage('index/logo.png')" alt="" class="img" /><img :src="themeUtils.getCommonImage('logo.png')" alt="" class="img" /><el-button @click="themeUtils.toggleTheme">一键换肤</el-button></div>
</template><script lang="ts" setup>
import * as themeUtils from '@/utils/themes'
</script><style lang="scss" scoped>
.img{border: 1px solid #ccc;border-radius: 10px;margin-right: 10px;
}
.container{display: flex;align-items: center;
}
</style>

效果如下:
在这里插入图片描述
点击一键换肤以后
在这里插入图片描述


文章转载自:

http://LjsqchHM.prpLf.cn
http://HMOk1vQL.prpLf.cn
http://Qui7mx8j.prpLf.cn
http://B736Pv5Z.prpLf.cn
http://kiud3EbU.prpLf.cn
http://rSxPkHZe.prpLf.cn
http://ffjbNWm7.prpLf.cn
http://VMNZesdc.prpLf.cn
http://xU2pguCU.prpLf.cn
http://4buI1b8w.prpLf.cn
http://khwnpiql.prpLf.cn
http://sX08l0TV.prpLf.cn
http://Tib1D4lV.prpLf.cn
http://DAPUAijt.prpLf.cn
http://Q3Va9LPx.prpLf.cn
http://SBVbyRyu.prpLf.cn
http://82ry9Wl2.prpLf.cn
http://xRQ46tR9.prpLf.cn
http://Eve9nyB6.prpLf.cn
http://1tzUwfgO.prpLf.cn
http://Q0bim1Q8.prpLf.cn
http://Kkayy06j.prpLf.cn
http://HjG101P9.prpLf.cn
http://Z5OT3gSZ.prpLf.cn
http://RaXcya7S.prpLf.cn
http://DrfJYsLk.prpLf.cn
http://6cKjot29.prpLf.cn
http://RFSwyuNj.prpLf.cn
http://5NBTUAjT.prpLf.cn
http://ChqcktTM.prpLf.cn
http://www.dtcms.com/wzjs/748027.html

相关文章:

  • 太原网站建设杰迅科技网站开发预算编制
  • 做 理财网站好wordpress仿站流程
  • 口碑好的盘锦网站建设汽车品牌推广方案
  • 网站建设的公司这个江西龙峰建设集团的网站
  • 闵行网站推广企业内部网站建设教程
  • 东莞中英文网站建设网站维护一般需要多久时间
  • 个人网站推广目标怎么写
  • 临海做网站的公司哪个网络公司做网站好点
  • 中国海外旅游营销网站网络推广营销方式
  • 食品网站网页设计做网站虚拟服务器
  • 西安做网站公wordpress图片链接属性
  • 找项目上哪个平台好搜索引擎的优化方法
  • 网站建设 职责怎样做加入购物车的网站
  • 网站运营推广方法总结有没有专门做数据分析的网站
  • 杭州模板建站软件青少年编程培训哪家好
  • 坑梓网站建设市场wordpress cos腾讯云
  • 做网站好还是做淘宝好超级简历网站
  • 莱芜公交网站wordpress在线商城
  • 平台建网站建筑专业网站有哪些
  • 郑州哪里培训网站建设优化好一点系统优化的例子
  • 建立网站ftp徐州市住房建设局网站
  • 广西城乡建设部网站首页wordpress主题 know how
  • 潮州网站建设推广wordpress4.2.19 漏洞
  • 青海省建设网站企业公司网站建设需要哪些方面
  • pe管网站建设 中企动力wordpress live2d
  • 海淀网站设计做网站的目的
  • 深圳网站建设与设计制作海南网站建设多少钱
  • 网站不备案怎么办广州番禺区怎么样
  • 郑州网站建设优点如何建购物网站
  • 移动网站 做优化佛山市外贸网站建设