042-computed-style.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>042 computed style</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. #t { display: block; position: relative; left: 12px; top: 7px;
  12. width: 150px; height: 40px; margin-left: 20px; padding: 6px;
  13. background-color: #ff0000; color: #0000ff; font-size: 18px;
  14. font-weight: bold; font-style: italic; text-align: center;
  15. z-index: 5; white-space: nowrap;
  16. transform: translate(3px, 4px) scale(1.5);
  17. border-top: 2px solid #00ff00; }
  18. #n { display: none; }
  19. #result { margin-top: 12px; padding: 8px; background: #eeeeee; color: #000000;
  20. font-family: monospace; font-weight: bold; }
  21. </style>
  22. </head>
  23. <body>
  24. <h1>042 &middot; getComputedStyle</h1>
  25. <p>The script checks the computed values of the styled box and a hidden element.</p>
  26. <div id="t">styled box</div>
  27. <div id="n">hidden</div>
  28. <div id="result">checking...</div>
  29. <script>
  30. setTimeout(function () {
  31. var cs = getComputedStyle(document.getElementById('t'));
  32. var hs = getComputedStyle(document.getElementById('n'));
  33. var checks = [
  34. ['display', 'block'], ['position', 'relative'], ['width', '150px'],
  35. ['height', '40px'], ['margin-left', '20px'], ['padding-left', '6px'],
  36. ['color', 'rgb(0, 0, 255)'], ['background-color', 'rgb(255, 0, 0)'],
  37. ['font-size', '18px'], ['font-weight', '700'], ['font-style', 'italic'],
  38. ['text-align', 'center'], ['z-index', '5'], ['left', '12px'],
  39. ['top', '7px'], ['border-top-width', '2px'],
  40. ['border-top-color', 'rgb(0, 255, 0)']
  41. ];
  42. var ok = true, details = [];
  43. for (var i = 0; i < checks.length; i++) {
  44. var got = cs.getPropertyValue(checks[i][0]);
  45. if (got !== checks[i][1]) {
  46. ok = false;
  47. details.push(checks[i][0] + '="' + got + '" want "' + checks[i][1] + '"');
  48. }
  49. }
  50. if (cs.marginLeft !== '20px' || cs.backgroundColor !== 'rgb(255, 0, 0)') {
  51. ok = false;
  52. details.push('direct properties');
  53. }
  54. if (hs.getPropertyValue('display') !== 'none') {
  55. ok = false;
  56. details.push('hidden display=' + hs.getPropertyValue('display'));
  57. }
  58. if (cs.transform.indexOf('scale(1.5') < 0) {
  59. ok = false;
  60. details.push('transform=' + cs.transform);
  61. }
  62. var out = document.getElementById('result');
  63. out.textContent = ok ? 'PASS: computed style properties' :
  64. ('FAIL: ' + details.join('; '));
  65. out.style.background = ok ? '#ccffcc' : '#ffcccc';
  66. }, 600);
  67. </script>
  68. </body>
  69. </html>