第五天

第二页 放在底部和右面


  CSS在定位上的缺点是它只有left和top属性,但是没有bottom或right。这就意味着你只能放一个对象在离浏览器窗口50个像素的位置,但是你不能只用CSS就能把对象放在离浏览器窗口右边50像素的地方。这个问题在CSS2中应该能得到解决,样式表的下一代specification正在出笼;在它被主流浏览器支持之前,你只好求助于JavaScript。

  用JavaScript解决这个问题的最好方式是基于文档宽度的计算。两种4.0浏览器都在DOM中包含文档大小的对象。所以定位对象的方式是把窗口的宽度减去对象的宽度。

<div id="foo">

your content here

</div>

<script>

if (document.layers) {

  document.foo.left = window.innerWidth - document.foo.clip.width;

} else if (document.all) {

  foo.style.left = document.body.offsetWidth - parseInt(foo.style.width);

}

</script>

  这可以把div定位到屏幕的右边。这种技术也可以把对象定位到窗口底部。

  两种浏览器都支持的屏幕对象是:

Feature Internet Explorer Netscape Navigator
height of the screen screen.height screen.height
width of the screen screen.width screen.width
color depth of the screen screen.colorDepth screen.colorDepth
height of the window document.body.offsetHeight* window.innerHeight
width of the window document.body.offsetWidth* window.innerWidth

*从技术上讲,这是文档的高度和宽度,不是窗口的,但是大多数情况下可以把它们看成一件事。


□ 资料来源:sohu.com

本书由“E书时空”免费制作;
想要更多的免费电子图书,请光临
http://www.eshunet.com/