Guest User

Untitled

a guest
Jul 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import Scrollbar from 'smooth-scrollbar'
  2.  
  3. const smooth = {
  4. data() {
  5. return {
  6. smooth: {
  7. scrollbar: null,
  8. elements: [],
  9. cachedElements: []
  10. }
  11. }
  12. },
  13. mounted() {
  14. if (!this.$device.isMobileOrTablet) this.$_smooth_init()
  15. },
  16. destroy() {
  17. if (!this.$device.isMobileOrTablet) this.$_smooth_destroy()
  18. },
  19. methods: {
  20. $_smooth_listener() {
  21. this.smooth.scrollbar.addListener((status) => {
  22. this.$_smooth_run()
  23. })
  24. },
  25. $_smooth_setCache() {
  26. this.smooth.elements = this.$refs.scrollbar.querySelectorAll('[data-scroll]')
  27.  
  28. this.smooth.elements.forEach((el) => {
  29. if (el.offsetTop < window.innerHeight) return
  30.  
  31. const cachedElement = {
  32. element: el,
  33. animation: el.dataset.scroll,
  34. timeline: null,
  35. triggerOffset: el.dataset.offset || 0.25,
  36. isTriggered: false
  37. }
  38.  
  39. this.smooth.cachedElements.push(cachedElement)
  40. })
  41. },
  42. $_smooth_setAnimationState(el) {
  43. el.timeline = new TimelineLite({ paused: true })
  44.  
  45. el.timeline
  46. .from(el.element, 1, {
  47. y: 100,
  48. autoAlpha: 0,
  49. ease: Expo.easeOut
  50. })
  51.  
  52. el.timeline.progress(1).progress(0)
  53. },
  54. $_smooth_run() {
  55. this.smooth.cachedElements.forEach((el) => {
  56. if (el.isTriggered) return
  57.  
  58. if (this.smooth.scrollbar.isVisible(el.element)) {
  59. el.timeline.play()
  60. el.isTriggered = true
  61. }
  62. })
  63. },
  64. $_smooth_destroy() {
  65. this.smooth.scrollbar.destroy()
  66. },
  67. $_smooth_init() {
  68. this.smooth.scrollbar = Scrollbar.init(this.$refs.scrollbar)
  69. this.$_smooth_setCache()
  70. this.smooth.cachedElements.forEach((el) => this.$_smooth_setAnimationState(el))
  71. this.$_smooth_listener()
  72. }
  73. }
  74. }
  75.  
  76. export default smooth
Add Comment
Please, Sign In to add comment