Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. <template>
  2. <div :class="card.container" :style="translateCard">
  3. <img :src="artistData.img.backgroundSrc" :class="card.background">
  4. <div :class="card.gradient" :style="gradientBackground"></div>
  5. <img :src="artistData.img.artistSrc" :alt="artistData.img.alt" :class="card.artist">
  6. </div>
  7. </template>
  8.  
  9. <script>
  10. let INITIAL_TRANSLATE_Y = null
  11. let FINAL_TRANSLATE_Y = null
  12. const INITIAL_ROTATE_Z = 0.2
  13. const FINAL_ROTATE_Z = 0.5
  14. const INITIAL_ROTATE_A = -25
  15. const FINAL_ROTATE_A = 25
  16. const INITIAL_GRADIENT = 0.1
  17. const FINAL_GRADIENT = 1.3
  18. const INITIAL_GRADIENT_A = 90
  19. const FINAL_GRADIENT_A = 45
  20. export default {
  21. name: 'artist-card',
  22. props: {
  23. artistData: Object,
  24. scrollY: Number
  25. },
  26. data: () => {
  27. return {
  28. translateCard: {
  29. transform: `translate3d(0, -100px, 0) rotate3d(0, 1, 0, -25deg)`
  30. },
  31. gradientBackground: {
  32. background: `linear-gradient(90deg, rgba(104, 107, 104, 0.1), rgba(0, 0, 0, 0.1))`
  33. }
  34. }
  35. },
  36. methods: {
  37. handleResize () {
  38. const bounds = this.$el.getBoundingClientRect()
  39. this.bounds = {
  40. top: bounds.top + (window.scrollY || window.pageYOffset),
  41. height: bounds.height
  42. }
  43. this.viewport = {
  44. height: window.innerHeight,
  45. width: window.innerWidth
  46. }
  47. }
  48. },
  49. created () {
  50. window.addEventListener('resize', this.handleResize)
  51. },
  52. mounted () {
  53. setTimeout(() => {
  54. this.handleResize()
  55. }, 100)
  56. },
  57. watch: {
  58. scrollY: function (val) {
  59. // only animate topY over 75% of vh
  60. const topY = this.bounds.top - (this.viewport.height * 0.75)
  61. const bottomY = this.bounds.top + this.bounds.height
  62.  
  63. // Change translateY depending on breakpoint
  64. if (this.viewport.width > 768) {
  65. INITIAL_TRANSLATE_Y = 70
  66. FINAL_TRANSLATE_Y = -140
  67. } else {
  68. INITIAL_TRANSLATE_Y = 30
  69. FINAL_TRANSLATE_Y = -30
  70. }
  71. // Determine percentage animation
  72. let pctPlay = 0
  73. if (this.scrollY >= topY && this.scrollY < bottomY) {
  74. const deltaY = this.scrollY - topY
  75. pctPlay = deltaY * 100 / (bottomY - topY)
  76. }
  77. if (this.scrollY > bottomY) {
  78. pctPlay = 100
  79. }
  80. // calculate translation Y
  81. let translateY = INITIAL_TRANSLATE_Y
  82. if (pctPlay > 0) {
  83. translateY = ((FINAL_TRANSLATE_Y - INITIAL_TRANSLATE_Y) * pctPlay / 100) + INITIAL_TRANSLATE_Y
  84. }
  85.  
  86. // calculate rotation Z
  87. let rotateZ = INITIAL_ROTATE_Z
  88. if (pctPlay > 0) {
  89. rotateZ = ((FINAL_ROTATE_Z - INITIAL_ROTATE_Z) * pctPlay / 100) + INITIAL_ROTATE_Z
  90. }
  91.  
  92. // calculate rotation angle
  93. let rotateA = INITIAL_ROTATE_A
  94. if (pctPlay > 0) {
  95. rotateA = ((FINAL_ROTATE_A - INITIAL_ROTATE_A) * pctPlay / 100) + INITIAL_ROTATE_A
  96. }
  97.  
  98. // calculate gradient value
  99. let gradient = INITIAL_GRADIENT
  100. if (pctPlay > 0) {
  101. gradient = ((FINAL_GRADIENT - INITIAL_GRADIENT) * pctPlay / 100) + INITIAL_GRADIENT
  102. }
  103.  
  104. // caluclate gradient angle
  105. let gradientA = INITIAL_GRADIENT_A
  106. if (pctPlay > 0) {
  107. gradientA = ((FINAL_GRADIENT_A - INITIAL_GRADIENT_A) * pctPlay / 100) + INITIAL_GRADIENT_A
  108. }
  109.  
  110. //
  111. this.translateCard.transform = `translate3d(0, ${translateY}px, 0) rotate3d(0, 1, ${rotateZ}, ${rotateA}deg)`
  112. this.gradientBackground.background = `linear-gradient(${gradientA}deg, rgba(104, 107, 104, 0.1), rgba(0, 0, 0, ${gradient}))`
  113. }
  114. }
  115. }
  116. </script>
  117.  
  118. <style module="card" src="./artist-card.css"></style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement