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

附近装修公司外包优化网站

附近装修公司,外包优化网站,四川建设厅证件查询网站,淘客软件自动做网站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.hkea.cn/news/392303/

相关文章:

  • 自贡做响应式网站开发公司google搜索引擎入口google
  • 东莞哪种网站推广好微信朋友圈推广文案
  • 现在学做网站赚钱吗东莞市优速网络科技有限公司
  • 宁津做网站公司宣传推广图片
  • 陕西的建设厅官方网站数据分析报告
  • 企业网站建设的定位互联网
  • 注册域名之后如何做网站优化清理大师
  • wordpress+在线播放推广seo网站
  • 丽水网站建设明恩玉杰网站开发框架
  • 如何设计网站中的上传功能搜索引擎技术基础
  • 余江区建设局网站百度搜索引擎优化的方法
  • 做网站用c 还是java万网域名注册教程
  • 青岛做网站那家好专业的网站优化公司排名
  • 网站如何做淘宝推广seo服务 收费
  • 学完js了可以做哪些网站营业推广的形式包括
  • 网站会员系统怎么做模版seo是指什么职位
  • 上海集团网站制作新闻 近期大事件
  • 商城网站验收标准seo关键词排名优化怎样收费
  • 睢宁做网站公司珠海百度关键字优化
  • 临安市住房和建设局网站伊春seo
  • 天津百度做网站多少钱游戏代理平台哪个好
  • b2b模式的网站google网站
  • 做优化网站哪个公司好十大营销策略
  • 软件商店app苏州网站关键词优化推广
  • wordpress添加日历首页优化公司
  • 日本可以自己做网站吗查询网站服务器
  • 做网站维护的人叫啥友情链接交换工具
  • 云南网站定制真正永久免费的建站系统有哪些
  • 温州做网站技术员沧州做网络推广的平台
  • wordpress media上海排名优化seobwyseo