flat: 暂存
This commit is contained in:
BIN
hook/.DS_Store
vendored
BIN
hook/.DS_Store
vendored
Binary file not shown.
44
hook/useColumnCount.js
Normal file
44
hook/useColumnCount.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// composables/useColumnCount.js
|
||||
import {
|
||||
ref,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
watch
|
||||
} from 'vue'
|
||||
|
||||
export function useColumnCount(onChange = () => {}) {
|
||||
const columnCount = ref(0)
|
||||
|
||||
const calcColumn = () => {
|
||||
const width = uni.getSystemInfoSync().windowWidth
|
||||
const count = Math.min(5, Math.floor(width / 375) + 1)
|
||||
if (count !== columnCount.value) {
|
||||
columnCount.value = count
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
columnCount.value = 2
|
||||
calcColumn()
|
||||
if (process.client) {
|
||||
window.addEventListener('resize', calcColumn)
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (process.client) {
|
||||
window.removeEventListener('resize', calcColumn)
|
||||
}
|
||||
})
|
||||
|
||||
// 列数变化时执行回调
|
||||
watch(columnCount, (newVal, oldVal) => {
|
||||
if (newVal !== oldVal) {
|
||||
onChange(newVal)
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
columnCount,
|
||||
}
|
||||
}
|
49
hook/useScrollDirection.js
Normal file
49
hook/useScrollDirection.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
ref
|
||||
} from 'vue'
|
||||
|
||||
export function useScrollDirection(options = {}) {
|
||||
const {
|
||||
threshold = 200, // 滚动偏移阈值
|
||||
throttleTime = 100, // 节流时间(毫秒)
|
||||
onChange = null // 滚动方向变化的回调
|
||||
} = options
|
||||
|
||||
const lastScrollTop = ref(0)
|
||||
const accumulatedScroll = ref(0)
|
||||
const isScrollingDown = ref(false)
|
||||
let lastInvoke = 0
|
||||
|
||||
function handleScroll(e) {
|
||||
const now = Date.now()
|
||||
if (now - lastInvoke < throttleTime) return
|
||||
lastInvoke = now
|
||||
|
||||
const scrollTop = e.detail.scrollTop
|
||||
const delta = scrollTop - lastScrollTop.value
|
||||
accumulatedScroll.value += delta
|
||||
|
||||
if (accumulatedScroll.value > threshold) {
|
||||
if (!isScrollingDown.value) {
|
||||
isScrollingDown.value = true
|
||||
onChange?.(true) // 通知变更为向下
|
||||
}
|
||||
accumulatedScroll.value = 0
|
||||
}
|
||||
|
||||
if (accumulatedScroll.value < -threshold) {
|
||||
if (isScrollingDown.value) {
|
||||
isScrollingDown.value = false
|
||||
onChange?.(false) // 通知变更为向上
|
||||
}
|
||||
accumulatedScroll.value = 0
|
||||
}
|
||||
|
||||
lastScrollTop.value = scrollTop
|
||||
}
|
||||
|
||||
return {
|
||||
isScrollingDown,
|
||||
handleScroll
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user