waypoints-inview.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. Waypoints Inview Shortcut - 4.0.1
  3. Copyright © 2011-2016 Caleb Troughton
  4. Licensed under the MIT license.
  5. https://github.com/imakewebthings/waypoints/blob/master/licenses.txt
  6. */
  7. (function() {
  8. 'use strict'
  9. function noop() {}
  10. var Waypoint = window.Waypoint
  11. /* http://imakewebthings.com/waypoints/shortcuts/inview */
  12. function Inview(options) {
  13. this.options = Waypoint.Adapter.extend({}, Inview.defaults, options)
  14. this.axis = this.options.horizontal ? 'horizontal' : 'vertical'
  15. this.waypoints = []
  16. this.element = this.options.element
  17. this.createWaypoints()
  18. }
  19. /* Private */
  20. Inview.prototype.createWaypoints = function() {
  21. var configs = {
  22. vertical: [{
  23. down: 'enter',
  24. up: 'exited',
  25. offset: '100%'
  26. }, {
  27. down: 'entered',
  28. up: 'exit',
  29. offset: 'bottom-in-view'
  30. }, {
  31. down: 'exit',
  32. up: 'entered',
  33. offset: 0
  34. }, {
  35. down: 'exited',
  36. up: 'enter',
  37. offset: function() {
  38. return -this.adapter.outerHeight()
  39. }
  40. }],
  41. horizontal: [{
  42. right: 'enter',
  43. left: 'exited',
  44. offset: '100%'
  45. }, {
  46. right: 'entered',
  47. left: 'exit',
  48. offset: 'right-in-view'
  49. }, {
  50. right: 'exit',
  51. left: 'entered',
  52. offset: 0
  53. }, {
  54. right: 'exited',
  55. left: 'enter',
  56. offset: function() {
  57. return -this.adapter.outerWidth()
  58. }
  59. }]
  60. }
  61. for (var i = 0, end = configs[this.axis].length; i < end; i++) {
  62. var config = configs[this.axis][i]
  63. this.createWaypoint(config)
  64. }
  65. }
  66. /* Private */
  67. Inview.prototype.createWaypoint = function(config) {
  68. var self = this
  69. this.waypoints.push(new Waypoint({
  70. context: this.options.context,
  71. element: this.options.element,
  72. enabled: this.options.enabled,
  73. handler: (function(config) {
  74. return function(direction) {
  75. self.options[config[direction]].call(self, direction)
  76. }
  77. }(config)),
  78. offset: config.offset,
  79. horizontal: this.options.horizontal
  80. }))
  81. }
  82. /* Public */
  83. Inview.prototype.destroy = function() {
  84. for (var i = 0, end = this.waypoints.length; i < end; i++) {
  85. this.waypoints[i].destroy()
  86. }
  87. this.waypoints = []
  88. }
  89. Inview.prototype.disable = function() {
  90. for (var i = 0, end = this.waypoints.length; i < end; i++) {
  91. this.waypoints[i].disable()
  92. }
  93. }
  94. Inview.prototype.enable = function() {
  95. for (var i = 0, end = this.waypoints.length; i < end; i++) {
  96. this.waypoints[i].enable()
  97. }
  98. }
  99. Inview.defaults = {
  100. context: window,
  101. enabled: true,
  102. enter: noop,
  103. entered: noop,
  104. exit: noop,
  105. exited: noop
  106. }
  107. Waypoint.Inview = Inview
  108. }())
  109. ;