index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. event.stopPropagation();
  9. }
  10. function clickHandler ( event ) {
  11. event.target.remove();
  12. event.stopPropagation();
  13. }
  14. ['first', 'second', 'third', 'fourth'].forEach (
  15. function ( tag, index, arr ) {
  16. index === 0 ? (elem = document.body.appendChild(document.createElement('div')),
  17. elem.style = `width: 700px;
  18. height: 700px;
  19. position: relative;
  20. background-color: #ff00ff50;
  21. border: dotted 1px yellow;`,
  22. elem.title = tag) : (elem = document.getElementById(arr[index - 1]).appendChild(document.createElement('div')),
  23. elem.style = `background-color: #ff00ff50;
  24. border: dotted 1px yellow;
  25. position: absolute;
  26. top: 50px;
  27. left: 50px;`,
  28. elem.style.width = parseInt(elem.parentElement.style.width) - 100 ,
  29. elem.style.height = parseInt(elem.parentElement.style.height) - 100 ,
  30. elem.title = tag);
  31. elem.id = tag;
  32. elem.addEventListener('mouseover', over);
  33. elem.addEventListener('mouseout', out);
  34. elem.addEventListener('click', clickHandler);
  35. collection.push(elem);
  36. }, collection);