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

无锡电子商务网站制作软件程序开发公司

无锡电子商务网站制作,软件程序开发公司,工程项目信息查询,长春好的做网站公司有哪些功能说明 ​​拖动功能​​: 鼠标按下时记录初始位置和滚动位置拖动过程中计算移动距离并更新滚动位置松开鼠标后根据速度实现惯性滚动 ​​滚动控制​​: 支持鼠标滚轮横向滚动(通过 wheel 事件)自动边界检测防止滚动超出内容…

功能说明

​​拖动功能​​:

  • 鼠标按下时记录初始位置和滚动位置
  • 拖动过程中计算移动距离并更新滚动位置
  • 松开鼠标后根据速度实现惯性滚动

​​滚动控制​​:

  • 支持鼠标滚轮横向滚动(通过 wheel 事件)
  • 自动边界检测防止滚动超出内容范围

实现代码

<template><div ref="scrollContainer" class="horizontal-scroll-container"@mousedown="startDrag"@mousemove="onDrag"@mouseup="stopDrag"@mouseleave="stopDrag"><div class="scroll-content"><div v-for="(item, index) in items" :key="index" class="item">{{ item }}</div></div></div>
</template><script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'// 定义滚动容器引用
const scrollContainer = ref<HTMLElement | null>(null)// 定义滚动内容数据
const items = ref<string[]>(Array.from({ length: 20 }, (_, i) => `Item ${i + 1}`))// 拖动状态变量
let isDragging = false
let startX = 0
let scrollLeft = 0
let lastTime = 0
let velocity = 0// 开始拖动
const startDrag = (e: MouseEvent): void => {if (!scrollContainer.value) returnisDragging = truestartX = e.pageX - scrollContainer.value.getBoundingClientRect().leftscrollLeft = scrollContainer.value.scrollLeftlastTime = performance.now()scrollContainer.value.style.cursor = 'grabbing'
}// 拖动中
const onDrag = (e: MouseEvent): void => {if (!isDragging || !scrollContainer.value) returnconst x = e.pageX - scrollContainer.value.getBoundingClientRect().leftconst walk = (x - startX) * 1.5 // 调整滚动速度系数scrollContainer.value.scrollLeft = scrollLeft - walk// 计算速度(用于惯性滚动)const now = performance.now()velocity = (x - startX) / (now - lastTime)lastTime = now
}// 停止拖动
const stopDrag = (): void => {if (!isDragging || !scrollContainer.value) returnisDragging = falseif (Math.abs(velocity) > 0.1) {requestAnimationFrame(inertiaScroll)}scrollContainer.value.style.cursor = 'grab'
}// 惯性滚动
const inertiaScroll = (): void => {if (!scrollContainer.value || Math.abs(velocity) < 0.01) returnscrollContainer.value.scrollLeft += velocity * 10velocity *= 0.95 // 摩擦系数requestAnimationFrame(inertiaScroll)
}// 边界检测
const checkBounds = (): void => {if (!scrollContainer.value) returnconst containerWidth = scrollContainer.value.clientWidthconst contentWidth = scrollContainer.value.scrollWidthif (scrollContainer.value.scrollLeft < 0) {scrollContainer.value.scrollLeft = 0} else if (scrollContainer.value.scrollLeft > contentWidth - containerWidth) {scrollContainer.value.scrollLeft = contentWidth - containerWidth}
}// 鼠标滚轮横向滚动
const handleWheel = (e: WheelEvent): void => {if (!scrollContainer.value) returne.preventDefault()scrollContainer.value.scrollLeft += e.deltaYcheckBounds()
}// 生命周期钩子
onMounted(() => {if (scrollContainer.value) {scrollContainer.value.addEventListener('wheel', handleWheel, { passive: false })}
})onUnmounted(() => {if (scrollContainer.value) {scrollContainer.value.removeEventListener('wheel', handleWheel)}
})
</script><style scoped>
.horizontal-scroll-container {width: 100%;overflow-x: auto;white-space: nowrap;cursor: grab;height: 220px; /* 确保容器有固定高度 */border: 1px solid #eee;border-radius: 8px;padding: 10px;box-sizing: border-box;
}.horizontal-scroll-container:active {cursor: grabbing;
}.scroll-content {display: inline-block;
}.item {display: inline-block;width: 200px;height: 200px;margin-right: 10px;background: #f0f0f0;display: flex;align-items: center;justify-content: center;font-size: 20px;border: 1px solid #ddd;box-sizing: border-box;border-radius: 4px;transition: transform 0.2s ease, box-shadow 0.2s ease;
}.item:hover {transform: scale(1.02);box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>
http://www.dtcms.com/wzjs/811034.html

相关文章:

  • 开发网站建设的问卷调查首页关键词排名
  • MIUI官方网站开发版试玩网站建设
  • 建设网站公司兴田德润广东省最差的三座城市
  • 河南网站建设的详细策划seo优化工作内容
  • 高端网站鉴赏接单子做网站
  • 程序员怎么做网站赚钱如何建设网站论文文献
  • 苏州企业建站系统模板网站建设包括网页设计
  • 深圳一元购网站建设网络营销培训机构排名
  • 网站怎么增加页面收录wordpress 木马
  • 网赌网站怎么做免费空间备案
  • 做网站都需要哪些软硬件wordpress+站群插件
  • 曹县商城网站建设网络优化及服务的工作任务
  • 重慶网站开发深圳前十网站建设公司
  • 江苏大丰做网站百度招聘2022年最新招聘
  • psdw做网站wordpress小工具文本图标
  • 网站内链html编写新闻页面
  • 常州本地做网站的大公司做一个网站指定页面的推广
  • 广州网站营销推广设计wordpress获取文章时间
  • wap网站开发方案紫鸟超级浏览器官网下载
  • 微商的自己做网站叫什么名字网页制作三剑客指的是
  • 兰州网站建设网站建设响应式电商网站制作
  • 做网站专业公司电话qa wordpress
  • 利用ps制作网站的设计wordpress显示文章简介
  • 单位网站维护 网站建设岗位个人网站名称举例
  • 昌乐网站设计wordpress搜索框使用
  • 巴中微网站建设商城网站建设的优点
  • 网站建设好多钱关键词调整排名软件
  • 响应式地方网站wordpress优化检测
  • 活动发布类网站开发谷歌官方app下载
  • 做seo为什么要了解网站wordpress算数验证