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

当img占不满div时,图片居中显示,两侧加当前图片模糊效果

当img占不满div时,图片居中显示,两侧加当前图片模糊效果

目录

    • 实现效果
    • 解决方案
      • 方案一:使用 CSS 伪元素实现模糊背景效果
      • 方案二:使用额外的背景元素
      • 方案三:使用双层图片结构
      • 推荐方案
        • 推荐使用方案一,因为它:
        • 关键点解释:

实现效果

当img占不满div时,图片居中显示,两侧加当前图片模糊效果

解决方案

方案一:使用 CSS 伪元素实现模糊背景效果

.video_list {.text_pics_box_item_img_box {width: 200px;height: 100px;position: relative;overflow: hidden;display: flex;align-items: center;justify-content: center;&::before {content: '';position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-image: var(--bg-image);background-size: cover;background-position: center;background-repeat: no-repeat;filter: blur(10px);transform: scale(1.1);z-index: 1;}::v-deep {.el-image {position: relative;z-index: 2;max-width: 100%;max-height: 100%;width: auto;height: auto;}}}
}

然后在模板中为 el-image 添加样式绑定:

<div class="text_pics_box_item_img_box"v-if="item.video_cover_imgs.length":style="{'--bg-image': `url(${item.video_cover_imgs[0]})`}"><el-image :src="item.video_cover_imgs[0]" fit="contain"></el-image>
</div>

方案二:使用额外的背景元素

.video_list {.text_pics_box_item_img_box {width: 200px;height: 100px;position: relative;overflow: hidden;.blur-background {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background-size: cover;background-position: center;background-repeat: no-repeat;filter: blur(10px);transform: scale(1.1);z-index: 1;}::v-deep {.el-image {position: relative;z-index: 2;max-width: 100%;max-height: 100%;width: auto;height: auto;}}}
}

对应的模板代码:

<div class="text_pics_box_item_img_box"v-if="item.video_cover_imgs.length"><div class="blur-background" :style="{backgroundImage: `url(${item.video_cover_imgs[0]})`}"></div><el-image :src="item.video_cover_imgs[0]" fit="contain"></el-image>
</div>

方案三:使用双层图片结构

.video_list {.text_pics_box_item_img_box {width: 200px;height: 100px;position: relative;overflow: hidden;.blur-img {position: absolute;top: 0;left: 0;width: 100%;height: 100%;::v-deep {.el-image__inner {width: 100%;height: 100%;object-fit: cover;filter: blur(10px);transform: scale(1.1);}}}.main-img {position: relative;z-index: 2;max-width: 100%;max-height: 100%;::v-deep {.el-image__inner {max-width: 100%;max-height: 100%;width: auto;height: auto;object-fit: contain;}}}}
}

对应的模板代码:

<div class="text_pics_box_item_img_box"v-if="item.video_cover_imgs.length"><div class="blur-img"><el-image :src="item.video_cover_imgs[0]"></el-image></div><div class="main-img"><el-image :src="item.video_cover_imgs[0]"></el-image></div>
</div>

推荐方案

推荐使用方案一,因为它:
  1. 代码简洁,只需一个额外的伪元素
  2. 性能较好,不需要额外的DOM元素
  3. 易于维护和修改
  4. 使用CSS变量动态设置背景图,灵活性高
关键点解释:
  1. filter: blur(10px) 创建模糊效果
  2. transform: scale(1.1) 防止模糊边缘出现白边
  3. z-index确保清晰图片在模糊背景之上
  4. object-fit: contain 保持图片比例完整显示
  5. 使用CSS变量动态设置背景图片路径
http://www.dtcms.com/a/329678.html

相关文章:

  • Redis基础命令
  • 【易错题】C语言
  • Git Bash
  • Linux内存管理机制分析
  • 汽车免拆诊断案例 | 2010款奥迪A4L车行驶中发动机偶尔自动熄火
  • uniapp微信小程序-登录页面验证码的实现(springboot+vue前后端分离)EasyCaptcha验证码 超详细
  • 超实用!ToDesk/网易UU/向日葵:远程办公文件协作效率与安全实测
  • redis 内存使用率高居高不下,如何分析 key占用情况
  • 物联网之小白调试网关设备
  • 18.9 BERT问答模型实战:从数据到部署的完整指南
  • C++面试艺术:我的思考与避坑指南
  • framebuffer
  • 深入理解Java Set集合特性
  • windows下以all-in-one模式快速启动jaeger
  • Linux学习-UI技术
  • ROS2实用工具
  • Spring AI 的特性 及其 最佳实践
  • CompletableFuture介绍及使用方式
  • 天猫商品评论API:获取商品热门评价与最新评价
  • Jmeter TPS与QPS
  • Ant Design 的 `Image` 组件,通过 `preview.src` 加载本地图片文件
  • Dockerhub 代理设置
  • 破解测试数据困境:5招兼顾安全与真实性
  • Nature Communications 西湖大学姜汉卿教授:弹电磁驱动新范式--赋能昆虫级软体机器人的肌肉仿生策略
  • HTML第三次作业
  • Redis ubuntu下载Redis的C++客户端
  • Ubuntu 20.04 虚拟机安装完整教程:从 VMware 到 VMware Tools
  • 如何在 Ubuntu 24.04 LTS Noble Linux 上安装 FileZilla Server
  • Python【算法中心 03】Docker部署Django搭建的Python应用流程实例(Docker离线安装配置+Django项目Docker部署)
  • java中list的api详细使用