menu.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict"
  2. define(function () {
  3. return function (menu) {
  4. return function () {
  5. var background = document.createElement("div")
  6. background.className = "menu-background"
  7. document.body.appendChild(background)
  8. document.body.classList.add("noscroll")
  9. var offset = this.getBoundingClientRect()
  10. var container = document.createElement("ul")
  11. container.className = "menu"
  12. container.style.top = offset.top + "px"
  13. container.style.left = offset.left + "px"
  14. background.onclick = destroy
  15. menu.forEach(function (item) {
  16. var li = document.createElement("li")
  17. li.textContent = item[0]
  18. li.action = item[1]
  19. li.onclick = function () {
  20. destroy()
  21. this.action()
  22. }
  23. container.appendChild(li)
  24. })
  25. document.body.appendChild(container)
  26. function destroy() {
  27. document.body.classList.remove("noscroll")
  28. document.body.removeChild(background)
  29. document.body.removeChild(container)
  30. }
  31. }
  32. }
  33. })