Not sure why you want to do this but it's unfortunately not possible because an event dispatched by JavaScript code is not trusted.
See 3.4 Trusted events in the W3C UI Events Specification
Events that are generated by the user agent, either as a result of
user interaction, or as a direct result of changes to the DOM, are
trusted by the user agent with privileges that are not afforded to
events generated by script through the
DocumentEvent.createEvent("Event") method, modified using the
Event.initEvent() method, or dispatched via the
EventTarget.dispatchEvent() method. The isTrusted attribute of trusted
events has a value of true, while untrusted events have a isTrusted
attribute value of false.
Most untrusted events should not trigger default actions, with the
exception of the click event. This event always triggers the default
action, even if the isTrusted attribute is false (this behavior is
retained for backward-compatibility). All other untrusted events must
behave as if the Event.preventDefault() method had been called on that
event.
This code demonstrates what happens when you cancel a trusted wheel
event, and try to dispatch one with dispatchEvent
:
const [container, overlay] = ['container', 'overlay'].map(x => document.getElementsByClassName(x)[0]);
overlay.addEventListener('wheel', e => {
'use strict'; // Effectively throws an error when trying to assign isTrusted
console.log(`Is the user event trusted? ${e.isTrusted}`); // true
e.preventDefault(); // Stops the default behavior
const newEvent = new e.constructor(e.type, e); // Clone the event
console.log(`Is the generated event trusted? ${newEvent.isTrusted}`); // false
try {
newEvent.isTrusted = true;
}
catch (e) {
console.error(e); // TypeError
}
container.dispatchEvent(newEvent);
});
container.addEventListener('wheel', e => {
console.log(`Wheel event received by the container, is it trusted? ${e.isTrusted} Is it cancelled? ${e.defaultPrevented}`);
// You have to implement the scroll behavior yourself because it won't happen otherwise
});
.container {
overflow-y: scroll;
height: 200px;
}
.content {
height: 600px;
}
.overlay {
position: static;
width: 100vw;
height: 100vh;
}
<div class="container">
<div class="content">
<div>
</div>
<div class="overlay">
</div>