iphone/ipad triggering unexpected resize events

前端 未结 8 2070
北荒
北荒 2020-12-13 01:54

I\'m working on a mobile version of my site. I\'m using media queries and CSS as much as possible, but I\'m also using some javascript to, for example, turn my navigation in

8条回答
  •  轮回少年
    2020-12-13 02:16

    Here's the vanilla javascript version of the accepted answer

    document.addEventListener('DOMContentLoaded', function() {
    
        // Store the window width
        var windowWidth = window.innerWidth
    
        // Resize Event
        window.addEventListener("resize", function() {
    
            // Check window width has actually changed and it's not just iOS triggering a resize event on scroll
            if (window.innerWidth != windowWidth) {
    
                // Update the window width for next time
                windowWidth = window.innerWidth
    
                // Do stuff here
    
            }
    
            // Otherwise do nothing
        })
    
    })
    

提交回复
热议问题