对角放置 HTML 元素
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用绝对定位来达到此目的。
在CSS中:
You can use absolute positioning for this purpose.
In CSS:
现场演示
现场演示(
左
属性顺序已更改以匹配您的模型)我试图保留您要求的数字。
HTML:
CSS:
这是如何工作的?请参阅: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:
CSS:
How does this work? See: http://css-tricks.com/absolute-positioning-inside-relative-positioning/