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

【css特效】:实现背景色跟随图片相近色处理

目录

第一章 效果

第二章 源码


第一章 效果

第二章 源码

这里小编直接上代码(复制即可使用),涉及到的技术小编会在最后说明!

<script setup>
import { ref } from 'vue'
import ColorThief from 'colorthief'
import tool from '@/utils/tool'
import { cs } from '@/utils/cs'const colorThief = new ColorThief()
const dom = ref(null)
// 本地图片
const source = ref(['images/Color/1.jpg','images/Color/2.jpg','images/Color/3.jpg','images/Color/4.jpg'
])
// 用随机图片网址生成的图片
// const source = ref([
//   'https://picsum.photos/200/300?i=1',
//   'https://picsum.photos/200/300?i=2',
//   'https://picsum.photos/200/300?i=3',
//   'https://picsum.photos/200/300?i=4'
// ])const hoverIndex = ref(-1) // 鼠标移入的图片索引
const handleMouseEnter = async (e, idx) => {// 鼠标移入事件hoverIndex.value = idx // 记录当前鼠标移入的图片索引console.log('元素', e.target) // 获取当前鼠标移入的图片元素const colors = await colorThief.getPalette(e.target, 3) // 获取图片的3个主要颜色console.log('颜色数组', colors) // 打印颜色数组const [c1, c2, c3] = colors.map((c) => `rgb(${c[0]}, ${c[1]}, ${c[2]})`) // 将颜色数组转换为rgb格式// 设置渐变背景dom.value.style.setProperty('--grad',`linear-gradient(135deg, ${c1}, ${c2}, ${c3})`)
}const handleMouseLeave = () => { // 鼠标移出事件dom.value.style.setProperty('--grad', `#fff`) // 移出时恢复默认背景hoverIndex.value = -1 // 重置当前鼠标移入的图片索引
}
</script><template><div:class="cs('main','w-full h-full','flex justify-start items-center flex-wrap gap-20px')"ref="dom"><img:src="tool.viteRequire(item)"@mouseenter="handleMouseEnter($event, index)"@mouseleave="handleMouseLeave($event)"alt=""v-for="(item, index) in source":key="index"class="img_item h-1/2":style="{opacity: hoverIndex === -1 ? 1 : hoverIndex === index ? 1 : 0.2}"/><!-- <imgcrossorigin="anonymous"@mouseenter="handleMouseEnter($event, index)"@mouseleave="handleMouseLeave($event)":src="item"alt=""v-for="(item, index) in source":key="index"class="img_item h-1/2":style="{opacity: hoverIndex === -1 ? 1 : hoverIndex === index ? 1 : 0.2}"/> --></div>
</template><style lang="scss">
.main {background: var(--grad);.img_item {width: calc(100% / 2 - 20px);}
}
</style>
  • 知识点1:colorthief插件,获取当前图片的主要颜色

官方文档:https://lokeshdhakar.com/projects/color-thief/

  • dom.value.style.setProperty:设置dom样式方法
  • background: var(--grad):自定义属性名,--grad需要根节点定义
  • https://picsum.photos/200/300:随机生成图片

推荐一个随机生成图片的网站: Lorem Picsum

http://www.dtcms.com/a/394159.html

相关文章:

  • vuex原理
  • 内存泄露怎么排查?
  • nginx配置防盗链入门
  • Kafka 多机房、跨集群复制、多租户、硬件与操作系统、全栈监控
  • leetcode136.只出现一次的数字
  • 力扣hot100:环形链表II(哈希算法与快慢指针法思路讲解)
  • 【算法】【Leetcode】【数学】统计1的个数 数位统计法
  • Kafka面试精讲 Day 21:Kafka Connect数据集成
  • MySQL 主从复制完整配置指南
  • 力扣每日一刷Day 23
  • LeetCode 53. 最大子数组和(四种解题思路)包含扩展返回最大和的数组
  • RTX 4090助力深度学习:从PyTorch到生产环境的完整实践指南——高效模型训练与优化策略
  • 23种设计模式之【桥接模式】-核心原理与 Java实践
  • LabVIEW手部运动机能实验
  • 669. 修剪二叉搜索树
  • 大QMT自动可转债申购
  • PolarCTF PWN 网络安全2023秋季个人挑战赛刷题
  • MySQL-day4_02(事务)
  • JUC(8)线程安全集合类
  • springboot中@EnableAsync有什么作用
  • Spark专题-第二部分:Spark SQL 入门(6)-算子介绍-Generate
  • C#练习题——Dictionary
  • Feign
  • SPA小说集之三《森林城市反甩锅战:ERP的权责边界》
  • Qt(模态对话框和非模态对话框)
  • 【无标题】物联网 frid卡控制
  • 【LLM LangChain】 模型绑定工具+调用工具(手动调用/LangGraph/AgentExecutor)+相关注意事项
  • 图神经网络(GNN)入门:用PyG库处理分子结构与社会网络
  • 【C++】编码表 STL简介:STL是什么,版本,六大组件,重要性以及学习方法总结
  • show_interrupts函数的进一步解析及irq_desc结构体