vue中mousewheel滚动

vue中mousewheel滚动效果点击上下可以中间滚动 鼠标在框内滚动也会滚动 外部 jsconstmouse wheel opt console log opt if opt target console log 什么都没有 还让我帮忙 return

效果

vue中mousewheel滚动

点击上下可以中间滚动,鼠标在框内滚动也会滚动。

外部js

const mouseEvent = { wheel(opt){ console.log(opt) if(!opt.target){ console.log('什么都没有,还让我帮忙!'); return ; } let callback = opt.callback || function () {}; let target = opt.target; //获取兼容事件 let mouseWheel=(/Firefox/i.test(navigator.userAgent))?"DOMMouseScroll": "mousewheel"; //IE if(document.attachEvent){ document.attachEvent('on'+mouseWheel,function (e) { if(e.target.parentElement == target){ callback(e, e.wheelDelta) } }) } //FF、Chrome、Opera、safari else{ document.addEventListener(mouseWheel,function (e) { e = e || window.event; if(e.target.parentElement == target){ if(e.detail){ //FF callback(e,e.detail*40) }else{ callback(e,e.wheelDelta) } } }) } } }; export { mouseEvent }

在图中模块处引入,代码如下

<template> <div class="text-carousel-group"> <div class="go-up mt-10" @click="goUp">︿</div> <div class="content-wrapper" ref="carouselBox"> <div class="content moveContent" ref="carouselContent" :style="{transform:'translateY('+transLateY+'px)'}"> <p v-for="(item,index) in lists" :key="item.id" :class="{active:item.selected}" @click="selectEle(item)">{ 
  {item.text}}</p> </div> </div> <div class="go-down mb-10" @click="goDown">﹀</div> </div> </template> <script> import {mouseEvent} from '../../assets/js/common' export default { name: "text-carousel", props:['lists'], data(){ return { carouselEle:0, //滚动元素 carouselBox:0, //滚动盒子 carouselEleH:0, //滚动元素 carouselBoxH:0, //滚动盒子 transLateY:0 //滚动Y轴距离 } }, methods:{ goUp(){ if(this.carouselEleH + this.transLateY <= this.carouselBoxH){ this.transLateY = this.carouselBoxH - this.carouselEleH >0 ? 0:this.carouselBoxH - this.carouselEleH; }else{ this.transLateY -= 29; } }, goDown(){ if(this.transLateY>=0){ this.transLateY= 0; }else{ this.transLateY += 29; } }, selectEle(str){ this.$emit('myChange',str) }, }, mounted(){ let This = this; window.addEventListener('scroll',this.handleScroll,true) this.carouselBox = this.$refs.carouselBox; this.carouselEle = this.$refs.carouselContent; this.carouselBoxH = this.$refs.carouselBox.offsetHeight; this.carouselEleH = this.$refs.carouselContent.offsetHeight; this.transLateY = 0; //将选中元素显示在中间 var curIndex = 0; this.lists.forEach(function (value,index) { if(value.selected){ curIndex = index; } }); if(this.carouselBoxH<this.carouselEleH){ if(curIndex>3){ if( curIndex < this.lists.length - 3){ this.transLateY = -(curIndex - 3)*29 }else if(curIndex >= this.lists.length - 3){ this.transLateY = this.carouselBoxH - this.carouselEleH } } }; mouseEvent.wheel({ target: This.carouselEle, callback(e, direction){ if(direction>0){ This.goUp(); }else{ This.goDown(); } } }) }, watch:{ } } </script> <style scoped> p{ height: 29px; line-height: 29px; cursor: pointer; } p:hover, p.active{ background-color: rgba(0, 0, 0, 0.2); } .go-up,.go-down{ height: 29px; line-height: 29px; color: #fff; cursor: pointer; user-select: none; } .content p{ color: rgba(255,255,255,0.6); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } </style>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231207.html原文链接:https://javaforall.net

(0)
上一篇 2026年1月22日 上午8:01
下一篇 2026年1月22日 上午8:22


相关推荐

  • mysql一主多从 读写分离_MySQL主从复制原理

    mysql一主多从 读写分离_MySQL主从复制原理文章目录前言一、基本概念1.读写分离(1)什么是读写分离(2)为什么要读写分离(3)什么时候要读写分离(4)主从复制与读写分离2.MySQL主从复制(1)mysql支持的复制类型(2)主从复制的工作过程(3)mysql主从复制高延迟的原因(4)mysql主从复制高延迟的解决办法3.常见的MySQL读写分离方式(1)基于程序代码内部实现(2)基于中间代理层实现二、MySQL主从复制架构搭建1.服务器配置2.实验前准备3.mysql主从服务器时间同步4.主服务器

    2022年8月13日
    7
  • VMware许可证过期_windows许可证过期什么原因

    VMware许可证过期_windows许可证过期什么原因激活密钥VMware2017v14.x永久许可证激活密钥FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD这表文章解决了大部分的常见问题,大家可以去看看http://www.zdfans.com/5928.html

    2025年11月17日
    9
  • 华为交换机vlan配置教程

    华为交换机vlan配置教程Sys  \\进入系统视图Dhcpenable \\全局开启dhcp功能Vlanbacth234 \\批量创建vlan 234Intvlanif2 \\进入vlan 2Ipadd192.168.xxx.1 255.255.255.0 \\为vlan2分配IP地址及子网掩码Dhcpselectglobal\\dhcp选择全局开启Q \…

    2025年11月22日
    6
  • Hibernate初级入门

    Hibernate初级入门

    2021年7月10日
    88
  • 对java中public、static的理解

    对java中public、static的理解原帖:https://blog.csdn.net/v7595v/article/details/45845347首先是public 在说明这四个关键字之前,我想就class之间的关系做一个简单的定义,对于继承自己的class,baseclass可以认为他们都是自己的子女,而对于和自己一个目录下的classes,认为都是自己的朋友。 1、public:public表明…

    2022年7月25日
    21
  • soapclient php 扩展,PHP扩展—SOAP[通俗易懂]

    soapclient php 扩展,PHP扩展—SOAP[通俗易懂]一、概述及安装SOAP扩展可以用于编写SOAP服务器和客户端,支持SOAP1.1,SOAP1.2和WSDL1.1规格的子集。此扩展需要libxmlPHP扩展。这表示需要使用–enable-libxml,尽管这将隐式完成因为libxml是缺省开启的。要开启SOAP支持,配置PHP时要加上–enable-soap.二、相关函数is_soap_fault—…

    2025年6月1日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号