动态HTML教程 - 第五天
Taylor 1998.3.9
 

第二页:放在底部和右面

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

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

>>

动态HTML教程
第一页 排错
第二页 放在底部和右面
第三页 Netscape的调整大小问题
第四页 一定要把样式加在标记中吗?
第五页 关于Event对象
第六页 课程结束

[第1天][第2天][第3天][第4天][第5天]


返回