059-absolute-image.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>059 absolute image and containing block</title>
  6. <style>
  7. body { background: #ffffff; color: #202020; font-family: sans-serif;
  8. margin: 12px; font-size: 18px; }
  9. #outer { position: relative; margin-left: 100px; margin-top: 20px;
  10. width: 480px; height: 240px; background: #eeeeee;
  11. overflow: hidden; }
  12. #abs { position: absolute; top: 10px; left: 20px; width: 50px;
  13. height: 20px; background: #cc2222; }
  14. #poster { position: absolute; top: 0; left: 0; width: 100%;
  15. height: auto; }
  16. #result { background: #ffc8c8; color: #000000; padding: 8px;
  17. margin-top: 12px; }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>059 &mdash; absolute image and containing block</h1>
  22. <p>An absolutely positioned poster must use its nearest positioned ancestor
  23. as the origin, and its percentage width must fill that block.</p>
  24. <div id="outer">
  25. <div id="abs"></div>
  26. <img id="poster" src="TILE.PNG" alt="">
  27. </div>
  28. <div id="result">checking...</div>
  29. <script>
  30. setTimeout(function () {
  31. function R(id) { return document.getElementById(id).getBoundingClientRect(); }
  32. var o = R('outer'), a = R('abs'), p = R('poster');
  33. var ok = Math.abs(a.left - (o.left + 20)) <= 2 &&
  34. Math.abs(a.top - (o.top + 10)) <= 2 &&
  35. Math.abs(p.left - o.left) <= 2 &&
  36. Math.abs(p.top - o.top) <= 2 &&
  37. Math.abs(p.width - o.width) <= 2;
  38. var r = document.getElementById('result');
  39. if (r) {
  40. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  41. r.textContent = (ok ? 'ABS-OK ' : 'ABS-FAIL ') +
  42. 'outer=' + Math.round(o.left) + ',' + Math.round(o.top) +
  43. ' abs=' + Math.round(a.left) + ',' + Math.round(a.top) +
  44. ' poster=' + Math.round(p.left) + ',' + Math.round(p.top) +
  45. ' ' + Math.round(p.width) + 'x' + Math.round(p.height);
  46. }
  47. }, 300);
  48. </script>
  49. </body>
  50. </html>