【Vue】鼠标滚轮横向滚动操作设计

需求:

鼠标滑轮滚动,操作横向滚动条

解决:

监控滚动操作,根据滚动偏移量,修改横向滚动条的位置

js 复制代码
<template>
  <div class="image_view">
    <div class="image_content">
      <div
        v-for="(item, index) in 20"
        :key="item"
        class="image_item"
        :style="{ background: colors[index % colors.length] }"
      ></div>
    </div>
  </div>
</template>
 
<script setup>
import TipsFooterVue from "@/components/base/TipsFooter.vue";
import { onMounted } from "vue";
 
const colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"];
 
onMounted(() => {
  // 获取 dom
  let div = document.querySelector(".image_content");
  // 监听 dom
  div.addEventListener("wheel", function (e) {
    let left = -e.wheelDelta || e.deltaY / 2;
    console.log("wheelDelta:", -e.wheelDelta, "deltaY :", e.deltaY);
    // 修改滚动条位置
    div.scrollLeft = div.scrollLeft + left;
  });
});
</script>
 
<style lang="less" scoped>
.image_view {
  width: 100%;
  .image_content {
    width: 100%;
    height: 200px;
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    .image_item {
      width: 300px;
      height: 200px;
      flex: 0 0 auto;
    }
  }
}
</style>
相关推荐
Mintopia5 分钟前
一个月速成 AI 工程师:从代码小白到智能工匠的修炼手册
前端·javascript·aigc
Mintopia8 分钟前
Next.js 全栈:接收和处理请求
前端·javascript·next.js
袁煦丞42 分钟前
2025.8.18实验室【代码跑酷指南】Jupyter Notebook程序员的魔法本:cpolar内网穿透实验室第622个成功挑战
前端·程序员·远程工作
Joker Zxc1 小时前
【前端基础】flex布局中使用`justify-content`后,最后一行的布局问题
前端·css
无奈何杨1 小时前
风控系统事件分析中心,关联关系、排行、时间分布
前端·后端
Moment1 小时前
nginx 如何配置防止慢速攻击 🤔🤔🤔
前端·后端·nginx
晓得迷路了1 小时前
栗子前端技术周刊第 94 期 - React Native 0.81、jQuery 4.0.0 RC1、Bun v1.2.20...
前端·javascript·react.js
江城开朗的豌豆1 小时前
React Native 实战心得
javascript
前端小巷子1 小时前
Vue 自定义指令
前端·vue.js·面试