【问题标题】:Position HTML elements diagonally对角放置 HTML 元素
【发布时间】:2011-02-28 08:47:04
【问题描述】:

我是 Javascript 新手,对 HTML 比较陌生。我想知道我应该使用哪种语言(纯 html 或 Javascript 和 html)以及一些算法建议,以便执行以下操作:

在背景上放置 4 个正方形,但要对角放置。每个方块都是一个链接,您可以单击以转到不同的页面。 那么我如何才能像下面的图片一样对角放置 html 元素?

http://i54.tinypic.com/2v7uw5u.png

我相信我的代码应该是这样的:

<script>
    function positionIt()
    {
        var screenW = // javascript function to get screen width??;
        var screenH = // javascript function to get screen height??;

        var sq1 = document.getElementById( "square1" );
        var sq2 = document.getElementById( "square2" );
        var sq3 = document.getElementById( "square3" );
        var sq4 = document.getElementById( "square4" );

        // What javascript functions set a elements x,y positions?
        // Should I use another way of positioning the square (not by absolute terms)
        sq1.setXPos( screenW / 4 );
        sq1.setYPos( screenH / 4 );

        sq2.setXPos( screenW / 3 );
        sq2.setYPos( screenH / 3 );
    }
</script>

// OR if I use css
.menu { background-color: blue; }
/* Position the square in absolute terms diagonally */
#square1 { x=200; y=200; }
#square2 { x=300; y=300; }
#square3 { x=400; y=400; }
#square4 { x=500; y=500; }

<div class="menu" onload="positionIt()">
    <a id="square1" href="home.html"><img src="square.jpg" /></a>
    <a id="square2" href="home.html"><img src="square.jpg" /></a>
    <a id="square3" href="home.html"><img src="square.jpg" /></a>
    <a id="square4" href="home.html"><img src="square.jpg" /></a>
</div>

【问题讨论】:

    标签: javascript html positioning


    【解决方案1】:

    您可以为此使用绝对定位。

    在 CSS 中:

    #menu {
        position: relative;
        width: /*enough width for all of the positioned elements*/
        height: /*enough height for all of the position elements*/
    }
    #squareN {
        top: /*how far from top of #menu*/
        left: /*how far from left of #menu*/
        position: absolute;
        width: /*as required*/
        height: /*as required*/
    }
    

    【讨论】:

      【解决方案2】:

      Live Demo
      Live Demoleft 属性顺序已更改以匹配您的模型)

      我尽量保持你要求的数字。

      HTML:

      <div class="menu">
          <a id="square1" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
          <a id="square2" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
          <a id="square3" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
          <a id="square4" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
      </div>
      

      CSS:

      .menu {
          background: blue;
          width: 800px;
          height: 800px;
          position: relative
      }
      /* Position the square in absolute terms diagonally */
      #square1 { position:absolute; left:200px; top:200px; }
      #square2 { position:absolute; left:300px; top:300px; }
      #square3 { position:absolute; left:400px; top:400px; }
      #square4 { position:absolute; left:500px; top:500px; }
      

      这是如何工作的?见:http://css-tricks.com/absolute-positioning-inside-relative-positioning/

      【讨论】:

      • 谢谢,太好了。有没有办法在 css 中获取屏幕/浏览器尺寸,所以我可以使用这样的表达式:left: expression(screen.width
      • @Mack: 不使用 CSS(是的,使用 JS),但如果我将我的演示更改为基于 % 的布局,而不是你想补偿不同尺寸的屏幕,那会更好。跨度>
      • @Mack:实际上,我不确定你想要什么。你能解释一下屏幕大小和页面上应该改变什么之间的关系吗? (盒子的大小,盒子的位置等)
      • @thritydot,蓝色方块(链接所在的位置)将根据屏幕宽度的不同而不同。但我开始认为使用 CSS 和 % 将其隔开应该可以补偿屏幕宽度,对吗?
      • @Mack:是的,应该。这应该填满整个页面,还是应该只是页面的一部分? (菜单?)我问是因为如果我为您做错了更改它可能会很烦人。
      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      相关资源
      最近更新 更多