当前位置: 首页 > wzjs >正文

目前最好的免费网站抖音seo优化公司

目前最好的免费网站,抖音seo优化公司,南京企业网站设计,wordpress oa 插件主要用于实时监测目标元素滚动位置及状态 工具函数源码 /*** 组合式函数* 实时监测目标元素滚动位置及状态** 自定义钩子用于处理滚动事件和状态* param target 滚动目标元素,可以是 Ref、HTMLElement、Window 或 Document,默认为 window* param throt…

主要用于实时监测目标元素滚动位置及状态

工具函数源码

/*** 组合式函数* 实时监测目标元素滚动位置及状态** 自定义钩子用于处理滚动事件和状态* @param target 滚动目标元素,可以是 Ref、HTMLElement、Window 或 Document,默认为 window* @param throttleDelay 节流延迟,用于限制滚动事件的触发频率,默认为 0* @param onScroll 滚动事件的回调函数,可选* @param onStop 滚动结束的回调函数,可选* @returns 返回一个对象,包含滚动位置和各种状态信息*/
import { ref, computed, watch, onBeforeUnmount } from 'vue'
import type { Ref } from 'vue'
import { throttle, debounce } from 'vue-amazing-ui'
export function useScroll(target: Ref | HTMLElement | Window | Document = window,throttleDelay: number = 0,onScroll?: (e: Event) => void,onStop?: (e: Event) => void
) {const x = ref(0) // 水平滚动距离const xScrollMax = ref(0) // 水平最大可滚动距离const y = ref(0) // 垂直滚动距离const yScrollMax = ref(0) // 垂直最大可滚动距离const isScrolling = ref(false) // 是否正在滚动const left = ref(false) // 是否向左滚动const right = ref(false) // 是否向右滚动const top = ref(false) // 是否向上滚动const bottom = ref(false) // 是否向下滚动const lastScrollLeft = ref(0) // 上一次水平滚动距离const lastScrollTop = ref(0) // 上一次垂直滚动距离// 滚动事件function scrollEvent(e: Event) {isScrolling.value = trueconst eventTarget = ((e.target as Document).documentElement ?? e.target) as HTMLElementx.value = eventTarget.scrollLefty.value = eventTarget.scrollTopleft.value = x.value < lastScrollLeft.valueright.value = x.value > lastScrollLeft.valuetop.value = y.value < lastScrollTop.valuebottom.value = y.value > lastScrollTop.valuelastScrollLeft.value = x.valuelastScrollTop.value = y.valuedebounceScrollEnd(e)onScroll && onScroll(e)}// 使用节流函数限制滚动事件触发频率const throttleScroll = throttle(scrollEvent, throttleDelay)// 滚动结束事件function scrollEndEvent(e: Event) {if (!isScrolling.value) {return}isScrolling.value = falseleft.value = falseright.value = falsetop.value = falsebottom.value = falseonStop && onStop(e)}// 使用防抖函数延迟处理滚动结束事件const debounceScrollEnd = debounce(scrollEndEvent, throttleDelay + 200)// 计算滚动目标元素const scrollTarget = computed(() => {const targetValue = toValue(target)if (targetValue) {return targetValue}return null})// 监听滚动目标元素的变化watch(() => scrollTarget.value,(to: any, from: any) => {if (from) {cleanup(from)}if (to) {const el: Element = ((to as Window)?.document?.documentElement ||(to as Document)?.documentElement ||(to as HTMLElement)) as ElementxScrollMax.value = el.scrollWidth - el.clientWidthyScrollMax.value = el.scrollHeight - el.clientHeightel.addEventListener('scroll', throttleScroll)el.addEventListener('scrollend', debounceScrollEnd)}},{immediate: true,flush: 'post'})// 清理函数,用于移除事件监听器function cleanup(target: any) {const el: Element = ((target as Window)?.document?.documentElement ||(target as Document)?.documentElement ||(target as HTMLElement)) as Elementel.removeEventListener('scroll', throttleScroll)el.removeEventListener('scrollend', debounceScrollEnd)}// 在组件卸载前调用清理函数onBeforeUnmount(() => cleanup(scrollTarget.value))// 返回滚动位置和各种状态信息return { x, xScrollMax, y, yScrollMax, isScrolling, left, right, top, bottom }
}

