简体   繁体   English

如何创建自定义事件以进行上下滚动?

[英]How can I create a custom event for scroll up and down?

The MDN told me how to create a custom event (which I didn't get it well) MDN告诉我如何创建自定义事件(我做得不好)

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

and I know how to detect mouse scroll as well... 而且我也知道如何检测鼠标滚动...

var doScroll = function (e) {
    // cross-browser wheel delta
    e = window.event || e;
    var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));

    // Do something with `delta`
    document.body.innerHTML = delta;

    e.preventDefault();
};

if (window.addEventListener) {
    window.addEventListener("mousewheel", doScroll, false);
    window.addEventListener("DOMMouseScroll", doScroll, false);
} else {
    window.attachEvent("onmousewheel", doScroll);
}

but I don't know how to mix these two with each other and create a custom event so I can have something like this: 但我不知道如何将两者相互混合并创建自定义事件,因此我可能会遇到以下情况:

window.addEventListener('scrollUp', sUpFunction);
window.addEventListener('scrollDown', sDownFunction);

Thank you guys. 感谢大伙们。

First I would use the "wheel" event instead of the non-standardrized "mousewheel" event . 首先,我将使用“ wheel”事件而不是非标准化的“ mousewheel”事件
I created a simple implementation of a scrollUp and scrollDown custom events dispatch. 我创建了scrollUp和scrollDown自定义事件分配的简单实现。

EDIT 编辑

I have a added the IE polyfill for the CustomEvent in order to support IE as well 为了支持IE,我为CustomEvent添加了IE polyfill

 // For IE support (function () { if ( typeof window.CustomEvent === "function" ) return false; //If not IE function CustomEvent ( event, params ) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt = document.createEvent( 'CustomEvent' ); evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); return evt; } CustomEvent.prototype = window.Event.prototype; window.CustomEvent = CustomEvent; })(); let element = document.getElementsByClassName("scroll-area")[0], scrollUpEvent = new CustomEvent("scrollUp"), scrollDownEvent = new CustomEvent("scrollDown"); function scrollDown(){ console.log("scrolled down"); } function scrollUP(){ console.log("scrolled up"); } function scrollHappened(e){ if(e.deltaY < 0){ element.dispatchEvent(scrollUpEvent); } else { element.dispatchEvent(scrollDownEvent); } } element.addEventListener("wheel", scrollHappened); element.addEventListener("scrollUp", scrollUP); element.addEventListener("scrollDown", scrollDown); 
 .scroll-area { border: solid 2px black; height: 30px; } 
 <div class="scroll-area">Scroll on me</div> 

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

相关问题 我如何在向下滚动时创建隐藏并在向上滚动菜单上显示隐藏的jQuery或其他依赖项 - How can I create a hide on scroll down and show on scroll up menu whitout jquery or other dependencies 如何在鼠标事件上上下滚动列表 - How to scroll list up and down on mouse event 我如何知道用户的滚动是向上还是向下? - How can I know whether the scroll of the user is UP or DOWN? 如何在滚动事件上运行一次功能? - How to run once function on scroll up and scroll down event? 向下滚动时如何缩小动画标题,而向上滚动时如何放大动画标题? - How can I shrink a header animated when I scroll down and enlarge it when I scroll up? 如何创建一个 header 和 JQuery,它在向上滚动时固定在顶部但在向下滚动时隐藏? - How do I create a header with JQuery that is fixed on top on scroll up but hides on scroll down? 如何检测元素中任何位置的按键向上/向下事件? - How can I detect a key up / down event anywhere in an element? 如何在javascript中触发onwheel事件上下效果? - How can I trigger onwheel event up and down effect in javascript? 如何使标题在向下滚动时消失/消失并在向上滚动时重新出现? - How can I make my header fade out/disappear on scroll down and reappear on scroll up? 向上或向下滚动JavaScript事件 - JavaScript event on scroll up or scroll down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM