0

从这个脚本中,我得到了浏览器窗口的宽度和高度。我想将 html 中的 backgroundimage 设置为 thissize 的 100%。(无论屏幕尺寸如何)。其他元素(导航栏、文本、图片或 svg)必须根据大小进行调整。

--> 我想要任何时候都没有滚动条:)

window.onload = function () 
{
    // initale Breite und Höhe des Browserfensters
    var iWidth = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
    var iHeight = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);

    var bWin = document.getElementById('bWin');
    bWin.innerHTML = "Fensterbreite " + iWidth + " <br />Fensterhöhe " + iHeight;
    window.onresize = function (evt) 
    {
         var width = window.innerWidth || (window.document.documentElement.clientWitdh || window.document.body.clientWidth);
         var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
         var bWin = document.getElementById('bWin');
         bWin.innerHTML = "Aktuelle Breite " + width + "  <br />Aktuelle Höhe  " + height;
     }
 }
4

1 回答 1

1

Not 100% sure what you are asking, but I think you are trying to stretch your background-image to match the width/height of your browser..... the path you are going looks extra complex, why dont you try some simple CSS

body,html { width: 100%; height 100%; }

body {
        background: url(images/bg.jpg) no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
        /*ie8 Fix*/
         filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
zoom:1;

}

this works crossbrowser

to resize your page elements (divs, navs etc.) you need to look at css media queries and responsive design (using %'s for your widths + heights) you should probably google some material to read up on this if you are new to responsive websites

A quick example of what a responsive design css might look like with media queries

#container { 
 width: 900px;
 margin: 0 auto;
}
@media (max-width: 900px) {
 #container { 
  width: 100%;
 }
}
于 2013-11-07T14:58:27.513 回答