To trigger a function on window resize, you can utilize the window.onresize
event.
window.onresize = function () {
if (window.innerWidth <= 768) {
var snapper = new Snap({
element: document.getElementById('content'),
disable: 'right'
});
}
}
Keep in mind that this event only occurs once the resizing is completed. For more consistent results, consider using jQuery's $(window).width()
instead of vanilla JavaScript, as it may provide better accuracy. Additionally, remember to include an else
block to revert any changes made in the initial condition. To enhance performance and prevent redundant code execution, set up flags that are checked by both the initial condition and subsequent conditions to determine whether creating a new Snap
instance is necessary.