简体   繁体   English

触发“ touchstart”事件时如何防止“滚动”

[英]How to prevent “scroll” when a “touchstart” event is fired

I have an scrollable element to which I have added two event handlers ("touchstart" and "scroll"). 我有一个可滚动元素,在其中添加了两个事件处理程序(“ touchstart”和“ scroll”)。 Both work perfectly fine if fired by themselves. 如果自己开除,两者都可以正常工作。 However, when the "touchstart" event is fired, I need to prevent the "scroll" from being fired, but I still need the "scroll" event to be available. 但是,当触发“ touchstart”事件时,我需要防止触发“ scroll”,但是仍然需要“ scroll”事件可用。 The reason is that both events call the same function and so it is causing the app to look buggy when the logic runs many times. 原因是两个事件都调用相同的函数,因此,当逻辑多次运行时,这会使应用程序看起来有问题。

I have created a basic fiddle and here is the link: 我创建了一个基本的小提琴,这里是链接:

http://jsfiddle.net/alejandro1585/0t7v76m7/3/ http://jsfiddle.net/alejandro1585/0t7v76m7/3/

HTML: HTML:

 <div class="main-container">
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
    Hello world
</div>

CSS: CSS:

.main-container{
    border:  1px solid red;
    width: 120px;
    height:200px;
    overflow-y: auto;
}

JAVASCRIPT: JAVASCRIPT:

    function myFunction(e) {
    console.log("myFunction is being executed", e);
};

/*Method adds handlers to the scrollable list so that functions to show or hide the header can be triggered*/
var scrollShowHideHeaderInit = function () {
    var divObject = document.getElementsByClassName("main-container"); //Scrollable element object

    for (var i = 0; i < divObject.length; i++) {
        divObject[i].addEventListener("scroll", myFunction);
        divObject[i].addEventListener("touchstart", myFunction);
    };
};

scrollShowHideHeaderInit();

You should be able to just stop event propagation on touch start and still maintain scroll-ability 您应该能够仅在触摸开始时停止事件传播,并且仍然保持滚动性

function touch(e){
  console.log("touch", e);
  e.stopPropagation();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何<a>在纯JS中</a>触发<a>标签上的</a> touchstart事件时阻止滚动而不是链接<a>?</a> - How to prevent scroll but not link following when firing touchstart event over <a> tag in pure JS? Android - JavaScript:在缩放或滚动页面之前不会触发touchstart事件 - Android - JavaScript : touchstart event not fired until zoom or scroll the page 滑动时防止触摸启动 - prevent touchstart when swiping 在移动设备上滚动/滑动时防止touchstart事件 - Prevent touchstart event when scrolling/swiping on mobile devices 使用srcdoc =时不会触发iframe滚动事件 - iframe scroll event not fired when using srcdoc= 以编程方式滚动时是否可以确保触发“ scroll”事件? - Is the 'scroll' event guaranteed to be fired when scrolling programatically? 在JQuery / Javascript中触发“调整大小”事件时,如何更新在“滚动”事件中使用的某些全局变量? - How to update some global variables used in 'scroll' event when 'resize' event is fired in JQuery / Javascript? 滚动后touchstart事件停止工作 - touchstart event stops working after scroll 当我向下滚动页面时,事件触发了几次 - Event fired several times when i scroll down the page 用于“doubletap”的“touchstart”事件阻止滚动向上或向下滑动 - "touchstart" event used for a "doubletap" prevents scroll working on swipe up or down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM