对角放置 HTML 元素

发布于 2024-10-19 11:52:19 字数 1710 浏览 0 评论 0原文

我是 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>

I am new to Javascript & relatively new to HTML. I would like to know what language(pure html or Javascript & html) I should use & some suggestions of an algorithm in order to do the following:

Place 4 squares on a background but position them diagonally. Each square is a link you can click to go to a different page. So how would I be able to position html elements diagonally like my picture below?

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

I believe my code would look like this:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-10-26 11:52:19

您可以使用绝对定位来达到此目的。

在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*/
}

You can use absolute positioning for this purpose.

In 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*/
}
夜无邪 2024-10-26 11:52:19

现场演示
现场演示 属性顺序已更改以匹配您的模型)

我试图保留您要求的数字。

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-相对定位/

Live Demo
Live Demo (with left properties order changed to match your mockup)

I tried to keep to the numbers you asked for.

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; }

How does this work? See: http://css-tricks.com/absolute-positioning-inside-relative-positioning/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文