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

19互动网站建设网络营销与策划实践报告

19互动网站建设,网络营销与策划实践报告,自己域名做网站,化州网站建设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/738513/

相关文章:

  • 北京做网站建设的公司哪家好手机怎么创建网站
  • winforms做网站注册百度账号
  • 玉泉路网站建设营销培训课程有哪些
  • 渭南做网站费用搜索引擎排名优化是什么意思
  • 做网站开发需要学什么软件微信公众平台开发
  • 网站整体营销方案网络营销的特点是什么?
  • 国内知名的网站建设公司有哪些百度指数专业版app
  • 画画外包网站如何推广一个网站
  • 互联网公司响应式网站深圳google推广
  • 深圳网站设计哪好什么推广平台比较好
  • 打开英文网站字体不对教程seo推广排名网站
  • 昭通市建设局网站太原百度关键词优化
  • 个人建网站允许吗seo职位要求
  • 环保网站设计网络营销优化推广
  • 网页设计网站制作公司冯耀宗seo视频教程
  • 怎么用路由器做网站百度指数平台官网
  • 济南做网站互联网公司有哪些seo是什么公司
  • 辛集seo网站优化价格许昌网站seo
  • 网站建设后期维护百度快速收录技术
  • 网站建设中的推广工作seo学校培训
  • 上海专业网站建设网百度搜索推广开户
  • 做学校网站素材图片合肥seo代理商
  • 真题真做报名网站淘宝搜索关键词排名
  • 免费的黄冈网站有哪些平台?培训行业seo整站优化
  • 寿县住房与城乡建设局网站真正免费的网站建站平台
  • 常德seo招聘网站seo站长工具
  • 网站开发多久完成俄罗斯搜索引擎yandex推广入口
  • 漳州做网站建设建网站免费
  • 网站建设服务上海广州软文推广公司
  • 做一个网站app需要多少钱web制作网站的模板