Advertisement
AlaminSakib

Untitled

Dec 18th, 2020
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. animate = () => {
  2.  
  3. // select random data-index of an image and its random neighbor to swap
  4. elems = document.querySelectorAll('[data-index]')
  5. // let imageToAnimate = Math.floor((Math.random() * elems.length) + 1)
  6. imageToAnimate = 1
  7. neighborToAnimateWith = [imageToAnimate + 1 === elems.length + 1 ? 1 : imageToAnimate + 1, imageToAnimate - 1 === 0 ? elems.length : imageToAnimate - 1][Math.floor((Math.random() * 2))]
  8. // neighborToAnimateWith = 2
  9. // get the html nodes
  10.  
  11. imageToAnimate = document.querySelector(`[data-index='${imageToAnimate}']`)
  12. neighborToAnimateWith = document.querySelector(`[data-index='${neighborToAnimateWith}']`)
  13.  
  14. console.log("before", imageToAnimate.style.transform, neighborToAnimateWith.style.transform)
  15.  
  16. imageToAnimate.style.transition = 'transform 1000ms ease-in-out'
  17. neighborToAnimateWith.style.transition = 'transform 1000ms ease-in-out'
  18.  
  19. // perform the animation
  20. let imgTemp = imageToAnimate.getAttribute('data-index')
  21. let nhbrTemp = neighborToAnimateWith.getAttribute('data-index')
  22.  
  23. // imageToAnimate.style.transform = `translate(${-neighborToAnimateWith.clientWidth}px, ${0}px)`
  24. // neighborToAnimateWith.style.transform = `translate(${-imageToAnimate.clientWidth}px, ${0}px)`
  25. if(nhbrTemp == 2) {
  26. imageToAnimate.style.transform = `translate${imageToAnimate.clientWidth}px, 0px)`
  27. neighborToAnimateWith.style.transform = `translate${-imageToAnimate.clientWidth}px, 0px)`
  28. // console.log(imageToAnimate)
  29. } else {
  30. imageToAnimate.style.transform = `translate(0px, ${neighborToAnimateWith.clientWidth}px)`
  31. neighborToAnimateWith.style.transform = `translate(0px, ${-imageToAnimate.clientWidth}px)`
  32. }
  33.  
  34. console.log("after", imageToAnimate.style.transform, neighborToAnimateWith.style.transform)
  35.  
  36. // if(parseFloat(neighborToAnimateWith.style.left) > parseFloat(imageToAnimate.style.left)) {
  37. // imageToAnimate.style.transform = `translate(${neighborToAnimateWith.clientWidth}px, ${0}px)`
  38. // neighborToAnimateWith.style.transform = `translate(${-imageToAnimate.clientWidth}px, ${0}px)`
  39. // console.log("first")
  40. // } else if(parseFloat(neighborToAnimateWith.style.left) < parseFloat(imageToAnimate.style.left)) {
  41. // imageToAnimate.style.transform = `translate(${-neighborToAnimateWith.clientWidth}px, ${0}px)`
  42. // neighborToAnimateWith.style.transform = `translate(${imageToAnimate.clientWidth}px, ${0}px)`
  43. // console.log("second")
  44. // } else if(parseFloat(neighborToAnimateWith.style.top) > parseFloat(imageToAnimate.style.top)) {
  45. // imageToAnimate.style.transform = `translate(${0}px, ${neighborToAnimateWith.clientWidth}px)`
  46. // neighborToAnimateWith.style.transform = `translate(${0}px, ${-imageToAnimate.clientWidth}px)`
  47. // console.log("third")
  48. // } else if(parseFloat(neighborToAnimateWith.style.top) > parseFloat(imageToAnimate.style.top)) {
  49. // imageToAnimate.style.transform = `translate(${0}px, ${-neighborToAnimateWith.clientWidth}px)`
  50. // neighborToAnimateWith.style.transform = `translate(${0}px, ${imageToAnimate.clientWidth}px)`
  51. // console.log("fourth")
  52. // }
  53. // let temp = imageToAnimate.style.left
  54. // imageToAnimate.style.left = neighborToAnimateWith.style.left
  55. // neighborToAnimateWith.style.left = temp
  56.  
  57. // temp = imageToAnimate.style.top
  58. // imageToAnimate.style.top = neighborToAnimateWith.style.top
  59. // neighborToAnimateWith.style.top = temp
  60.  
  61.  
  62. // imageToAnimate.style.removeProperty('transition');
  63. // neighborToAnimateWith.style.removeProperty('transition');
  64.  
  65. imageToAnimate.setAttribute('data-index', `${nhbrTemp}`);
  66. neighborToAnimateWith.setAttribute('data-index', `${imgTemp}`)
  67.  
  68. return
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement