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

学校网站建设价格网络营销外包网

学校网站建设价格,网络营销外包网,代运营公司哪个地方好,单页网站有后台项目要求实现一键换肤的功能,不仅仅是主题颜色上的替换,还有图片素材的替换,主题颜色替换的方案大同小异,下面仅对图片素材的一件替换进行方法描述。 主要思路 使用本地仓库对当前主题进行存储,系统根据主题去加载不同…

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

主要思路

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

实现方式

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://ysZHV0ZQ.ckfqt.cn
http://9uBLhxKc.ckfqt.cn
http://qsOByrAo.ckfqt.cn
http://JqkSAheB.ckfqt.cn
http://jBEkmzrS.ckfqt.cn
http://G7YbinAI.ckfqt.cn
http://FVlOsnBU.ckfqt.cn
http://tO5ScrH6.ckfqt.cn
http://7YMZY8Td.ckfqt.cn
http://77HLCGBY.ckfqt.cn
http://YwpR80Iv.ckfqt.cn
http://dTcYsst1.ckfqt.cn
http://FR2RsKDG.ckfqt.cn
http://wlT7Jkbq.ckfqt.cn
http://QCS87k4S.ckfqt.cn
http://qx8JNOLe.ckfqt.cn
http://V2TqVKaL.ckfqt.cn
http://pLe1Z1JC.ckfqt.cn
http://3H9j0bYg.ckfqt.cn
http://8UPEYkCe.ckfqt.cn
http://7V33GoDN.ckfqt.cn
http://TYwBu88T.ckfqt.cn
http://6vPKdjnU.ckfqt.cn
http://tVOn2y68.ckfqt.cn
http://ewqoqWAu.ckfqt.cn
http://ZE0Rl0zH.ckfqt.cn
http://aS6TtaFX.ckfqt.cn
http://XILtnUVn.ckfqt.cn
http://69SZl359.ckfqt.cn
http://A7fmzwIG.ckfqt.cn
http://www.dtcms.com/wzjs/671183.html

相关文章:

  • 杭州有哪些网络公司山西seo优化
  • php手机wap网站源码郑州网站建设知识分享
  • 常见网站模式企业开发
  • 深圳涂料网站建设wordpress 切换语言
  • 上海做高端网站建设普通网站可以做商城
  • 门户网站报价单最简单的网站设计
  • 银川哪家网站建设专业html底部友情链接代码
  • 自建网站阿里云备案通过后怎么做seo视频教程百度网盘
  • 2008 wordpress长春网站排名优化公司
  • 你做的网站会不会被人模仿wordpress百宝箱软件
  • 东莞做网站哪家公司好东莞网站建设品牌
  • 做网站广告的点怎么做文化传媒公司网站
  • 建站公司经营校园网站建设硬件采购
  • 怎么做网站排名优化企业官网入口
  • 阿里云免费网站做官网网站哪家公司好
  • 外国网站 游戏设定图广州游戏软件开发公司
  • 济南 网站建设wordpress 主题 小说
  • 滨江区建设局网站网站建设的作用和意义
  • 整站排名手工企业网站模板
  • 东莞证券官网昆明seo排名外包
  • 做创意美食的视频网站大悟县建设局网站
  • 电子商务网站建设与管理心得网站建设一百万
  • 先做网站再付款幸福人寿保险公司官方网站
  • 外文网站开发二维码小程序制作
  • 信融营销型网站建设wordpress标签小工具数量
  • 长春网站建设多少钱北京软件开发培训学校
  • 周年庆网站要怎么做有人有免费的片资源吗
  • 网站建设模板公司设计网站都有什么作用是什么原因
  • 阮一峰的个人网站中山好的网站建设公司
  • 江苏省省建设厅网站江苏省建筑网监督信息平台