030a-rect.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>030a getBoundingClientRect</title>
  6. <style>
  7. html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
  8. body { font-family: sans-serif; font-size: 13px; padding: 10px; }
  9. h1 { font-size: 20px; margin: 0 0 6px 0; }
  10. p { margin: 0 0 10px 0; }
  11. #box { width: 200px; height: 40px; background: #cc3333; }
  12. #below { height: 20px; background: #333333; color: #ffffff; }
  13. #stage { width: 300px; height: 100px; background: #eeeeee; margin-top: 10px; }
  14. #abs { position: absolute; left: 20px; top: 10px; width: 50px; height: 30px; background: #2244cc; }
  15. #pic { width: 100px; height: 60px; margin-top: 10px; }
  16. #result { margin-top: 12px; padding: 8px; background: #eeeeee; font-family: monospace; font-weight: bold; }
  17. </style>
  18. </head>
  19. <body>
  20. <h1>030a &middot; getBoundingClientRect</h1>
  21. <p>The result must say PASS. This page exercises block, absolute and image boxes.</p>
  22. <div id="box"></div>
  23. <div id="below">below</div>
  24. <div id="stage"><div id="abs"></div></div>
  25. <img id="pic" src="PIC.BMP" alt="pic">
  26. <div id="result">checking...</div>
  27. <script>
  28. function near(a, b) { return Math.abs(a - b) <= 2; }
  29. function check() {
  30. var box = document.getElementById('box').getBoundingClientRect();
  31. var below = document.getElementById('below').getBoundingClientRect();
  32. var stage = document.getElementById('stage').getBoundingClientRect();
  33. var abs = document.getElementById('abs').getBoundingClientRect();
  34. var pic = document.getElementById('pic').getBoundingClientRect();
  35. var ok = near(box.width, 200) && near(box.height, 40) &&
  36. near(box.right - box.left, 200) &&
  37. near(below.top - box.bottom, 0) &&
  38. near(abs.width, 50) && near(abs.height, 30) &&
  39. near(abs.left - stage.left, 20) &&
  40. near(abs.top - stage.top, 10) &&
  41. near(pic.width, 100) && near(pic.height, 60);
  42. document.getElementById('result').textContent = ok ? 'PASS: rects match' :
  43. ('FAIL: box=' + box.width + 'x' + box.height +
  44. ' gap=' + (below.top - box.bottom) +
  45. ' abs=' + (abs.left - stage.left) + ',' + (abs.top - stage.top) +
  46. ' stageTop=' + stage.top + ' absTop=' + abs.top +
  47. ' pic=' + pic.width + 'x' + pic.height);
  48. }
  49. setInterval(check, 500);
  50. </script>
  51. </body>
  52. </html>