职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 513|回复: 10

四种浏览器内核测试100%≠20%+80%

[复制链接]
feiguo 发表于 2011-7-30 09:24 | 显示全部楼层 |阅读模式

经过对各种浏览器的测试发现,以Webkit和Presto为内核的浏览器会出现如下情况:

当父容器width%5!=0,子元素左右布局width分别为A_width+B_width=100%,问题见图



当打开一个宽度%5==0的窗口时,这个像素差值就没有了。(当然你也可以尝试拖动改变窗口大小,发现右边没有差值的时候,点击“Our width?”可以查看这时的所有元素的宽度)



我觉得是内核对百分比算法上可能存在问题,已经将BUG提交给Webkit了,让他们测试一下是不是有这个问题吧。
Html代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6. <style type="text/css">  
  7. html,body { width:100%;}   
  8. </style>  
  9. </head>  
  10.   
  11. <body id="body" style="margin:0;padding:0;">  
  12. <div id="green" style="width:100%; background:green;">I'm green!</div>  
  13. <div id="red" style="width:20%; float:left; background:red;">I'm red!</div>  
  14. <div id="blue" style="width:80%; float:left; background:blue;">I'm blue!</div>  
  15. <input type="button" value="Our width?" onclick="showOurWidth()"/>  
  16. <input type="button" value="Open width%5==0" onclick="openWindow()"/>  
  17. <script type="text/javascript">  
  18. function getWidth(id) {   
  19.     return document.getElementById(id).offsetWidth;   
  20. }   
  21. function showOurWidth() {   
  22.     alert('Body width:'+getWidth('body'));   
  23.     alert('Green width:'+getWidth('green'));   
  24.     alert('Red width:'+getWidth('red'));   
  25.     alert('Blue width:'+getWidth('blue'));   
  26.     alert('Green width == Red+Blue = '+(getWidth('green')==(getWidth('red')+getWidth('blue'))));   
  27. }   
  28. function openWindow() {   
  29.     window.open(window.location, '', 'width=500, height300');   
  30. }   
  31. </script>  
  32. </body>  
  33. </html>  
复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <style type="text/css">
  7. html,body { width:100%;}
  8. </style>
  9. </head>

  10. <body id="body" style="margin:0;padding:0;">
  11. <div id="green" style="width:100%; background:green;">I'm green!</div>
  12. <div id="red" style="width:20%; float:left; background:red;">I'm red!</div>
  13. <div id="blue" style="width:80%; float:left; background:blue;">I'm blue!</div>
  14. <input type="button" value="Our width?" onclick="showOurWidth()"/>
  15. <input type="button" value="Open width%5==0" onclick="openWindow()"/>
  16. <script type="text/javascript">
  17. function getWidth(id) {
  18.         return document.getElementById(id).offsetWidth;
  19. }
  20. function showOurWidth() {
  21.         alert('Body width:'+getWidth('body'));
  22.         alert('Green width:'+getWidth('green'));
  23.         alert('Red width:'+getWidth('red'));
  24.         alert('Blue width:'+getWidth('blue'));
  25.         alert('Green width == Red+Blue = '+(getWidth('green')==(getWidth('red')+getWidth('blue'))));
  26. }
  27. function openWindow() {
  28.         window.open(window.location, '', 'width=500, height300');
  29. }
  30. </script>
  31. </body>
  32. </html>
复制代码
补充:示例代码中是采用的20%和80%,如果是其他百分比(19% 81%),width%?==0的?可能会是其他值,这种情况应该能够说明,渲染的算法可能是小数点进位上出现了问题。
紫衿 发表于 2011-7-30 09:24 | 显示全部楼层
猜測可能算的寬度的時候直接就把小數點后面的給忽略了。少了些算法快些三,你也可以這樣理解 chrome是便宜貨嘛 將就點用。

broken 发表于 2011-7-30 09:24 | 显示全部楼层
chrome 4.0.279.0
表示没有重现

话说我当年 发表于 2011-7-30 09:24 | 显示全部楼层
chrome 4.0.2222.12
同样有问题.

Jethro 发表于 2011-7-30 09:25 | 显示全部楼层
不单单只有Chrome会有这个问题,而是基于Webkit和Presto(Opera)引擎的浏览器都出现了这个问题,我又进一步测试过了,应该能确定是小数点进位的问题了。

fl 发表于 2011-7-30 09:25 | 显示全部楼层
我觉得还是尽量不要用这种方式写百分比的好,你觉得浏览器应该怎么处理才好?四舍五入?会引发更多问题的baby。因为浏览器也不知道你到底要怎么做。好比说一共50像素,分成33%、34%、33%,你觉得浏览器计算出什么样呢width更好一些呢?


郁闷小男人 发表于 2011-8-7 11:08 | 显示全部楼层
我赞成。嘿嘿
找不到我 发表于 2011-8-11 14:30 | 显示全部楼层
不错 不错  比我强多了
木已 发表于 2011-8-13 10:57 | 显示全部楼层
今天再看下
木已 发表于 2011-8-17 12:21 | 显示全部楼层
…没我说话的余地…飘走
走就走吧 发表于 2011-9-15 10:24 | 显示全部楼层
我帮不了你
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-30 13:52 , Processed in 0.127801 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表