首页 > 其他分享 >Vue横向滚动条拖动事件

Vue横向滚动条拖动事件

时间:2023-05-29 21:33:06浏览次数:33  
标签:function startX Vue 拖动 scrollLeft 滚动条 isDragging scrollContainer event

<template>
  <div class="scroll-container" ref="scrollContainer"
       v-on:mousedown="handleMouseDown"
       v-on:mousemove="handleMouseMove"
       v-on:mouseup="handleMouseUp">
    <div class="scroll-content">
      <!-- 横向内容 -->
    </div>
  </div>
</template>

<style>
.scroll-container {
  overflow-x: scroll;
  white-space: nowrap;
}

.scroll-content {
  /* 横向内容样式 */
}
</style>
<script setup lang="ts">
import { ref } from 'vue'

let isDragging = false
let startX = 0
let scrollLeft = 0

const scrollContainer = ref(null)

function handleMouseDown(event: MouseEvent) {
  isDragging = true;
  startX = event.clientX;
  scrollLeft = scrollContainer.value.scrollLeft;
}

function handleMouseMove(event: MouseEvent) {
  if (!isDragging) return;
  const x = event.clientX - startX;
  scrollContainer.value.scrollLeft = scrollLeft - x;
}

function handleMouseUp() {
  isDragging = false;
}
</script>

标签:function,startX,Vue,拖动,scrollLeft,滚动条,isDragging,scrollContainer,event
From: https://www.cnblogs.com/pphboy/p/17441726.html

相关文章

  • Vue3+TS后台项目笔记
    Date:2023-05-2917:56:27Author:GavinPS:不喜欢做复制粘贴,这篇笔记只是简写关键P1~12Vue3中的通信方式props父=>子传的为不可变数据自定义事件子=>父全局事件总线任意组件mitt实现v-model父<=>子写在组件标签上,实现props和自定义事件ref子=>父子组件需e......
  • vue-cli 的配置和使用
    vue-cli介绍vue-cli是Vue.js开发的标准工具。它简化了程序员基于webpack创建工程化的Vue项目的过程。中文官网在工程化的项目中,vue要做的事情很单纯:通过main.js把App.vue渲染到index.html的指定区域中。配置和使用Vue-CLI的安装、使用及环境配置(超详细)vue项......
  • vue导航吸顶
    所说的吸顶效果就是在页面没有滑动之前,当页面向上滑动之后,导航栏始终固定在页面的上方。具体代码:写入事件监听,监听滚动条。mounted(){//事件监听滚动条window.addEventListener('scroll',this.watchScroll,true)}然后在methods中写入watchScroll方法。......
  • 线上环境如何开启vue devtool
    varVue,walker,node;walker=document.createTreeWalker(document.body,1);while((node=walker.nextNode())){if(node.__vue__){Vue=node.__vue__.$options._base;if(!Vue.config.devtools){Vue.config.devtools=true;if(windo......
  • vue对kindeditor进行封装以及使用
    下载kindeditor后需要使用的文件如下(其他没用到的可删除)封装成组件以及自定义上传插件上传图片后生成image标签插入上传文件后生成下载a链接插入<template><divclass="kindeditor-component"><inputid="file-input"style="display:none"type="file"&g......
  • vue this.$route.query 和this.$route.params的使用与区别
    一:this.$route.query的使用#1、传参数:this.$router.push({path:'/index/detail',query:{itemId:item.id}});#2、获取参数this.$route.query.itemId#3、url的表现形式http://localhost:8080/#/index/detail?itemId=22二:this.$route.params的使......
  • 直播app开发搭建,Vue Element UI Upload 上传多张图片
    直播app开发搭建,VueElementUIUpload上传多张图片<template> <div>  <el-card>   <h1>轮播图-图片上传管理</h1>   <el-divider></el-divider>   <!--注意:使用:model="uploadImgForm"不要使用v-model="uploadImgForm&q......
  • vue-封装组件-超出部分限制...,并且提示
    显示效果 代码:<template><divclass="tip"><el-tooltip:content="content"placement="top"width="400":disabled="!isShowTooltip"><spanclass="me......
  • vue中el-select 多选限制条件(根据不同选项进行不同可选可不选)
    首先看一个数据结构:1:无缺陷2:有缺陷-》缺陷1,缺陷2,缺陷33:审核不通过把它们集成到一个平面数据,进行下拉所选就变成了:1:无缺陷,2:审核不通过,3:缺陷一,4:缺陷二,5:缺陷三。因为字典项是个平面的,所以在类似的大批量的情况下,有缺陷这个类如果还按照这种结构除非自己去归类。想要直接映射到......
  • vue3学习中使用vue-router@4的问题Invalid VNode type: undefined (undefined)
    首先是按照常规的箭头函数引入的方法,结果报一下错误,且页面报错constHelloWorld=()=>import('../components/HelloWorld.vue'); 解决办法import{defineAsyncComponent}from'vue'constHelloWorld=defineAsyncComponent(()=>import('../components/HelloWorld.vue......