base.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @fileoverview 图片放大效果 ImageZoom.
  3. * @author 玉伯<lifesinger@gmail.com>, 乔花<qiaohua@taobao.com>
  4. * @see silde.html
  5. */
  6. KISSY.add('imagezoom/base', function(S, DOM, Event, UA, Anim, UIBase, Node, Zoomer, undefined) {
  7. var IMAGEZOOM_ICON_TMPL = "<span class='{iconClass}'></span>",
  8. IMAGEZOOM_WRAP_TMPL = "<div class='{wrapClass}'></div>";
  9. function require(s) {
  10. return S.require("uibase/" + s);
  11. }
  12. function show(obj) {
  13. obj && obj.show();
  14. }
  15. function hide(obj) {
  16. obj && obj.hide();
  17. }
  18. return UIBase.create([require("boxrender"),
  19. require("contentboxrender"),
  20. require("positionrender"),
  21. require("loadingrender"),
  22. UA['ie'] == 6 ? require("shimrender") : null,
  23. require("align"),
  24. require("maskrender"),
  25. Zoomer
  26. ], {
  27. initializer:function() {
  28. var self = this,
  29. tmp;
  30. tmp = self.image = self.get('imageNode');
  31. // 在小图加载完毕时初始化
  32. tmp && Zoomer.__imgOnLoad(tmp, function() {
  33. if (!self.imageWrap) {
  34. self._render();
  35. self._bind();
  36. }
  37. });
  38. },
  39. /*renderUI:function() {
  40. },
  41. syncUI:function() {
  42. },
  43. bindUI: function() {
  44. },*/
  45. destructor: function() {
  46. var self = this;
  47. self.image.detach();
  48. },
  49. _render: function() {
  50. var self = this, wrap,
  51. image = self.image,
  52. elem = image.parent();
  53. if (elem.css('display') !== 'inline') {
  54. elem = image;
  55. }
  56. self.imageWrap = new Node(S.substitute(IMAGEZOOM_WRAP_TMPL, {
  57. wrapClass: self.get('wrapClass')
  58. })).insertBefore(elem);
  59. self.imageWrap.prepend(elem);
  60. if (self.get('showIcon')) {
  61. self.icon = new Node(S.substitute(IMAGEZOOM_ICON_TMPL, {
  62. iconClass: self.get("iconClass")
  63. }));
  64. self.imageWrap.append(self.icon);
  65. }
  66. },
  67. /**
  68. * 绑定鼠标进入/离开/移动事件, 只有进入, 才响应鼠标移动事件
  69. * @private
  70. */
  71. _bind: function() {
  72. var self = this,
  73. timer;
  74. self.image.on('mouseenter', function(ev) {
  75. if (!self.get('hasZoom')) return;
  76. timer = S.later(function() {
  77. self.set('currentMouse', ev);
  78. if (self._fresh) {
  79. self.set('align', self._fresh);
  80. self._fresh = undefined;
  81. }
  82. self.show();
  83. timer = undefined;
  84. }, 50);
  85. }).on('mouseleave', function() {
  86. if (timer) {
  87. timer.cancel();
  88. timer = undefined;
  89. }
  90. });
  91. self.on('afterVisibleChange', function(ev) {
  92. var isVisible = ev.newVal;
  93. if (isVisible) {
  94. hide(self.icon);
  95. } else {
  96. show(self.icon);
  97. }
  98. });
  99. },
  100. _uiSetHasZoom: function(v) {
  101. if (v) {
  102. show(this.icon);
  103. } else {
  104. hide(this.icon);
  105. }
  106. }
  107. },
  108. {
  109. ATTRS:{
  110. imageNode: {
  111. setter: function(el) {
  112. return Node.one(el);
  113. }
  114. },
  115. wrapClass: {
  116. value: 'ks-imagezoom-wrap'
  117. },
  118. imageWidth: {
  119. valueFn: function() {
  120. var img = this.get('imageNode');
  121. img = img && img.width();
  122. return img || 400;
  123. }
  124. },
  125. imageHeight: {
  126. valueFn: function() {
  127. var img = this.get('imageNode');
  128. img = img && img.height();
  129. return img || 400;
  130. }
  131. },
  132. imageLeft: {
  133. valueFn: function() {
  134. var img = this.get('imageNode');
  135. img = img && img.offset().left;
  136. return img || 400;
  137. }
  138. },
  139. imageTop: {
  140. valueFn: function() {
  141. var img = this.get('imageNode');
  142. img = img && img.offset().top;
  143. return img || 400;
  144. }
  145. },
  146. /**
  147. * 显示放大区域标志
  148. * @type {boolean}
  149. */
  150. hasZoom: {
  151. value: true,
  152. setter: function(v) {
  153. return !!v;
  154. }
  155. },
  156. /**
  157. * 是否显示放大镜提示图标
  158. * @type {boolean}
  159. */
  160. showIcon: {
  161. value: true
  162. },
  163. iconClass: {
  164. value: 'ks-imagezoom-icon'
  165. },
  166. prefixCls:{
  167. value: 'ks-'
  168. }
  169. }
  170. });
  171. }, {
  172. requires: ['dom','event', 'ua', 'anim', 'uibase', 'node', 'imagezoom/zoomer']
  173. });
  174. /**
  175. * NOTES:
  176. * 201101
  177. * - 重构代码, 基于 UIBase
  178. *
  179. */