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

Vue实现悬浮图片弹出大图预览弹窗,弹窗顶部与图片顶部平齐

Vue实现悬浮图片弹出大图预览弹窗,弹窗顶部与图片顶部平齐

在这里插入图片描述

需求背景

当前项目内某页面左侧展示图片列表,图片列表展示的均为小图。需求希望鼠标移动到对应图片时,右侧出现大图预览弹窗,且该弹窗顶部需与图片顶部平齐。同时弹窗要在页面中展示完全,不能超出窗口高度导致被遮挡

核心实现

1.获取悬浮图片的位置信息

<div class="image_item" v-for="(item, index) in imgList" :key="index"><img:src="item"class="img"@mouseenter="e => handlePreview(e, item)"@mouseleave="preview.show = false"/>
</div>
// 获取当前图片顶部与视口顶部的距离top和底部的距离bottom
const handlePreview = (e, url) => {const targetRect = e.target.getBoundingClientRect()preview.bottom = window.innerHeight - targetRect.toppreview.top = targetRect.toppreview.url = urlpreview.show = true
}

2.动态计算大图预览弹窗位置

<!-- 样式绑定计算属性,根据悬浮图片位置变化 -->
<div class="module_view" v-show="preview.show" :style="previewStyle"><img :src="preview.url" class="img" />
</div>
const previewStyle = computed(() => {// 弹窗实际高度const previewHeight = 538const container = listRef.value// 容器与视口顶部距离const containerTop = container ? container.getBoundingClientRect().top : 0const previewTop = preview.topconst previewBottom = preview.bottom// 弹窗顶部与容器顶部的距离let top = previewTop - containerToplet bottom = previewBottom - previewHeight// 判断弹窗顶部与视口底部的距离是否能容纳整个弹窗if (bottom < 0) {// 无法容纳时,减小弹窗顶部距离容器顶部的距离从而抬升弹窗// 还需判断抬升后弹窗顶部与视口顶部是否仍大于0,否则设置为置顶距离,即负的容器与视口顶部距离top = previewTop + bottom > 0 ? top + bottom : 0 - containerTop}return {top: top + 'px'}
})

完整代码

<!-- 实现图片悬浮右侧展开预览大图弹窗功能,弹窗顶部与图片顶部平齐 -->
<template><div class="image_view"><div class="image_list" ref="listRef"><div class="image_item" v-for="(item, index) in imgList" :key="index"><img:src="item"class="img"@mouseenter="e => handlePreview(e, item)"@mouseleave="preview.show = false"/></div><div class="module_view" v-show="preview.show" :style="previewStyle"><img :src="preview.url" class="img" /></div></div></div>
</template><script>
import { defineComponent, ref, reactive, computed } from 'vue'
import { getImg } from '@/utils/imgExample'export default defineComponent({setup() {const imgList = ref(getImg(0, 12))const listRef = ref(null)const preview = reactive({top: 0,bottom: 0,url: '',show: false})const previewStyle = computed(() => {// 弹窗实际高度const previewHeight = 538const container = listRef.value// 容器与视口顶部距离const containerTop = container ? container.getBoundingClientRect().top : 0const previewTop = preview.topconst previewBottom = preview.bottom// 弹窗顶部与容器顶部的距离let top = previewTop - containerToplet bottom = previewBottom - previewHeight// 判断弹窗顶部与视口底部的距离是否能容纳整个弹窗if (bottom < 0) {// 无法容纳时,减小弹窗顶部距离容器顶部的距离从而抬升弹窗// 还需判断抬升后弹窗顶部与视口顶部是否仍大于0,否则设置为置顶距离,即负的容器与视口顶部距离top = previewTop + bottom > 0 ? top + bottom : 0 - containerTop}return {top: top + 'px'}})// 获取当前图片顶部与视口顶部的距离top和底部的距离bottomconst handlePreview = (e, url) => {const targetRect = e.target.getBoundingClientRect()preview.bottom = window.innerHeight - targetRect.toppreview.top = targetRect.toppreview.url = urlpreview.show = true}return {listRef,imgList,preview,previewStyle,handlePreview}}
})
</script><style lang="less" scoped>
.image_view {width: 100%;height: 100%;.image_list {position: relative;width: 404px;height: 100%;padding: 12px;border: 1px solid #ededed;display: flex;flex-wrap: wrap;gap: 8px;.image_item {width: 120px;height: 120px;border-radius: 4px;overflow: hidden;.img {width: 100%;height: 100%;object-fit: cover;}}.module_view {position: absolute;top: 0;right: -404px;width: 400px;height: 528px;background: #fff;box-shadow: 0 4px 10px 2px rgba(0, 0, 0, 0.16);padding: 8px;border-radius: 12px;}}
}
</style>
http://www.xdnf.cn/news/13965.html

相关文章:

  • 隧道代理IP的使用与技术优势分析
  • 几种经典排序算法的C++实现
  • jenkins连接git仓库
  • 03 - ECA模块
  • git checkout 详解
  • 危险品运输行业观察
  • Kafka环境搭建全攻略:从Docker到Java实战
  • Logback-spring.xml 配置屏蔽特定路径的日志
  • Vue3+Element Plus动态表格列宽设置
  • 【写实交互数字人】实时数字人助力政务变革:技术、产品与应用价值的全景剖析
  • 【插件推荐】WebRTC Protect — 防止 IP 泄漏
  • 苹果越来越像安卓,华为越来越像苹果
  • 电路图识图基础知识-电动机软启动器技术解析与应用(二十五)
  • 【Zephyr 系列 22】从单机开发到平台化:构建你自己的 Zephyr 物联网开发平台
  • 【结合JSR380自定义校验】
  • Altera系列FPGA基于ADV7180解码PAL视频,纯verilog去隔行,提供2套Quartus工程源码和技术支持
  • 智慧物流园区——解读华为智慧物流园区解决方案【附全文阅读】
  • 上海市计算机学会竞赛平台2022年4月月赛丙组圆环独立集(一)
  • 基于 Spring Cloud Gateway + Sentinel 实现高并发限流保护机制
  • PHP基础-控制结构
  • 全链路实时感知:网络专线端到端监控运维
  • SwiftUI隐藏返回按钮保留右滑手势方案
  • MyBatis原理
  • 关于阿里云-云消息队列MQTT的连接和使用,以及SpringBoot的集成使用
  • P8784 [蓝桥杯 2022 省 B] 积木画
  • 基于 STM32 七段数码管显示模块详解
  • 如何设置爬虫的访问频率?
  • 基于51单片机的直流电机运动控制proteus仿真
  • vue二级路由的写法,以及动态路由的匹配和获取动态参数的值
  • FreeSWITCH mod_curl 和 mod_xml_rpc 测试