简体   繁体   English

如何更改与浏览器窗口大小有关的网站背景

[英]How do you change a website background in relation the size of a browser window

I'm working on a website where if the browser window is smaller than a certain size, part of the background clashes with the text. 我在一个网站上工作,如果浏览器窗口小于特定大小,则部分背景与文本冲突。 Is it possible to change the background after a browser window becomes smaller than a certain amount? 浏览器窗口变得小于一定数量后,是否可以更改背景?

You can hook into the window.onresize event to check the width and height of the window, then based on these values you can set the background color to what you desire. 您可以挂接到window.onresize事件中以检查窗口的宽度和高度,然后根据这些值可以将背景色设置为所需的颜色。

window.onresize = function(event) {
    var height = window.innerHeight;
    var width = window.innerWidth;
}

This solution may have compatability issues in other browsers to IE and so the jQuery function is probably more prefered: 此解决方案在与IE的其他浏览器中可能存在兼容性问题,因此jQuery函数可能更受欢迎:

$( window ).resize(function() {
   var height = $(this).height();
   var width = $(this).width();
});

EDIT: (Example provided) 编辑:(提供示例)

    <script type="text/javascript">
        window.onresize = function (event) {
            var height = window.innereight;
            var width = window.innerWidth;

            if (width < 500 || height < 500) {
                document.getElementById('Div1').style.backgroundColor = '#FF0000';
            }
        }

//OR WITH JQUERY

        $(window).resize(function () {
            var height = $(this).height();
            var width = $(this).width();

            if (width < 500 || height < 500) {
                $('#Div1').css('background-color', '#FF0000');
            }
        });
    </script>

    <div id="Div1" style="width: 300px; height: 300px;">
        test div
    </div>

Please not that when using the innerHeight property in Chrome that the value is returned as undefined and so this does not seem to be supported. 请注意,在Chrome中使用innerHeight属性时,该值将返回为undefined,因此似乎不支持此值。 The jQuery method would be the preferred solution. jQuery方法将是首选解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM