Last active
November 8, 2023 21:02
-
-
Save seojacky/9ab1c367d914b695f450e18bc01d062f to your computer and use it in GitHub Desktop.
Пассивные прослушиватели событий не используются для улучшения производительности при прокрутке
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Пассивные прослушиватели событий не используются для улучшения производительности при прокрутке | |
//© Калинский Евгений aka seojacky (https://t.me/big_jacky) | |
add_action( 'wp_footer', 'script_passive_event_listeners', 99 ); | |
function script_passive_event_listeners(){ | |
?> | |
<script> | |
(function () { | |
var supportsPassive = eventListenerOptionsSupported(); | |
if (supportsPassive) { | |
var addEvent = EventTarget.prototype.addEventListener; | |
overwriteAddEvent(addEvent); | |
} | |
function overwriteAddEvent(superMethod) { | |
var defaultOptions = { | |
passive: true, | |
capture: false | |
}; | |
EventTarget.prototype.addEventListener = function (type, listener, options) { | |
var usesListenerOptions = typeof options === 'object'; | |
var useCapture = usesListenerOptions ? options.capture : options; | |
options = usesListenerOptions ? options : {}; | |
if (type == 'touchstart' || type == 'scroll' || type == 'wheel') { | |
options.passive = options.passive !== undefined ? options.passive : defaultOptions.passive; | |
} | |
options.capture = useCapture !== undefined ? useCapture : defaultOptions.capture; | |
superMethod.call(this, type, listener, options); | |
}; | |
} | |
function eventListenerOptionsSupported() { | |
var supported = false; | |
try { | |
var opts = Object.defineProperty({}, 'passive', { | |
get: function () { | |
supported = true; | |
} | |
}); | |
window.addEventListener("test", null, opts); | |
} catch (e) { } | |
return supported; | |
} | |
})(); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment