How to make event listeners passive to improve scrolling performance, PageSpeed Insights

Hòa Nguyễn Coder

Published in Web Developer · Đã đăng vào tháng 8 8, 2023 8:33 PM

Nay mình chia sẻ với mọi người cách fix lỗi tối ưu website qua Pagespeed Insights. 


<script>
	jQuery.event.special.touchstart = {
		setup: function( _, ns, handle ) {
			this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
		}
	};
	jQuery.event.special.touchmove = {
		setup: function( _, ns, handle ) {
			this.addEventListener("touchmove", handle, { passive: !ns.includes("noPreventDefault") });
		}
	};
	jQuery.event.special.wheel = {
		setup: function( _, ns, handle ){
			this.addEventListener("wheel", handle, { passive: true });
		}
	};
	jQuery.event.special.mousewheel = {
		setup: function( _, ns, handle ){
			this.addEventListener("mousewheel", handle, { passive: true });
		}
	};
</script>

Sử dụng đoạn code trên để fix lỗi đó, giúp cho điểm số hiệu suất được tăng

Nếu bạn thấy đem lại kiến thức bổ ích, hãy Click xem quảng cáo trên trang website của mình nhé! 🚀