效果如下图

在这里插入图片描述

在线预览

基本使用

<script setup lang="ts">
import { ref } from 'vue'
import { useScroll } from 'vue-amazing-ui'
const scrollRef = ref()
const { x, xScrollMax, y, yScrollMax, isScrolling, left, right, top, bottom } = useScroll(scrollRef, 0, onScroll, onStop)
function onScroll(e: Event) {console.log('scroll', e)
}
function onStop(e: Event) {console.log('scrollend', e)
}
</script>
<template><Flex justify="space-between"><div class="scroll-container" ref="scrollRef" ><div class="scroll-content"><div class="inside-content">Scroll Me</div></div></div><Card title="滚动位置及状态" :body-style="{ fontSize: '16px' }"><p>水平滚动距离:{{ x }}</p><p>垂直滚动距离:{{ y }}</p><p>水平最大可滚动距离:{{ xScrollMax }}</p><p>垂直最大可滚动距离:{{ yScrollMax }}</p><p>是否正在滚动:{{ isScrolling }}</p><p>是否向左滚动:{{ left }}</p><p>是否向右滚动:{{ right }}</p><p>是否向上滚动:{{ top }}</p><p>是否向下滚动:{{ bottom }}</p></Card></Flex>
</template>
<style lang="less" scoped>
.scroll-container {width: 360px;height: 360px;border-radius: 12px;border: 2px solid #1677ff;overflow: scroll;.scroll-content {position: relative;width: 600px;height: 600px;.inside-content {position: absolute;top: 33.3%;left: 33.3%;font-size: 20px;color: rgba(0, 0, 0, 0.88);font-weight: 500;background: #fafafa;padding: 6px 8px;border-radius: 8px;}}
}
</style>

Params

参数说明类型默认值
target滚动目标元素Ref | HTMLElement | Window | Documentwindow
throttleDelay节流延迟时间,单位 ms,用于限制滚动事件的触发频率number0
onScroll?滚动事件的回调函数(e: Event) => voidundefined
onStop?滚动结束的回调函数(e: Event) => voidundefined
http://www.dtcms.com/wzjs/254112.html

相关文章:

  • 郑州汉狮做网站多少钱站长工具 忘忧草
  • 网站建设有什么工作武汉seo外包平台
  • 金山建设机械网站线下推广有哪几种渠道
  • 房产网站怎么推广宁波网站推广大全
  • 做网站分辨率多少钱北京seo怎么优化
  • 做网站费用上海seo平台是什么
  • 网站开发招标技术规范书各大网站收录查询
  • wordpress 另类加速优化百度seo
  • 做h5页面的网站口碑营销推广
  • 素材网站有哪些海外seo
  • 用discuz怎样做网站自建站模板
  • 网站开发流程图 最视频号的链接在哪
  • 个人 网站建设方案书 备案有哪些实用的网络推广方法
  • 购物网站建设课程设计报告上海有什么seo公司
  • 广州网站建设吧广东省自然资源厅
  • 营销型网站分类百度做免费推广的步骤
  • 济宁房地产网站建设seo排名优化培训怎样
  • 建设网站实验活动小结北京百度推广seo
  • 建站行业的利润网络搜索词排名
  • 征婚网站开发系统推广公司
  • 安徽网站建设价格好的seo公司营销网
  • 营销型企业网站建设板块设置百度学术搜索入口
  • 广东省网站免备案合肥seo排名收费
  • 深圳机票网站建设站长之家seo综合查询
  • 中国住房和城乡建设部招标网站图片外链生成工具在线
  • 青岛专业网站制作团队2023年7月疫情爆发
  • 一起做网店的类似网站优帮云查询数据云查询
  • 阿里云做淘宝客网站吗怎么建设自己的网站
  • 佛山做礼物的网站优化外包服务公司
  • 南平购物网站开发设计网站优化排名金苹果下拉