scrollslide.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * 侧边栏滑动
  3. * 通用性不好,请慎用
  4. */
  5. define(function(require, exports, module){
  6. var $ = require('lib/zepto'),
  7. slip = require('lib/slip'),
  8. ai = require('lib/ai');
  9. /**
  10. * 判断是否支持translate属性(目前只有webkit/firefox)
  11. */
  12. function isTranslateUseful(){
  13. return ai.ovb.webkit || ai.ovb.safari;
  14. }
  15. function ScrollSlide(param){
  16. this.options = $.extend({
  17. direction : 'left',//滑动方向
  18. slideContent : '',//侧边栏
  19. slideBody : [],//页面主体
  20. width : 0,
  21. height : 0
  22. }, param);
  23. this.animateQueue = [];
  24. this.isShow = false;
  25. }
  26. ScrollSlide.prototype = {
  27. toggle : function(){
  28. var that = this;
  29. //正在动画
  30. if(this.isProcessing){
  31. this.animateQueue.push(function(){
  32. that.toggle();
  33. });
  34. return;
  35. }
  36. this.isProcessing = true;
  37. this._animate();
  38. },
  39. _animate : function(){
  40. var width = this.options.width + 'px', height = this.options.height + 'px'
  41. , left = '0px', top = '0px', that = this, slideArray = this.options.slideBody;
  42. switch(this.options.direction){
  43. case 'left': left = '-' + width;break;
  44. case 'right': left = width;break;
  45. case 'top': top = '-' + height;break;
  46. case 'down': top = height;break;
  47. }
  48. if(isTranslateUseful()){
  49. if(this.isShow){
  50. //隐藏
  51. $(this.options.slideContent).css({
  52. 'zIndex' : -1
  53. });
  54. for(var i=0, len=slideArray.length; i<len; i++){
  55. (function(i){
  56. $(slideArray[i]).animate({'translate' : '0px, 0px'}, {'duration': 'fast', 'complete': function(){
  57. if(i == len-1){
  58. that._changeStatus();
  59. }
  60. }});
  61. })(i);
  62. }
  63. }
  64. else{
  65. //显示
  66. $(this.options.slideContent).css({
  67. 'zIndex' : -1,
  68. 'visibility' : 'visible',
  69. 'display' : ''
  70. });
  71. for(var i=0, len=slideArray.length; i<len; i++){
  72. (function(i){
  73. $(slideArray[i]).animate({'translate' : left + ', ' + top}, {'duration': 'fast', 'complete': function(){
  74. if(i == len-1){
  75. that._changeStatus();
  76. }
  77. }});
  78. })(i);
  79. }
  80. }
  81. }else{
  82. if(this.isShow){
  83. //隐藏
  84. $(this.options.slideContent).css({
  85. 'zIndex' : -1
  86. });
  87. for(var i=0, len=slideArray.length; i<len; i++){
  88. (function(i){
  89. $(slideArray[i]).animate({'right':'0px', 'top':'0px'}, {'duration': 'fast', 'complete': function(){
  90. if(i == len-1){
  91. that._changeStatus();
  92. }
  93. }});
  94. })(i);
  95. }
  96. }else{
  97. //显示
  98. $(this.options.slideContent).css({
  99. 'zIndex' : -1,
  100. 'visibility' : 'visible',
  101. 'display' : ''
  102. });
  103. for(var i=0, len=slideArray.length; i<len; i++){
  104. (function(i){
  105. $(slideArray[i]).animate({'right': width, 'top': top}, {'duration': 'fast', 'complete': function(){
  106. if(i == len-1){
  107. that._changeStatus();
  108. }
  109. }});
  110. })(i);
  111. }
  112. }
  113. }
  114. },
  115. _changeStatus : function(){
  116. if(this.isShow){
  117. $(this.options.slideContent).css({
  118. 'zIndex' : -1,
  119. 'visibility' : 'hidden',
  120. 'display' : 'none'
  121. });
  122. this.options.afterHide && this.options.afterHide();
  123. }else{
  124. $(this.options.slideContent).css({
  125. 'zIndex' : 1,
  126. 'visibility' : 'visible',
  127. 'display' : 'block'
  128. });
  129. this.options.afterShow && this.options.afterShow();
  130. }
  131. this.isShow = !this.isShow;
  132. this.isProcessing = false;
  133. var cb = this.animateQueue.shift();
  134. if(typeof cb == 'function'){
  135. cb();
  136. }
  137. }
  138. }
  139. return ScrollSlide;
  140. });