【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>
相关推荐
科杰智能制造20 分钟前
纯前端html、js实现人脸检测和表情检测,可直接在浏览器使用
前端·javascript·html
每天吃饭的羊37 分钟前
组件库的有些点击事件是name-click这是如何分装de
前端·javascript·vue.js
Coder-coco37 分钟前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
x***01061 小时前
SpringSecurity+jwt实现权限认证功能
android·前端·后端
1024肥宅1 小时前
防抖(Debounce)
前端·javascript·ecmascript 6
1024肥宅1 小时前
节流(Throttle)
前端·javascript·ecmascript 6
大怪v1 小时前
【Virtual World 02】两点一线!!!
javascript·css·html
by__csdn1 小时前
Vue2纯前端图形验证码实现详解+源码
前端·javascript·typescript·vue·状态模式·css3·canva可画
Gomiko1 小时前
JavaScript基础(八):函数
开发语言·javascript·ecmascript
w***37511 小时前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring