触摸屏上的“滚轮”事件

2023-12-21

我有这个小提琴https://jsfiddle.net/316n1xmL/1/ https://jsfiddle.net/316n1xmL/1/这在桌面上完美地满足了我的需要。根据滚轮滚动方向向上或向下计数并添加和删除类。我遇到的问题是如何在触摸屏上执行此操作。

我尝试过hammer.js,但他们的文档非常糟糕而且不清楚。

遗憾的是,我正在构建的网站使用的是 bootstrap 4,这意味着我无法使用 juqery mobile,因为 BS4 使用不支持 jquery mobile 的 jquery 3。

任何帮助表示赞赏

// Scroll Direction Plugin Move Later to own file

!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a:a(jQuery)}(function(a){function b(b){var g=b||window.event,h=i.call(arguments,1),j=0,l=0,m=0,n=0,o=0,p=0;if(b=a.event.fix(g),b.type="mousewheel","detail"in g&&(m=-1*g.detail),"wheelDelta"in g&&(m=g.wheelDelta),"wheelDeltaY"in g&&(m=g.wheelDeltaY),"wheelDeltaX"in g&&(l=-1*g.wheelDeltaX),"axis"in g&&g.axis===g.HORIZONTAL_AXIS&&(l=-1*m,m=0),j=0===m?l:m,"deltaY"in g&&(m=-1*g.deltaY,j=m),"deltaX"in g&&(l=g.deltaX,0===m&&(j=-1*l)),0!==m||0!==l){if(1===g.deltaMode){var q=a.data(this,"mousewheel-line-height");j*=q,m*=q,l*=q}else if(2===g.deltaMode){var r=a.data(this,"mousewheel-page-height");j*=r,m*=r,l*=r}if(n=Math.max(Math.abs(m),Math.abs(l)),(!f||f>n)&&(f=n,d(g,n)&&(f/=40)),d(g,n)&&(j/=40,l/=40,m/=40),j=Math[j>=1?"floor":"ceil"](j/f),l=Math[l>=1?"floor":"ceil"](l/f),m=Math[m>=1?"floor":"ceil"](m/f),k.settings.normalizeOffset&&this.getBoundingClientRect){var s=this.getBoundingClientRect();o=b.clientX-s.left,p=b.clientY-s.top}return b.deltaX=l,b.deltaY=m,b.deltaFactor=f,b.offsetX=o,b.offsetY=p,b.deltaMode=0,h.unshift(b,j,l,m),e&&clearTimeout(e),e=setTimeout(c,200),(a.event.dispatch||a.event.handle).apply(this,h)}}function c(){f=null}function d(a,b){return k.settings.adjustOldDeltas&&"mousewheel"===a.type&&b%120===0}var e,f,g=["wheel","mousewheel","DOMMouseScroll","MozMousePixelScroll"],h="onwheel"in document||document.documentMode>=9?["wheel"]:["mousewheel","DomMouseScroll","MozMousePixelScroll"],i=Array.prototype.slice;if(a.event.fixHooks)for(var j=g.length;j;)a.event.fixHooks[g[--j]]=a.event.mouseHooks;var k=a.event.special.mousewheel={version:"3.1.12",setup:function(){if(this.addEventListener)for(var c=h.length;c;)this.addEventListener(h[--c],b,!1);else this.onmousewheel=b;a.data(this,"mousewheel-line-height",k.getLineHeight(this)),a.data(this,"mousewheel-page-height",k.getPageHeight(this))},teardown:function(){if(this.removeEventListener)for(var c=h.length;c;)this.removeEventListener(h[--c],b,!1);else this.onmousewheel=null;a.removeData(this,"mousewheel-line-height"),a.removeData(this,"mousewheel-page-height")},getLineHeight:function(b){var c=a(b),d=c["offsetParent"in a.fn?"offsetParent":"parent"]();return d.length||(d=a("body")),parseInt(d.css("fontSize"),10)||parseInt(c.css("fontSize"),10)||16},getPageHeight:function(b){return a(b).height()},settings:{adjustOldDeltas:!0,normalizeOffset:!0}};a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})});

