disappearing_element.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. <title>An element that disappears on click</title>
  5. <style>
  6. #under {
  7. position: absolute;
  8. top: 20;
  9. left: 20;
  10. width: 100px;
  11. height: 100px;
  12. background-color: white;
  13. }
  14. #over {
  15. position: absolute;
  16. top: 20;
  17. left: 20;
  18. width: 100px;
  19. height: 100px;
  20. background-color: red;
  21. opacity: 0.5;
  22. }
  23. #log {
  24. position: absolute;
  25. top: 120px;
  26. }
  27. </style>
  28. </head>
  29. <body id="body">
  30. <div id="under"><p id="contents">Hello</p></div>
  31. <div id="over"></div>
  32. <div id="log">
  33. <p>Log:<p>
  34. </div>
  35. <script>
  36. var byId = document.getElementById.bind(document);
  37. var log = byId("log");
  38. function handler(ev) {
  39. log.innerHTML += "<p></p>";
  40. log.lastElementChild.textContent = ev.type + " in " + ev.target.id + " (handled by " + ev.currentTarget.id + ")\n";
  41. }
  42. var under = byId("under");
  43. var over = byId("over");
  44. var body = document.body;
  45. var types = ["click", "mousedown", "mouseup"];
  46. for (var i = 0, type; (type = types[i]); ++i) {
  47. under.addEventListener(type, handler);
  48. over.addEventListener(type, handler);
  49. body.addEventListener(type, handler);
  50. }
  51. over.addEventListener("mousedown", function () {
  52. over.style.display = "none";
  53. })
  54. </script>
  55. </body>
  56. </html>