Requires

Provides

Fx.Scroll.js

Effect to smoothly scroll any element, including the window.

License:
MIT-style license
Authors:
Valerio Proietti
  1. 27
  2. 28
  3. 29
  4. 30
  5. 31
  6. 32
  7. 33
  8. 34
  9. 35
  10. 36
  11. 37
  12. 38
  13. 39
  14. 40
  15. 41
  16. 42
  17. 43
  18. 44
  19. 45
  20. 46
  21. 47
  22. 48
  23. 49
  24. 50
  25. 51
  26. 52
  27. 53
  28. 54
  29. 55
  30. 56
  31. 57
  32. 58
  33. 59
  34. 60
  35. 61
  36. 62
  37. 63
  38. 64
  39. 65
  40. 66
  41. 67
  42. 68
  43. 69
  44. 70
  45. 71
  46. 72
  47. 73
  48. 74
  49. 75
  50. 76
  51. 77
  52. 78
  53. 79
  54. 80
  55. 81
  56. 82
  57. 83
  58. 84
  59. 85
  60. 86
  61. 87
  62. 88
  63. 89
  64. 90
  65. 91
  66. 92
  67. 93
  68. 94
  69. 95
  70. 96
  71. 97
  72. 98
  73. 99
  74. 100
  75. 101
  76. 102
  77. 103
  78. 104
  79. 105
  80. 106
  81. 107
  82. 108
  83. 109
  84. 110
  85. 111
  86. 112
  87. 113
  88. 114
  89. 115
  90. 116
  91. 117
  92. 118
  93. 119
  94. 120
  95. 121
  96. 122
  97. 123
  98. 124
  99. 125
  100. 126
  101. 127
  102. 128
  103. 129
  104. 130
  105. 131
  106. 132
  107. 133
  108. 134
  109. 135
  110. 136
  111. 137
  112. 138
  113. 139
  114. 140
  115. 141
  116. 142
  117. 143
  118. 144
  119. 145
  120. 146
  121. 147
  122. 148
  123. 149
  124. 150
Fx.Scroll = new Class({ Extends: Fx, options: { offset: {x: 0, y: 0}, wheelStops: true }, initialize: function(element, options){ this.element = this.subject = document.id(element); this.parent(options); var cancel = this.cancel.bind(this, false); if (typeOf(this.element) != 'element') this.element = document.id(this.element.getDocument().body); var stopper = this.element; if (this.options.wheelStops){ this.addEvent('start', function(){ stopper.addEvent('mousewheel', cancel); }, true); this.addEvent('complete', function(){ stopper.removeEvent('mousewheel', cancel); }, true); } }, set: function(){ var now = Array.flatten(arguments); if (Browser.firefox) now = [Math.round(now[0]), Math.round(now[1])]; this.element.scrollTo(now[0] + this.options.offset.x, now[1] + this.options.offset.y); }, compute: function(from, to, delta){ return [0, 1].map(function(i){ return Fx.compute(from[i], to[i], delta); }); }, start: function(x, y){ if (!this.check(x, y)) return this; var scrollSize = this.element.getScrollSize(), scroll = this.element.getScroll(), values = {x: x, y: y}; for (var z in values){ var max = scrollSize[z]; if (values[z] != null || values[z] === 0) values[z] = (typeOf(values[z]) == 'number') ? values[z] : max; else values[z] = scroll[z]; values[z] += this.options.offset[z]; } return this.parent([scroll.x, scroll.y], [values.x, values.y]); }, toTop: function(){ return this.start(false, 0); }, toLeft: function(){ return this.start(0, false); }, toRight: function(){ return this.start('right', false); }, toBottom: function(){ return this.start(false, 'bottom'); }, toElement: function(el){ var position = document.id(el).getPosition(this.element); return this.start(position.x, position.y); }, scrollIntoView: function(el, axes, offset){ axes = axes ? Array.from(axes) : ['x','y']; var to = {}; el = document.id(el); var pos = el.getPosition(this.element); var size = el.getSize(); var scroll = this.element.getScroll(); var containerSize = this.element.getSize(); var edge = { x: pos.x + size.x, y: pos.y + size.y }; ['x','y'].each(function(axis){ if (axes.contains(axis)){ if (edge[axis] > scroll[axis] + containerSize[axis]) to[axis] = edge[axis] - containerSize[axis]; if (pos[axis] < scroll[axis]) to[axis] = pos[axis]; } if (to[axis] == null) to[axis] = scroll[axis]; if (offset && offset[axis]) to[axis] = to[axis] + offset[axis]; }, this); if (to.x != scroll.x || to.y != scroll.y) this.start(to.x, to.y); return this; }, scrollToCenter: function(el, axes, offset){ axes = axes ? Array.from(axes) : ['x', 'y']; el = document.id(el); var to = {}, pos = el.getPosition(this.element), size = el.getSize(), scroll = this.element.getScroll(), containerSize = this.element.getSize(), edge = { x: pos.x + size.x, y: pos.y + size.y }; ['x','y'].each(function(axis){ if (axes.contains(axis)){ to[axis] = pos[axis] - (containerSize[axis] - size[axis])/2; } if (to[axis] == null) to[axis] = scroll[axis]; if (offset && offset[axis]) to[axis] = to[axis] + offset[axis]; }, this); if (to.x != scroll.x || to.y != scroll.y) this.start(to.x, to.y); return this; } });