Showing
1 changed file
with
48 additions
and
20 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2024-09-26 13:42:11 | 2 | * @Date: 2024-09-26 13:42:11 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2024-11-07 14:28:25 | 4 | + * @LastEditTime: 2024-11-07 14:34:27 |
| 5 | * @FilePath: /hager/src/components/common/hagerHeader.vue | 5 | * @FilePath: /hager/src/components/common/hagerHeader.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -140,6 +140,24 @@ import $ from 'jquery'; | ... | @@ -140,6 +140,24 @@ import $ from 'jquery'; |
| 140 | import hagerMenu from '../hagerMenu.vue'; | 140 | import hagerMenu from '../hagerMenu.vue'; |
| 141 | import { getProductCateAPI, getUserInfoAPI } from "@/api/hager.js"; | 141 | import { getProductCateAPI, getUserInfoAPI } from "@/api/hager.js"; |
| 142 | 142 | ||
| 143 | +// 处理菜单高度被挡住的问题 | ||
| 144 | +function isElementVisible($element) { | ||
| 145 | + var windowHeight = $(window).height(); // 获取窗口高度 | ||
| 146 | + var windowScrollTop = $(window).scrollTop(); // 获取窗口的滚动高度 | ||
| 147 | + | ||
| 148 | + var elementOffsetTop = $element.offset().top; // 获取元素的顶部偏移 | ||
| 149 | + var elementHeight = $element.outerHeight(); // 获取元素高度 | ||
| 150 | + | ||
| 151 | + // 判断元素是否完全在可视区域内 | ||
| 152 | + if (elementOffsetTop >= windowScrollTop && | ||
| 153 | + elementOffsetTop + elementHeight <= windowScrollTop + windowHeight) { | ||
| 154 | + return true; // 元素完全可见 | ||
| 155 | + } else if (elementOffsetTop < windowScrollTop || | ||
| 156 | + elementOffsetTop + elementHeight > windowScrollTop + windowHeight) { | ||
| 157 | + return false; // 元素被挡住或部分被遮挡 | ||
| 158 | + } | ||
| 159 | +} | ||
| 160 | + | ||
| 143 | export default { | 161 | export default { |
| 144 | components: { hagerMenu }, | 162 | components: { hagerMenu }, |
| 145 | mixins: [mixin.init], | 163 | mixins: [mixin.init], |
| ... | @@ -215,9 +233,36 @@ export default { | ... | @@ -215,9 +233,36 @@ export default { |
| 215 | // } | 233 | // } |
| 216 | // }); | 234 | // }); |
| 217 | // | 235 | // |
| 218 | - | 236 | + window.addEventListener('resize', this.handleHeightResize); |
| 237 | + }, | ||
| 238 | + beforeDestroy() { | ||
| 239 | + // 在组件销毁前移除监听器,防止内存泄漏 | ||
| 240 | + window.removeEventListener('resize', this.handleHeightResize); | ||
| 219 | }, | 241 | }, |
| 220 | methods: { | 242 | methods: { |
| 243 | + handleHeightResize () { | ||
| 244 | + // 高度监听 | ||
| 245 | + this.$nextTick(() => { | ||
| 246 | + var $productRightWrapper = $('.product-right-wrapper'); | ||
| 247 | + const productRightWrapper = document.querySelector('.product-right-wrapper'); | ||
| 248 | + // 获取窗口的高度 | ||
| 249 | + const windowHeight = $(window).height(); | ||
| 250 | + // 计算适合的高度,假设元素的 top 值固定,调整高度为可用高度 | ||
| 251 | + const topOffset = $('.product-right-wrapper').offset().top; // 获取元素距离顶部的偏移量 | ||
| 252 | + const availableHeight = windowHeight - topOffset; | ||
| 253 | + | ||
| 254 | + if (isElementVisible($productRightWrapper)) { | ||
| 255 | + console.log(".product-right-wrapper 完全可见"); | ||
| 256 | + productRightWrapper.style.removeProperty('height'); | ||
| 257 | + productRightWrapper.style.removeProperty('overflow-y'); | ||
| 258 | + } else { | ||
| 259 | + console.log(".product-right-wrapper 被部分或完全遮挡"); | ||
| 260 | + // 设置 .product-right-wrapper 的最大高度,确保不会溢出窗口 | ||
| 261 | + productRightWrapper.style.height = availableHeight + 'px'; | ||
| 262 | + productRightWrapper.style.overflowY = 'scroll'; | ||
| 263 | + } | ||
| 264 | + }); | ||
| 265 | + }, | ||
| 221 | async getUserInfo () { | 266 | async getUserInfo () { |
| 222 | const { code, data } = await getUserInfoAPI(); | 267 | const { code, data } = await getUserInfoAPI(); |
| 223 | if (code) { | 268 | if (code) { |
| ... | @@ -267,23 +312,6 @@ export default { | ... | @@ -267,23 +312,6 @@ export default { |
| 267 | onClickProduct() { | 312 | onClickProduct() { |
| 268 | // 当鼠标悬停时 | 313 | // 当鼠标悬停时 |
| 269 | this.show = true; | 314 | this.show = true; |
| 270 | - // 处理菜单高度被挡住的问题 | ||
| 271 | - function isElementVisible($element) { | ||
| 272 | - var windowHeight = $(window).height(); // 获取窗口高度 | ||
| 273 | - var windowScrollTop = $(window).scrollTop(); // 获取窗口的滚动高度 | ||
| 274 | - | ||
| 275 | - var elementOffsetTop = $element.offset().top; // 获取元素的顶部偏移 | ||
| 276 | - var elementHeight = $element.outerHeight(); // 获取元素高度 | ||
| 277 | - | ||
| 278 | - // 判断元素是否完全在可视区域内 | ||
| 279 | - if (elementOffsetTop >= windowScrollTop && | ||
| 280 | - elementOffsetTop + elementHeight <= windowScrollTop + windowHeight) { | ||
| 281 | - return true; // 元素完全可见 | ||
| 282 | - } else if (elementOffsetTop < windowScrollTop || | ||
| 283 | - elementOffsetTop + elementHeight > windowScrollTop + windowHeight) { | ||
| 284 | - return false; // 元素被挡住或部分被遮挡 | ||
| 285 | - } | ||
| 286 | - } | ||
| 287 | this.$nextTick(() => { | 315 | this.$nextTick(() => { |
| 288 | var $productRightWrapper = $('.product-right-wrapper'); | 316 | var $productRightWrapper = $('.product-right-wrapper'); |
| 289 | 317 | ||
| ... | @@ -301,7 +329,7 @@ export default { | ... | @@ -301,7 +329,7 @@ export default { |
| 301 | productRightWrapper.style.height = availableHeight + 'px'; | 329 | productRightWrapper.style.height = availableHeight + 'px'; |
| 302 | productRightWrapper.style.overflowY = 'scroll'; | 330 | productRightWrapper.style.overflowY = 'scroll'; |
| 303 | } | 331 | } |
| 304 | - }) | 332 | + }); |
| 305 | }, | 333 | }, |
| 306 | onClick (item, index) { | 334 | onClick (item, index) { |
| 307 | this.product_info = item.children; | 335 | this.product_info = item.children; | ... | ... |
-
Please register or login to post a comment