dongmubi4444 2015-05-02 05:57
浏览 45
已采纳

如何强制HTML元素在没有JavaScript的情况下保持恒定的宽高比?

Here is an example I made using JavaScript. No matter what dimensions the page is, each brick will always be a 5:2 ratio and there will always be 20 bricks across the page.

<!doctype html>
<html>
  <body>
    <svg id="wall" style="position:absolute;top:0;left:0;" width="100%" height="100%" onload="renderBackground();" onresize="renderBackground();">
    </svg>
    <script>
      var wall=document.getElementById("wall");
      function renderBackground()
      {
        var width=window.innerWidth;
        wall.innerHTML="";
        for(y=0;y<window.innerHeight;y+=width/25)
        {
          // Even Bricks
          for(x=0;x<width;x+=width/20)
          {
            createBrick(x,y,width/20,width/50);
          }
          // Odd Bricks
          for(x=0-width/40;x<width;x+=width/20)
          {
            createBrick(x,y+width/50,width/20,width/50);
          }
        }
      }
      function createBrick(x,y,width,height)
      {
        var brick=document.createElementNS(wall.namespaceURI,"rect");
        var shine=document.createElementNS(wall.namespaceURI,"rect");
        var drain=Math.random()*24;
        wall.appendChild(brick);
        wall.appendChild(shine);
        brick.setAttribute("x",x);
        brick.setAttribute("y",y);
        brick.setAttribute("width",width);
        brick.setAttribute("height",height);
        shine.setAttribute("x",x+width/20);
        shine.setAttribute("y",y+height/10);
        shine.setAttribute("width",9*width/10);
        shine.setAttribute("height",height/10);
        brick.setAttribute("style","fill:"+rgb(6*Math.random()+128-drain,12*Math.random()+128-drain,6*Math.random()+128-drain)+";stroke:"+rgb(51,51,68)+";stroke-width:"+String(height/20));
        shine.setAttribute("style","fill:"+rgb(160,160,160));
      }
      function rgb(r,g,b)
      {
        return "rgb("+String(Math.floor(Math.max(Math.min(r,256),0)))+","+String(Math.floor(Math.max(Math.min(g,256),0)))+","+String(Math.floor(Math.max(Math.min(b,256),0)))+")";
      }
    </script>
  </body>
</html>

If I were to generate this exact same page with php, removing the JavaScript entirely, how could I force the bricks to keep the same 5:2 ratio without needing to call renderBackground() to redraw and adjust their size every time the window is resized?

I would like to know how to do this on all elements, not just SVG.

Thanks!

  • 写回答

1条回答 默认 最新

  • duangangmo0583 2015-05-02 06:37
    关注

    If you are using IE9 or later, with the exception of a few mobile browsers, CSS accepts dimensions of units "viewport width" (vw) and/or "viewport height" (vh). An element with a width of 5vw and a height of 2vw will be 5% of the viewport's width in width, as well as 2% of the viewport's width in height.

    Since you're setting the width and height of an element to a percentage of just the width of the viewport, it will maintain a constant aspect ratio.

    The catch: It will not work within an SVG element. It's strictly a CSS unit. It also only considers the whole viewport's width or height, not any containing element. Your example takes up the whole screen though, so that's fine.

    You will have some problems knowing how many rows to draw though, at least without using JavaScript. You're locking yourself to 20 columns, but the rows depend entirely on the client's aspect ratio.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置