【问题标题】:Window innerWidth,innerHeight issue窗口innerWidth,innerHeight问题
【发布时间】:2019-02-26 15:22:17
【问题描述】:

尝试在整个屏幕中使用水平和垂直线条(作为背景)。两条线之间的距离为 3 像素。

代码是:

  <!doctype html>
    <html>
        <head>
            <title>Ζωγραφίζοντας ένα γραφείο</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <link rel="StyleSheet" href="style.css" type="text/css" media="screen">
            <script type="text/javascript" src="script.js"></script>
        </head>
        <body>
            <!-- svg path for background -->
            <svg width="0" height="0">
                <path d="" stroke="#ccc" stroke-width="1" fill="none"/>
            </svg>
            <section>
            </section>
        </body>
    </html>

    *{
        margin:0;
        padding:0;
    }

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

    section{
        position:absolute;
        z-index:2;
        top:0px;
        left:0px;
    }

    var svg_1;
    var background_path;
    var window_height,window_width;
    var i;
    var d_for_path="";
    document.addEventListener('DOMContentLoaded', function(){ 
        svg_1 = document.getElementsByTagName("svg")[0];
        background_path = document.getElementsByTagName("path")[0];

        window_height = window.innerHeight;
        window_width = window.innerWidth;

        var w = window,
        d = document,
        e = d.documentElement,
        g = d.getElementsByTagName('body')[0],
        x = parseFloat(w.innerWidth) || parseFloat(e.clientWidth) || parseFloat(g.clientWidth),
        y = parseFloat(w.innerHeight) || parseFloat(e.clientHeight)|| parseFloat(g.clientHeight);
        x = x-1;
        y = y-3;

        //make svg_1 as big as possible
        svg_1.setAttribute("width",x+"px");
        svg_1.setAttribute("height",y+"px");

        //draw horizontal lines
        for(i=0;i<=y;i=i+3){
            d_for_path+= "M 0 "+i+",H "+x+",";
        }
        //draw vertical line
        for(i=0;i<=x;i=i+3){
            d_for_path+= "M "+i+" 0,V "+y+",";
        }
        background_path.setAttribute("d",d_for_path);

    }, false);

上面的代码有什么问题? 为什么一定要减少x、y变量?

提前致谢, 克里斯·帕帕斯

*我在 Firefox 浏览器中看不到这些行。

【问题讨论】:

    标签: javascript height width


    【解决方案1】:

    仅使用 css 找到了解决方案

    html, body { margin:0; padding:0; overflow:hidden }
    svg { position:fixed; top:0; left:0; height:100%; width:100% }
    

    喷泉:回复How to scale SVG image to fill browser window?

    【讨论】:

      【解决方案2】:

      你也可以使用 CSS 来做类似的事情。

      HTML, body {
        height: 100px;
        width: 100%;
      }
      
      #small {
        display: inline-block;
        height: 100px;
        width: 100px;
        background-color: rgb(50, 50, 50);
        background-image: 
            linear-gradient(rgb(202, 202, 202) 50%, transparent 50%, transparent), 
            linear-gradient(90deg, rgb(202, 202, 202) 50%, transparent 50%, transparent);
        background-size: 3px 3px;
      }
      
      #big {
        display: inline-block;
        height: 100px;
        width: 100px;
        background-color: rgb(50, 50, 50);
        background-image: 
            linear-gradient(rgb(202, 202, 202) 50%, transparent 50%, transparent), 
            linear-gradient(90deg, rgb(202, 202, 202) 50%, transparent 50%, transparent);
        background-size: 30px 30px;
      }
      <!doctype html>
      <html>
        <head>
          <title>Site</title>
        </head>
        <body>
          <label>Scale: small</label>
          <div id="small"></div>
          <label>Scale: big</label>
          <div id="big"></div>
        </body>
      </html>

      是的,它在 Google Chrome 上看起来好多了(至少对我来说) 让我知道它是否仍然对您有用。 祝你有美好的一天,埃利亚斯 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-21
        • 2013-07-24
        • 2019-04-09
        • 2014-03-04
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 2020-05-10
        相关资源
        最近更新 更多