feat(结账页面): 添加iframe高度自适应功能
添加adjustIframeHeight函数实现iframe内容高度自适应,移除固定高度样式 在iframe加载完成和关闭时触发高度调整,提升用户体验
Showing
1 changed file
with
49 additions
and
1 deletions
| ... | @@ -86,8 +86,10 @@ | ... | @@ -86,8 +86,10 @@ |
| 86 | :src="iframeInfoSrc" | 86 | :src="iframeInfoSrc" |
| 87 | class="w-full border-0" | 87 | class="w-full border-0" |
| 88 | ref="infoEntryIframe" | 88 | ref="infoEntryIframe" |
| 89 | - style="width: 100%; min-height: 600px; height: auto; border: none;" | 89 | + style="width: 100%; border: none; display: block;" |
| 90 | frameborder="0" | 90 | frameborder="0" |
| 91 | + scrolling="no" | ||
| 92 | + @load="adjustIframeHeight" | ||
| 91 | ></iframe> | 93 | ></iframe> |
| 92 | </FrostedGlass> | 94 | </FrostedGlass> |
| 93 | 95 | ||
| ... | @@ -267,6 +269,40 @@ const formData = ref({ | ... | @@ -267,6 +269,40 @@ const formData = ref({ |
| 267 | // 存储当前课程的localStorage键名,用于页面离开时清理 | 269 | // 存储当前课程的localStorage键名,用于页面离开时清理 |
| 268 | let currentInfoEntryKey = null | 270 | let currentInfoEntryKey = null |
| 269 | 271 | ||
| 272 | +// iframe高度自适应函数 | ||
| 273 | +const adjustIframeHeight = () => { | ||
| 274 | + const iframe = infoEntryIframe.value | ||
| 275 | + if (iframe) { | ||
| 276 | + try { | ||
| 277 | + // 等待iframe内容加载完成 | ||
| 278 | + setTimeout(() => { | ||
| 279 | + const iframeDoc = iframe.contentDocument || iframe.contentWindow.document | ||
| 280 | + if (iframeDoc) { | ||
| 281 | + const body = iframeDoc.body | ||
| 282 | + const html = iframeDoc.documentElement | ||
| 283 | + | ||
| 284 | + // 获取内容的实际高度 | ||
| 285 | + const height = Math.max( | ||
| 286 | + body.scrollHeight, | ||
| 287 | + body.offsetHeight, | ||
| 288 | + html.clientHeight, | ||
| 289 | + html.scrollHeight, | ||
| 290 | + html.offsetHeight | ||
| 291 | + ) | ||
| 292 | + | ||
| 293 | + // 设置iframe高度,添加一些padding | ||
| 294 | + iframe.style.height = (height + 20) + 'px' | ||
| 295 | + console.log('调整iframe高度:', height + 20 + 'px') | ||
| 296 | + } | ||
| 297 | + }, 100) | ||
| 298 | + } catch (error) { | ||
| 299 | + console.warn('无法访问iframe内容,可能存在跨域限制:', error) | ||
| 300 | + // 如果无法访问iframe内容,设置一个默认高度 | ||
| 301 | + iframe.style.height = '400px' | ||
| 302 | + } | ||
| 303 | + } | ||
| 304 | +} | ||
| 305 | + | ||
| 270 | onMounted(async () => { | 306 | onMounted(async () => { |
| 271 | const { code, data } = await getUserInfoAPI(); | 307 | const { code, data } = await getUserInfoAPI(); |
| 272 | 308 | ||
| ... | @@ -297,6 +333,11 @@ onMounted(async () => { | ... | @@ -297,6 +333,11 @@ onMounted(async () => { |
| 297 | formData.value.customize_id = infoData.customize_id | 333 | formData.value.customize_id = infoData.customize_id |
| 298 | iframeInfoSrc.value = infoData.form_url + '&page_type=info' + '&data_id=' + infoData.customize_id | 334 | iframeInfoSrc.value = infoData.form_url + '&page_type=info' + '&data_id=' + infoData.customize_id |
| 299 | console.log('从缓存恢复个人信息数据:', infoData) | 335 | console.log('从缓存恢复个人信息数据:', infoData) |
| 336 | + | ||
| 337 | + // 延迟调整iframe高度,等待内容加载 | ||
| 338 | + setTimeout(() => { | ||
| 339 | + adjustIframeHeight() | ||
| 340 | + }, 1000) | ||
| 300 | } catch (error) { | 341 | } catch (error) { |
| 301 | console.error('解析缓存数据失败:', error) | 342 | console.error('解析缓存数据失败:', error) |
| 302 | // 如果解析失败,重新录入 | 343 | // 如果解析失败,重新录入 |
| ... | @@ -329,6 +370,8 @@ const showInfoEntry = ref(false) | ... | @@ -329,6 +370,8 @@ const showInfoEntry = ref(false) |
| 329 | const iframeSrc = ref(null) | 370 | const iframeSrc = ref(null) |
| 330 | // 个人信息录入弹窗的iframe地址 | 371 | // 个人信息录入弹窗的iframe地址 |
| 331 | const iframeInfoSrc = ref(null) | 372 | const iframeInfoSrc = ref(null) |
| 373 | +// iframe元素引用 | ||
| 374 | +const infoEntryIframe = ref(null) | ||
| 332 | 375 | ||
| 333 | /** | 376 | /** |
| 334 | * 监听个人信息录入弹窗状态,动态修改页面标题 | 377 | * 监听个人信息录入弹窗状态,动态修改页面标题 |
| ... | @@ -435,6 +478,11 @@ const handleInfoEntryClose = () => { | ... | @@ -435,6 +478,11 @@ const handleInfoEntryClose = () => { |
| 435 | // 写入地址查询详情 | 478 | // 写入地址查询详情 |
| 436 | iframeInfoSrc.value = itemWithForm.form_url + '&page_type=info' + '&data_id=' + formData.value.customize_id | 479 | iframeInfoSrc.value = itemWithForm.form_url + '&page_type=info' + '&data_id=' + formData.value.customize_id |
| 437 | console.log('个人信息录入弹窗关闭,iframe地址:', iframeInfoSrc.value) | 480 | console.log('个人信息录入弹窗关闭,iframe地址:', iframeInfoSrc.value) |
| 481 | + | ||
| 482 | + // 延迟调整iframe高度,等待内容加载 | ||
| 483 | + setTimeout(() => { | ||
| 484 | + adjustIframeHeight() | ||
| 485 | + }, 500) | ||
| 438 | } else { | 486 | } else { |
| 439 | console.log('未找到包含form_url的购物车项目') | 487 | console.log('未找到包含form_url的购物车项目') |
| 440 | } | 488 | } | ... | ... |
-
Please register or login to post a comment