If a table has both "table-layout: fixed" and "width: 0", then cells with borders have wrong width. This occurs in both safari 3.x mac&win and linux svn 31841 (debian sid). The expected behavior is rendered by IE6, IE7, Firefox 2, Firefox 3 beta and Opera 9.2. Consider a table with 3 columns, all with width 200px. If one of the cells have "border-left: 50px; width: 150px;", then according to the rules of "table-layout: fixed", the cell should still have width 200px. But webkit renders the cell with only width 150px.
<body> <div id="" class="" style="position:fixed; right:20px; top:20px;border:solid 1px blue;"> fixed?? </div> <div style="height:1000px;">mock page content<div> </body>
主流的做法有兩種,一種使用expression表達式,在頁面滾動時重新設計元素的top屬性:
<body> <div id="" class="" style="position:fixed; right:20px; top:20px;border:solid 1px blue; _position:absolute;_top:expression(parseInt(document.documentElement.scrollTop,10)+20+'px');"> fixed?? </div> <div style="height:1000px;">mock page content<div> </body>
這種方法比較直接,但是每次滾動都重新計算元素的位置很消耗性能,而且頁面比較復雜的時候,會出現“抖動”的現象。
另外一種方法當你弄明白是才發現奇妙無窮,是否可以改變頁面的滾動條滾動的元素呢?滾動條默認滾動的是body元素。如果滾動的是一個div,而需要固定定位的元素并不在這個div中的話,自然就不會隨著滾動條的滾動而滾動。
<body> <style type="text/css"> html,body,#content{height:100%;overflow:auto;padding:0px;margin:0px;} #fixed{position:fixed; right:20px; bottom:20px; border:solid 1px blue;_position:absolute;} </style> <div id="content" class=""> <div id="" class="" style="background-color:#ccc;height:1000px;"> mock page content </div> </div> <div id="fixed" class=""> fixed content </div> </body>
設置一個div與body等高等寬,將頁面內容都放到這個div中。這樣頁面滾動條其實是這個div的滾動條。然后在body下放上position:fixed的元素。這樣就大功告成了。
這基本是一個白話版的解決辦法了。基本是原理和簡單實現,更多的細節可以參考:
http://www.qianduan.net/fix-ie6-dont-support-position-fixed-bug.html
這個代碼我沒有運行,因為一邊寫就發現不對勁,在propertychange函數中改變property會再次觸發propertychange事件,結果就可想而知了。stack overflow。
所以需要換一種思路,新建一個textarea,將同樣大的文本放到屬性一樣的textarea中計算其高度,然后把高度設置到目標textarea中。
搞清楚原理其實就簡單多了。要捕捉textarea的change事件在IE下可以使用propertychange,在!IE下可以使用input。
一些textarea資料做參考: