<template> <div ref="obtain"></div> <template/> <script> export default { data() { return {} }, mounted() { // 滾動條的獲取 window.addEventListener('scroll', this.handleScrollx, true) }, methods: { handleScrollx() { console.log('滾動高度', window.pageYOffset) console.log('距離頂部高度', this.$refs.obtain.getBoundingClientRect().top) } } } </ script>
獲取當前頁面高度
export default { data() { return { // 頁面高度 screenHeight: document.body.clientHeight, // 計算出的高度 PageHeight: '' } }, created() { this.GetHeight() }, methods: { GetHeight() { // 根據頁面高度計算 (可以自定義邏輯) this.PageHeight = (this.screenHeight) } } }