js实现浏览器宽度改变,样式随之变化

响应式做法,浏览器宽度

//document.documentElement.clientWidth  浏览器的宽度
 window.onresize = function () {
    
    
            //document.documentElement.clientWidth  浏览器的宽度
            if (document.documentElement.clientWidth > 200 && document.documentElement.clientWidth < 300) {
    
    
                document.body.style.backgroundColor = "red"
            } else if (document.documentElement.clientWidth > 300 && document.documentElement.clientWidth < 400) {
    
    
                document.body.style.backgroundColor = "green"
            } else {
    
    
                document.body.style.backgroundColor = "yellow"
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_45785424/article/details/106630428