1、获取小程序顶部/胶囊元素信息
export function getBoundInfo() {
const systemInfo = uni.getSystemInfoSync()
if (systemInfo.uniPlatform !== 'mp-weixin') {
return {
statusBarHeight: 0,
boundWidth: 0,
boundTop: 20,
}
}
const statusBarHeight = systemInfo.statusBarHeight
const boundWidth = uni.getMenuButtonBoundingClientRect()?.width || 0
const boundHeight = uni.getMenuButtonBoundingClientRect()?.height || 0
const boundTop = uni.getMenuButtonBoundingClientRect()?.top || statusBarHeight
return {
statusBarHeight,
boundWidth,
boundHeight,
boundTop,
}
}
2、控制元素滚动到顶部/底部
useScroll.js (这里一定要用防抖,不然小程序内会有问题, ,防抖函数网上自己找吧,这里就不写了)
import { ref } from 'vue'
import { debounce } from '@/utils'
const useScroll = () => {
const scrollTop = ref(0)
const handleScroll = debounce((e) => {
scrollTop.value = e.detail.scrollTop
}, 500)
const scrollToTop = () => {
scrollTop.value = 0
}
const scrollToBottom = () => {
scrollTop.value = 9999
}
return {
scrollTop,
handleScroll,
scrollToTop,
scrollToBottom,
}
}
export { useScroll }
const { scrollTop, handleScroll, scrollToTop } = useScroll()
defineExpose({
scrollToTop,
})
<scroll-view
class="u-scroll-view"
:scroll-y="true"
lower-threshold="100"
:scroll-top="scrollTop"
:scroll-with-animation="true"
@scroll="handleScroll"
@scrolltolower="loadMore"
>
</scroll-view>
3、监听左右滑动事件
import { ref } from 'vue'
const useSlipDirection = (callback) => {
const startX = ref(0)
const delTax = ref(0)
const touchStart = (event) => {
startX.value = event.touches[0].clientX
}
const touchMove = (event) => {
delTax.value = event.touches[0].clientX - startX.value
}
const touchEnd = (event) => {
console.log(16, delTax.value)
if (delTax.value > 50) {
console.log('right')
delTax.value = 0
callback('right')
} else if (delTax.value < -50) {
console.log('left')
delTax.value = 0
callback('left')
}
}
return {
touchStart,
touchMove,
touchEnd,
}
}
export { useSlipDirection }
const { touchStart, touchMove, touchEnd } = useSlipDirection(handleTouchMove)
<view
v-if="componentVisible"
class="inner-content"
:style="{ height: `calc(100% - ${statusBarHeight * 2 + 204 + 116}rpx)` }"
@touchstart="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
>
</view>
4、获取dom元素信息
export function getElRectAsync(query) {
return new Promise((resolve) => {
uni.getSystemInfo({
success: function (res) {
let info = uni.createSelectorQuery().select(query)
info
.boundingClientRect(function (data) {
resolve(data)
})
.exec()
},
})
})
}
getElRectAsync('.top-row').then((res) => {
variables.topRowHeight = res.height
})