layer.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. @Name:layer mobile v1.7 弹层组件移动版
  3. @Author:贤心
  4. @Site:http://layer.layui.com/mobie/
  5. @License:LGPL
  6. */
  7. ;!function(win){
  8. "use strict";
  9. var doc = document, query = 'querySelectorAll', claname = 'getElementsByClassName', S = function(s){
  10. return doc[query](s);
  11. };
  12. //默认配置
  13. var config = {
  14. type: 0,
  15. shade: true,
  16. shadeClose: true,
  17. fixed: true,
  18. anim: true
  19. };
  20. var ready = {
  21. extend: function(obj){
  22. var newobj = JSON.parse(JSON.stringify(config));
  23. for(var i in obj){
  24. newobj[i] = obj[i];
  25. }
  26. return newobj;
  27. },
  28. timer: {}, end: {}
  29. };
  30. //点触事件
  31. ready.touch = function(elem, fn){
  32. var move;
  33. if(!/Android|iPhone|SymbianOS|Windows Phone|iPad|iPod/.test(navigator.userAgent)){
  34. return elem.addEventListener('click', function(e){
  35. fn.call(this, e);
  36. }, false);
  37. }
  38. elem.addEventListener('touchmove', function(){
  39. move = true;
  40. }, false);
  41. elem.addEventListener('touchend', function(e){
  42. e.preventDefault();
  43. move || fn.call(this, e);
  44. move = false;
  45. }, false);
  46. };
  47. var index = 0, classs = ['layermbox'], Layer = function(options){
  48. var that = this;
  49. that.config = ready.extend(options);
  50. that.view();
  51. };
  52. Layer.prototype.view = function(){
  53. var that = this, config = that.config, layerbox = doc.createElement('div');
  54. that.id = layerbox.id = classs[0] + index;
  55. layerbox.setAttribute('class', classs[0] + ' ' + classs[0]+(config.type || 0));
  56. layerbox.setAttribute('index', index);
  57. var title = (function(){
  58. var titype = typeof config.title === 'object';
  59. return config.title
  60. ? '<h3 style="'+ (titype ? config.title[1] : '') +'">'+ (titype ? config.title[0] : config.title) +'</h3><button class="layermend"></button>'
  61. : '';
  62. }());
  63. var button = (function(){
  64. var btns = (config.btn || []).length, btndom;
  65. if(btns === 0 || !config.btn){
  66. return '';
  67. }
  68. btndom = '<span type="1">'+ config.btn[0] +'</span>'
  69. if(btns === 2){
  70. btndom = '<span type="0">'+ config.btn[1] +'</span>' + btndom;
  71. }
  72. return '<div class="layermbtn">'+ btndom + '</div>';
  73. }());
  74. if(!config.fixed){
  75. config.top = config.hasOwnProperty('top') ? config.top : 100;
  76. config.style = config.style || '';
  77. config.style += ' top:'+ ( doc.body.scrollTop + config.top) + 'px';
  78. }
  79. if(config.type === 2){
  80. config.content = '<i></i><i class="laymloadtwo"></i><i></i>';
  81. }
  82. if(config.type===3){
  83. layerbox.innerHTML = (config.shade ? '<div '+ (typeof config.shade === 'string' ? 'style="'+ config.shade +'"' : '') +' class="laymshade"></div>' : '')
  84. +'<div class="layermmain" '+ (!config.fixed ? 'style="position:static;"' : '') +'>'
  85. +'<div class="section">'
  86. +'<div class="layermchild '+ (config.className ? config.className : '') +' '+ ((!config.type && !config.shade) ? 'layermborder ' : '') + (config.anim ? 'layermanim' : '') +'" ' + ( config.style ? 'style="'+config.style+'"' : '' ) +'>'
  87. + title
  88. +'<div class="weui_loading">'
  89. +'<div class="weui_loading_leaf weui_loading_leaf_0"></div>'
  90. +'<div class="weui_loading_leaf weui_loading_leaf_1"></div>'
  91. +'<div class="weui_loading_leaf weui_loading_leaf_2"></div>'
  92. +'<div class="weui_loading_leaf weui_loading_leaf_3"></div>'
  93. +'<div class="weui_loading_leaf weui_loading_leaf_4"></div>'
  94. +'<div class="weui_loading_leaf weui_loading_leaf_5"></div>'
  95. +'<div class="weui_loading_leaf weui_loading_leaf_6"></div>'
  96. +'<div class="weui_loading_leaf weui_loading_leaf_7"></div>'
  97. +'<div class="weui_loading_leaf weui_loading_leaf_8"></div>'
  98. +'<div class="weui_loading_leaf weui_loading_leaf_9"></div>'
  99. +'<div class="weui_loading_leaf weui_loading_leaf_10"></div>'
  100. +'<div class="weui_loading_leaf weui_loading_leaf_11"></div>'
  101. +'</div>'
  102. +'<div class="layermcont">'+ config.content +'</div>'
  103. + button
  104. +'</div>'
  105. +'</div>'
  106. +'</div>';
  107. }else{
  108. layerbox.innerHTML = (config.shade ? '<div '+ (typeof config.shade === 'string' ? 'style="'+ config.shade +'"' : '') +' class="laymshade"></div>' : '')
  109. +'<div class="layermmain" '+ (!config.fixed ? 'style="position:static;"' : '') +'>'
  110. +'<div class="section">'
  111. +'<div class="layermchild '+ (config.className ? config.className : '') +' '+ ((!config.type && !config.shade) ? 'layermborder ' : '') + (config.anim ? 'layermanim' : '') +'" ' + ( config.style ? 'style="'+config.style+'"' : '' ) +'>'
  112. + title
  113. +'<div class="layermcont">'+ config.content +'</div>'
  114. + button
  115. +'</div>'
  116. +'</div>'
  117. +'</div>';
  118. }
  119. if(!config.type || config.type === 2){
  120. var dialogs = doc[claname](classs[0] + config.type), dialen = dialogs.length;
  121. if(dialen >= 1){
  122. layer.close(dialogs[0].getAttribute('index'))
  123. }
  124. }
  125. document.body.appendChild(layerbox);
  126. var elem = that.elem = S('#'+that.id)[0];
  127. config.success && config.success(elem);
  128. that.index = index++;
  129. that.action(config, elem);
  130. };
  131. Layer.prototype.action = function(config, elem){
  132. var that = this;
  133. //自动关闭
  134. if(config.time){
  135. ready.timer[that.index] = setTimeout(function(){
  136. layer.close(that.index);
  137. }, config.time*1000);
  138. }
  139. //关闭按钮
  140. if(config.title){
  141. var end = elem[claname]('layermend')[0], endfn = function(){
  142. config.cancel && config.cancel();
  143. layer.close(that.index);
  144. };
  145. ready.touch(end, endfn);
  146. }
  147. //确认取消
  148. var btn = function(){
  149. var type = this.getAttribute('type');
  150. if(type == 0){
  151. config.no && config.no();
  152. layer.close(that.index);
  153. } else {
  154. config.yes ? config.yes(that.index) : layer.close(that.index);
  155. }
  156. };
  157. if(config.btn){
  158. var btns = elem[claname]('layermbtn')[0].children, btnlen = btns.length;
  159. for(var ii = 0; ii < btnlen; ii++){
  160. ready.touch(btns[ii], btn);
  161. }
  162. }
  163. //点遮罩关闭
  164. if(config.shade && config.shadeClose){
  165. var shade = elem[claname]('laymshade')[0];
  166. ready.touch(shade, function(){
  167. layer.close(that.index, config.end);
  168. });
  169. }
  170. config.end && (ready.end[that.index] = config.end);
  171. };
  172. win.layer = {
  173. v: '1.8',
  174. index: index,
  175. //核心方法
  176. open: function(options){
  177. var o = new Layer(options || {});
  178. return o.index;
  179. },
  180. close: function(index){
  181. var ibox = S('#'+classs[0]+index)[0];
  182. if(!ibox) return;
  183. ibox.innerHTML = '';
  184. doc.body.removeChild(ibox);
  185. clearTimeout(ready.timer[index]);
  186. delete ready.timer[index];
  187. typeof ready.end[index] === 'function' && ready.end[index]();
  188. delete ready.end[index];
  189. },
  190. //关闭所有layer层
  191. closeAll: function(){
  192. var boxs = doc[claname](classs[0]);
  193. for(var i = 0, len = boxs.length; i < len; i++){
  194. layer.close((boxs[0].getAttribute('index')|0));
  195. }
  196. }
  197. };
  198. 'function' == typeof define ? define(function() {
  199. return layer;
  200. }) : function(){
  201. var js = document.scripts, script = js[js.length - 1], jsPath = script.src;
  202. var path = jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
  203. //如果合并方式,则需要单独引入layer.css
  204. if(script.getAttribute('merge')) return;
  205. document.head.appendChild(function(){
  206. var link = doc.createElement('link');
  207. link.href = path + 'need/layer.css';
  208. link.type = 'text/css';
  209. link.rel = 'styleSheet'
  210. link.id = 'layermcss';
  211. return link;
  212. }());
  213. }();
  214. }(window);