$(document).ready(function() {
    
    var counter = 0;
    var scrollThreshold = 15;

    var tiles = $('.list-tile');

    $(window).on('wheel', function(event){
        // deltaY obviously records vertical scroll, deltaX and deltaZ exist too
        if(event.originalEvent.deltaY < 0){
          // wheeled up
          counter--;
            if (Math.abs(counter) >= scrollThreshold) {
                tiles.filter('.animate-up').not('.first').last().removeClass('animate-up');
                counter = 0;
            }
        }
        else {
            // wheeled down
            counter++;
            if (counter >= scrollThreshold) {
                tiles.not('.animate-up').eq(0).addClass('animate-up');
                counter = 0;
            }
        }
      });

});
.hero-list {
  height: 100vh;
  width: 100%;
  touch-action: pan-x;
  user-select: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  position: relative;
  overflow: hidden;
}
.hero-list .hero-list-container {
  position: relative;
  z-index: 901;
  height: 100vh;
  overflow: hidden;
}
.hero-list .hero-list-container .list-tile {
  width: 100%;
  height: 100vh;
  margin: 0 auto;
  position: absolute;
  top: 0;
  left: 0;
  box-sizing: border-box;
  overflow: hidden;
  transform: translate3d(0, 100%, 0);
  transition-duration: 1s;
  transition-property: transform;
  transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
  transition-delay: 0s;
}
.hero-list .hero-list-container .list-tile:nth-of-type(2) {
  background-color: green;
}
.hero-list .hero-list-container .list-tile:nth-of-type(3) {
  background-color: blue;
}
.hero-list .hero-list-container .list-tile .module-background {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  display: block;
  transition-duration: 1s;
  transition-property: transform;
  transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
  transition-delay: 0s;
  transform: translate3d(0, -20%, 0) scale(1.5);
}
.hero-list .hero-list-container .list-tile.animate-up {
  transform: translate3d(0, 0%, 0) scale(1) !important;
}
.hero-list .hero-list-container .list-tile.animate-up .module-background {
  transform: translate3d(0, 0%, 0) scale(1) !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hero-list">
				<div class="hero-list-container">
					<div class="list-tile animate-up">
						<div class="module-background" style="background-image: url('https://thomasstorage1.blob.core.windows.net/wp-media/2017/11/tech-background.png');
				background-size: cover;">

						</div>
					</div>

					<div class="list-tile">
						<div class="module-background" style="background-image: url('https://thomasstorage1.blob.core.windows.net/wp-media/2017/11/tech-background.png');
				background-size: cover;">

						</div>
					</div>

					<div class="list-tile">
						<div class="module-background" style="background-image: url('https://thomasstorage1.blob.core.windows.net/wp-media/2017/11/tech-background.png');
				background-size: cover;">

						</div>
					</div>

					<div class="list-tile">
						<div class="module-background" style="background-image: url('https://thomasstorage1.blob.core.windows.net/wp-media/2017/11/tech-background.png');
				background-size: cover;">

						</div>
					</div>
				</div>
			</div>

您需要通过检测触摸事件来重构您的插件。 Hammer 是一个很棒的库,可以涵盖所有手势类型。如果你需要一些更简单、更简单的东西,你可以用简单的 JavaScript 自己添加这个功能:

添加功能检测和触摸处理程序:

if("ontouchstart" in window){
   el.addEventListener('touchstart', touchStartHandler);
   el.addEventListener('touchmove', touchMoveHandler);
   el.addEventListener('touchend', touchEndHandler);
}

将实际的代码放入您的onwheel处理程序在它自己的函数中。

现在,您应该决定要在插件中实现什么行为:即滚动是否应该在向下滑动期间发生,或者仅在手指释放后发生。在所需的触摸处理程序中使用该函数,或者在touchmove或里面touchend event.

由于您需要使用最新的 jQuery,我相信您不需要额外的向后跨浏览器兼容性,无论如何这里是一个示例:https://stackoverflow.com/a/27024624/4845566 https://stackoverflow.com/a/27024624/4845566.

最后,您可以通过允许触摸操作稍微对角,就像使用拇指一样,为您的插件添加更多可用性。这是关于该主题的精彩讨论以及您需要的所有源代码:vanilla js 上的简单滑动检测 https://gist.github.com/SleepWalker/da5636b1abcbaff48c4d

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

触摸屏上的“滚轮”事件 的相关文章

  • Electron:jQuery 未定义

    问题 在使用 Electron 进行开发时 当您尝试使用任何需要 jQuery 的 JS 插件时 即使您使用脚本标签加载到正确的路径 该插件也找不到 jQuery 例如 p Click me p jQuery should be loade
  • JavaScript 事件循环:队列、消息队列、事件队列

    阅读了大量 JavaScript 事件循环教程 我看到了不同的术语来标识当调用堆栈为空时准备由事件循环获取的队列存储消息 Queue 消息队列 事件队列 我找不到规范术语来识别这一点 甚至 MDN 似乎也对此感到困惑事件循环页面 https
  • javascript 中的正则表达式和分隔符

    我不太擅长正则表达式 我需要一些帮助 但我被困住了 这就是我得到的 编辑 现在工作正常 看看 http jsfiddle net oscarj24 qrPHk 1 http jsfiddle net oscarj24 qrPHk 1 这就是
  • img设置flex-grow来填充flex容器剩余空间,它会导致flex内部溢出flex容器[重复]

    这个问题在这里已经有答案了 以下是我的代码 text1 溢出 Flex 容器 我期望 Flex 容器中的 img 文本和 img 填充 Flex 容器其余部分
  • 添加/更改 URL 参数并重定向到新 URL

    If the view allURL 中不存在参数 我需要将其与值一起添加到 URL 的末尾 如果它确实存在 那么我需要能够仅更改该值而不创建新的 URL 因为它之前可能有也可能没有其他参数 我找到了这个功能 但我无法让它工作 https
  • 如何更改 angularjs $http.jsonp 的标头

    我读了document http docs angularjs org api ng 24http 但我想我一定是误解了 http defaults headers jsonp Accept application json http js
  • 在 JQueryUI 小部件的 QUnit 测试中测试可见性

    这对于其他人来说可能是显而易见的 但我没有通过搜索找到它 所以在这里发布问题和一个可能的答案 背景 使用自定义 JQuery UI 小部件小部件工厂 http jqueryui com widget 在小部件中 某些元素根据其他数据 选项隐
  • Mustache.js 只允许换行,转义其他 HTML

    我正在根据用户输入创建评论 并在用户单击 提交 后使用 Mustache js 渲染它们 我意识到我可以替换用户输入换行符 n with br 呈现为 HTML 中断 例如 myString replace n g br 我意识到我可以使用
  • 如何通过 Web-Workers 传递自定义类实例?

    由于 Web Worker JSON 在线程之间序列化数据 因此这样的方法不起作用 worker js function Animal Animal prototype foobar function self onmessage func
  • 用角度js中的字母过滤列表

    我在表格中显示了一个列表 我需要使用名称的第一个字母来过滤结果 在列表上方我有一个字母 A B C D 等等 单击后 字母列表将按名字过滤 例如 列表详细信息是Apple Boy Bridge点击后A Apple将显示 我必须过滤国家名称以
  • jquery .html() 不适用于 ie8

    我有一个 jquery 函数 它对 Web 服务器上的 Web 服务方法进行 ajax 调用 该方法返回一个包含数据的 html 表 我正在使用 html 渲染 div 上的返回值 这适用于 Firefox Chrome Safari 但不
  • 如何在 Strongloop 环回脚手架项目中覆盖基本用户?

    给定一个使用以下命令创建的全新项目 slc lb project myapp 我该如何更换 user 模型中models json带有 customer 模型放置在 models目录 客户应该有登录 注销等方法 并且 用户 不应该作为 AP
  • 为什么“jQuery-Rails”经常位于资产组之外

    为什么我经常看到gem jquery rails之外的 assets group group assets do gem sass rails gt 3 1 0 gem coffee rails gt 3 1 0 gem uglifier
  • 函数声明可以出现在 JavaScript 的语句内部吗?

    请考虑将官方 ECMAScript 规范作为您答案的来源 而不是特定浏览器供应商发布的文档 我知道 Mozilla 用 函数语句 扩展了它的 JavaScript 实现 因此 根据 ECMAScript 规范 因此 其中定义的语法产生式 这
  • 如何在 AngularJS 中设置选择选项中的文本格式?

    我有以下 json 对象 scope values id 2 code Code 1 name Sample 1 id 4 code Code 2 name Sample 2 id 7 code Code 3 name Sample 3 在
  • Vue js - 在同一级别的两个组件内传递数据

    我有需要从一个传递的数据component1到另一个component2 我不使用vuex or router 组件树 Parent Component1 Component2 从一开始component1我发出 ajax 请求 检索信息并
  • 以编程方式访问使用数据 URI 作为源的 iframe

    我正在使用 数据 URI 以编程方式创建一个 iframe 该框架加载良好 但似乎以编程方式使用 iframe 会遇到跨域安全检查 var iframeDoc document getElementById myFrame contentW
  • jQuery 仅附加一次

    所以我有这个 jQuery document ready function var nav nav var logo img src img logo png window scroll function if this scrollTop
  • 返回语句后的声明

    function f return f1 function f1 return 5 f returns 5 为什么这有效 之后声明局部函数有什么好处return 这是好的做法吗 它之所以有效 是因为函数声明都是由解释器在第一次传递时评估的
  • 理解“窗口”对象[重复]

    这个问题在这里已经有答案了 可能的重复 JS 窗口全局对象 https stackoverflow com questions 10035771 js window global object 如何window对象工作 我知道它是顶级对象并

随机推荐