| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>042 computed style</title>
- <style>
- html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
- body { font-family: sans-serif; font-size: 13px; padding: 10px; }
- h1 { font-size: 20px; margin: 0 0 6px 0; }
- p { margin: 0 0 10px 0; }
- #t { display: block; position: relative; left: 12px; top: 7px;
- width: 150px; height: 40px; margin-left: 20px; padding: 6px;
- background-color: #ff0000; color: #0000ff; font-size: 18px;
- font-weight: bold; font-style: italic; text-align: center;
- z-index: 5; white-space: nowrap;
- transform: translate(3px, 4px) scale(1.5);
- border-top: 2px solid #00ff00; }
- #n { display: none; }
- #result { margin-top: 12px; padding: 8px; background: #eeeeee; color: #000000;
- font-family: monospace; font-weight: bold; }
- </style>
- </head>
- <body>
- <h1>042 · getComputedStyle</h1>
- <p>The script checks the computed values of the styled box and a hidden element.</p>
- <div id="t">styled box</div>
- <div id="n">hidden</div>
- <div id="result">checking...</div>
- <script>
- setTimeout(function () {
- var cs = getComputedStyle(document.getElementById('t'));
- var hs = getComputedStyle(document.getElementById('n'));
- var checks = [
- ['display', 'block'], ['position', 'relative'], ['width', '150px'],
- ['height', '40px'], ['margin-left', '20px'], ['padding-left', '6px'],
- ['color', 'rgb(0, 0, 255)'], ['background-color', 'rgb(255, 0, 0)'],
- ['font-size', '18px'], ['font-weight', '700'], ['font-style', 'italic'],
- ['text-align', 'center'], ['z-index', '5'], ['left', '12px'],
- ['top', '7px'], ['border-top-width', '2px'],
- ['border-top-color', 'rgb(0, 255, 0)']
- ];
- var ok = true, details = [];
- for (var i = 0; i < checks.length; i++) {
- var got = cs.getPropertyValue(checks[i][0]);
- if (got !== checks[i][1]) {
- ok = false;
- details.push(checks[i][0] + '="' + got + '" want "' + checks[i][1] + '"');
- }
- }
- if (cs.marginLeft !== '20px' || cs.backgroundColor !== 'rgb(255, 0, 0)') {
- ok = false;
- details.push('direct properties');
- }
- if (hs.getPropertyValue('display') !== 'none') {
- ok = false;
- details.push('hidden display=' + hs.getPropertyValue('display'));
- }
- if (cs.transform.indexOf('scale(1.5') < 0) {
- ok = false;
- details.push('transform=' + cs.transform);
- }
- var out = document.getElementById('result');
- out.textContent = ok ? 'PASS: computed style properties' :
- ('FAIL: ' + details.join('; '));
- out.style.background = ok ? '#ccffcc' : '#ffcccc';
- }, 600);
- </script>
- </body>
- </html>
|