您的位置:首页 > 娱乐 > 八卦 > JS读取目录下的所有图片/文字高亮

JS读取目录下的所有图片/文字高亮

2024/12/23 3:47:24 来源:https://blog.csdn.net/qq_39691676/article/details/139708140  浏览:    关键词:JS读取目录下的所有图片/文字高亮

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/df510a62ec1247918bd0131b81e10a4d.png

<template class="aa"><div class="demo-image__lazy container"><div class="head"><div class="left-bar"><div><span>综合</span></div><div><span>定位</span></div></div><div class="right-bar"><div><el-radio-groupv-model="value1"v-for="(item, index) in group1":key="index"><el-radio :label="index">{{ item }}</el-radio></el-radio-group></div><div><el-radio-group v-model="value2"><el-radio label="0">全部</el-radio><el-radio label="1">战士</el-radio><el-radio label="2">坦克</el-radio><el-radio label="3">刺客</el-radio><el-radio label="4">射手</el-radio><el-radio label="5">法师</el-radio><el-radio label="6">辅助</el-radio><el-inputclass="p"v-model="name"placeholder="请输入要查找的英雄"></el-input><el-button style="margin-left: 10px" @click="search">查询</el-button></el-radio-group></div></div></div><div class="foot"><div class="imageBox" v-for="url in images" :key="url.path"><el-imageclass="imgSize":src="require('@/assets/王者荣耀壁纸/' + url.path + '.jpg')"lazy></el-image><span v-html="url.heroName"></span></div></div><!-- ----------------------------------------------------------------- --><!-- <div class="demo-image__lazy image" v-for="url in images" :key="url"><el-imageclass="":src="require('@/assets/王者荣耀壁纸/' + url.path + '.jpg')"lazy><template #placeholder><div class="custom-loading">加载中...</div></template></el-image><span>{{ url.heroName }}</span></div> --></div>
</template><script>
//引入组件import StudyM from "./components/StudyM.vue"
export default {name: "Test",components: {StudyM,},data() {return {images: [], // 图片集合cloneImages: [], // 克隆图片集合name: undefined, // 输入框查找英雄group1: ["本周免费", "新手推荐"],value1: 0,value2: "0",// prefixImg: "@/assets/王者荣耀壁纸/",// suffixImg: ".jpg",}},watch: {// 周免英雄/新手推荐value1(newValue) {this.getImage(newValue, "heroNumber")},// 英雄类型value2(newValue) {this.getImage(newValue, "heroType")},// 输入框查询name(newValue) {this.getImage(newValue, "search")},},created() {this.test()},methods: {// 图片处理test() {// 为什么require的变量跟字符串要分开写,而不能提前拼接到一起const controllerFiles = require.context("@/assets/王者荣耀壁纸",true,/\.jpg$/)const controller = controllerFiles.keys().reduce((controller, controllerPath) => {const controllerName = controllerPath.replace(/^\.\/(.*)\.\w+$/, "$1")var heroName = controllerName.split("-")var type = this.getRandomNumber(1, 6)/*** type 英雄类型* 1: 战士  2:坦克  3:刺客  4:射手 5:法师 6:辅助*/var number = this.getRandomNumber(0, 1)/*** number 1: 本周免费  2:新手推荐*/this.images.push({path: controllerName,heroName: heroName[1],type: type,number: number,})}, {})this.cloneImages = JSON.parse(JSON.stringify(this.images))},// 插件查询英雄getImage(param1, param2) {this.images = JSON.parse(JSON.stringify(this.cloneImages))if (param2 === "heroNumber") {this.images = this.images.filter((item) => item.number == param1)} else if (param2 === "heroType") {if (!(param1 == "0")) {//英雄类型的查询条件非"全部"this.images = this.images.filter((item) => item.type == param1)}} else if (param2 === "search") {this.images = this.images.filter((item) =>item.heroName.includes(param1))this.images.forEach((item) => {// 搜索词高亮// i 匹配大小写  g匹配全部const reg = new RegExp(param1, "ig")item.heroName = item.heroName.replace(reg, (match) => {//替换对应字符return `<span style="color:red; font-weight: bold;">${match}</span>`})})}},search() {this.getImage(this.name, "search")},// 用随机数表示英雄类型getRandomNumber(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min},},
}
</script><style lang="scss" scoped>
@mixin pub-css {display: flex;flex-direction: column;justify-content: space-around;
}.custom-loading {color: #409eff;text-align: center;font-size: 14px;
}.head {height: 80px;display: flex;flex-direction: row;
}.left-bar {width: 80px;background: red;@include pub-css;align-items: center;
}.right-bar {width: 100%;background: darkblue;@include pub-css;& div {margin-left: 10px;}& :nth-child(2) {.p {width: 160px;margin-left: 200px;}}
}.foot {margin-top: 25px;display: grid;grid-template-columns: repeat(auto-fill, 95px);grid-template-rows: repeat(auto-fill, 95px);gap: 24px 35px;.imageBox {margin-left: 15px;text-align: center;}.imgSize {width: 95px;height: 95px;border: 1px solid;border-top-left-radius: 10px;border-bottom-right-radius: 10px;}
}
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com