index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. let collection = []
  2. function over ( event ) {
  3. event.target.style.backgroundColor = '#ffff0050';
  4. event.stopPropagation();
  5. }
  6. function out ( event ) {
  7. event.target.style.backgroundColor = '#ff00ff50';
  8. }
  9. function clickHandler ( event ) {
  10. event.target.replaceWith(event.target.children[0]);
  11. event.stopPropagation();
  12. }
  13. [1, 2, 3, 4, 5, 6, 7].forEach (
  14. function ( tag, index, arr ) {
  15. index === 0 ? (elem = document.body.appendChild(document.createElement('div')),
  16. elem.style = `width: 900px;
  17. height: 900px;
  18. position: relative;
  19. background-color: #ff00ff50;
  20. border: dotted 1px yellow;`,
  21. elem.title = tag) : (elem = document.getElementById(arr[index - 1]).appendChild(document.createElement('div')),
  22. elem.style = `background-color: #ff00ff50;
  23. border: dotted 1px yellow;
  24. position: absolute;
  25. top: 50px;
  26. left: 50px;`,
  27. elem.style.width = parseInt(elem.parentElement.style.width) - 100 ,
  28. elem.style.height = parseInt(elem.parentElement.style.height) - 100 ,
  29. elem.title = tag);
  30. elem.id = tag;
  31. elem.addEventListener('mouseover', over);
  32. elem.addEventListener('mouseout', out);
  33. elem.addEventListener('click', clickHandler);
  34. collection.push(elem);
  35. }, collection);