origin-scroll.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var Util = require('./util'),
  4. Base = require('./base'),
  5. Core = require('./core'),
  6. Animate = require('./animate');
  7. var transformOrigin = Util.prefixStyle("transformOrigin");
  8. /**
  9. * @constructor
  10. * @param {object} cfg config for scroll
  11. * @extends XScroll
  12. * @example
  13. * var xscroll = new OriginScroll({
  14. * renderTo:"#scroll"
  15. * });
  16. * xscroll.render();
  17. */
  18. function OriginScroll(cfg) {
  19. OriginScroll.superclass.constructor.call(this, cfg);
  20. }
  21. Util.extend(OriginScroll, Core, {
  22. init: function() {
  23. var self = this;
  24. OriginScroll.superclass.init.call(this);
  25. self.resetSize();
  26. },
  27. /**
  28. * get scroll top value
  29. * @memberof OriginScroll
  30. * @return {number} scrollTop
  31. */
  32. getScrollTop: function() {
  33. return this.renderTo.scrollTop;
  34. },
  35. /**
  36. * get scroll left value
  37. * @memberof OriginScroll
  38. * @return {number} scrollLeft
  39. */
  40. getScrollLeft: function() {
  41. return this.renderTo.scrollLeft;
  42. },
  43. /**
  44. * vertical scroll absolute to the destination
  45. * @memberof SimuScroll
  46. * @param scrollTop {number} scrollTop
  47. * @param duration {number} duration for animte
  48. * @param easing {string} easing functio for animate : ease-in | ease-in-out | ease | bezier(n,n,n,n)
  49. **/
  50. scrollTop: function(y, duration, easing, callback) {
  51. var self = this;
  52. var y = Math.round(y);
  53. if (self.userConfig.lockY) return;
  54. var duration = duration || 0;
  55. var easing = easing || "quadratic";
  56. var config = {
  57. css: {
  58. scrollTop: y
  59. },
  60. duration: duration,
  61. easing: easing,
  62. run: function(e) {
  63. //trigger scroll event
  64. self.trigger("scroll", {
  65. scrollTop: self.getScrollTop(),
  66. scrollLeft: self.getScrollLeft()
  67. });
  68. },
  69. useTransition: false, //scrollTop
  70. end: callback
  71. };
  72. self.__timers.y = self.__timers.y || new Animate(self.renderTo, config);
  73. //run
  74. self.__timers.y.stop();
  75. self.__timers.y.reset(config);
  76. self.__timers.y.run();
  77. },
  78. /**
  79. * horizontal scroll absolute to the destination
  80. * @memberof SimuScroll
  81. * @param scrollLeft {number} scrollLeft
  82. * @param duration {number} duration for animte
  83. * @param easing {string} easing functio for animate : ease-in | ease-in-out | ease | bezier(n,n,n,n)
  84. **/
  85. scrollLeft: function(x, duration, easing, callback) {
  86. var self = this;
  87. var x = Math.round(x);
  88. if (self.userConfig.lockX) return;
  89. var duration = duration || 0;
  90. var easing = easing || "quadratic";
  91. var config = {
  92. css: {
  93. scrollLeft: x
  94. },
  95. duration: duration,
  96. easing: easing,
  97. run: function(e) {
  98. //trigger scroll event
  99. self.trigger("scroll", {
  100. scrollTop: self.getScrollTop(),
  101. scrollLeft: self.getScrollLeft()
  102. });
  103. },
  104. useTransition: false, //scrollTop
  105. end: callback
  106. };
  107. self.__timers.x = self.__timers.x || new Animate(self.renderTo, config);
  108. //run
  109. self.__timers.x.stop();
  110. self.__timers.x.reset(config);
  111. self.__timers.x.run();
  112. },
  113. _bindEvt: function() {
  114. OriginScroll.superclass._bindEvt.call(this);
  115. var self = this;
  116. if (self.__isEvtBind) return;
  117. self.__isEvtBind = true;
  118. self.renderTo.addEventListener("scroll", function(e) {
  119. self.trigger("scroll", {
  120. type: "scroll",
  121. scrollTop: self.getScrollTop(),
  122. scrollLeft: self.getScrollLeft()
  123. })
  124. }, false)
  125. }
  126. });
  127. if (typeof module == 'object' && module.exports) {
  128. module.exports = OriginScroll;
  129. }
  130. /** ignored by jsdoc **/
  131. else {
  132. return OriginScroll;
  133. }
  134. });