alert.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. 姓名:冯宇
  3. site: http://fy.035k.com
  4. 更新时间 :2018-04-02
  5. 更新内容 :添加actionsheet
  6. QQ : 515184405
  7. github : https://github.com/515184405/alert 还有一些其他的项目哦,欢迎star
  8. **/
  9. ;(function(){
  10. var zIndex = 10000;
  11. var jqueryAlert = function(opts){
  12. // 设置默认参数
  13. var opt = {
  14. 'style' : 'wap', //移动端和PC端
  15. 'title' : '', //标题
  16. 'icon' : '',
  17. 'content' : '', //内容
  18. 'contentTextAlign' : 'center', //内容对齐方式
  19. 'width' : 'auto', //宽度
  20. 'height' : 'auto', //高度
  21. 'minWidth' : '0', //最小宽度
  22. "className" : '', //添加类名
  23. 'position' : 'fixed', //定位方式
  24. 'animateType' : 'scale',
  25. 'modal' : false, //是否存在蒙层
  26. 'isModalClose' : false, //点击蒙层是否关闭
  27. 'bodyScroll' : false, //是否关闭body的滚动条
  28. 'closeTime' : 3000, //当没有按钮时关闭时间
  29. 'actionsheetCloseText' : '', //当当前样式为actionsheet时,关闭的文字按钮
  30. "buttons" : {}, //按钮对象</pre>
  31. };
  32. // 参数合并
  33. var option = $.extend({},opt,opts);
  34. var dialog = {
  35. };
  36. dialog.time = 450;//动画关闭时间
  37. dialog.init = function(){
  38. dialog.framework();
  39. };
  40. // 事件处理
  41. function IsPC() {
  42. var userAgentInfo = navigator.userAgent;
  43. var Agents = ["Android", "iPhone",
  44. "SymbianOS", "Windows Phone",
  45. "iPad", "iPod"];
  46. var flag = true;
  47. for (var v = 0; v < Agents.length; v++) {
  48. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  49. flag = false;
  50. break;
  51. }
  52. }
  53. return flag;
  54. }
  55. var isHaveTouch = IsPC();
  56. if(isHaveTouch){
  57. dialog.event = 'click';
  58. }else{
  59. dialog.event = 'touchstart';
  60. };
  61. var $modal = $("<div class='alert-modal none'>");
  62. var $container = $("<div class='alert-container animated'>");
  63. var $title = $("<div class='alert-title'>"+option.title+"</div>");
  64. var $content = $("<div class='alert-content'>");
  65. var $buttonBox = $("<div class='alert-btn-box'>");
  66. var $closeBtn = $("<div class='alert-btn-close'>×</div>");
  67. var $actionsheetCloseBtn = $("<p class='alert-btn-p alert-btn-sheet'>"+ option.actionsheetCloseText +"</p>");
  68. if(!!option.content[0] && (1 == option.content[0].nodeType)){
  69. var $newContent = option.content.clone();
  70. $content.append($newContent)
  71. }else{
  72. $content.html(option.content);
  73. };
  74. dialog.framework = function(){
  75. dialog.buttons = [];
  76. for(var key in option.buttons){
  77. dialog.buttons.push(key);
  78. };
  79. dialog.buttonsLength = dialog.buttons.length;
  80. option.style == 'actionsheet' ? $container.append($title) : $container.append($title) .append($content);
  81. if(option.style == 'pc'){
  82. $container.append($closeBtn).addClass('pcAlert');
  83. };
  84. if(option.style == 'actionsheet'){
  85. $container.addClass('alert-actionsheet');
  86. };
  87. if(option.modal || option.modal == 'true'){
  88. $('body').append($modal);
  89. $modal.fadeIn(dialog.time);
  90. option.bodyScroll && $('body').css('overflow','hidden');
  91. };
  92. $('body').append($container);
  93. // 设置内容的对齐方式
  94. $content.css({
  95. 'text-align' : option.contentTextAlign
  96. });
  97. if(parseInt(option.minWidth) > parseInt($container.css('width'))){
  98. option.width = option.minWidth;
  99. };
  100. $modal.css('position',option.position);
  101. $modal.css('z-index',zIndex);
  102. ++zIndex;
  103. if(option.position == 'fixed'){
  104. $container.css({
  105. 'position' : option.position,
  106. 'left' : '50%',
  107. 'top' : '50%',
  108. 'z-index' : zIndex,
  109. });
  110. };
  111. if(option.position == 'absolute'){
  112. $container.css({
  113. 'position' : option.position,
  114. 'left' : $(window).width()/2,
  115. 'top' : $(window).height()/2 + $(window).scrollTop(),
  116. 'z-index' : zIndex,
  117. });
  118. };
  119. $container.css('width',option.width);
  120. $container.css('height',option.height);
  121. if(option.width == 'auto'){
  122. $container.css('width',$container[0].clientWidth + 10);
  123. };
  124. if(parseInt($(window).height()) <= parseInt($container.css('height'))){
  125. $container.css('height',$(window).height());
  126. };
  127. // 设置class
  128. (!!option.className) && $container.addClass(option.className);
  129. // 设置button内容
  130. var indexs = 0;
  131. for(var key in option.buttons){
  132. indexs++;
  133. var $button = $("<p class='alert-btn-p alert-btn-p"+indexs+"'>"+ key +"</p>");
  134. if(option.style == 'wap'){
  135. $button.css({
  136. 'width' : Math.floor(($container[0].clientWidth) / dialog.buttonsLength),
  137. })
  138. };
  139. //绑定点击后的事件
  140. $button.bind(dialog.event,option.buttons[key]);
  141. $buttonBox.append($button);
  142. };
  143. if(dialog.buttonsLength > 0){
  144. $container.append($buttonBox);
  145. $content.css('padding-bottom','46px');
  146. //处理IE下滚动条的问题
  147. if (window.navigator.userAgent.indexOf("MSIE")>=1) {
  148. if($content[0].scrollHeight > $content[0].clientHeight){
  149. $content.css('height',parseInt($content.css('height')) - 46 );
  150. }
  151. };
  152. };
  153. if(option.title != ''){
  154. $content.css('padding-top','42px');
  155. };
  156. if(dialog.buttonsLength <= 0 && option.title == ''){
  157. $container.addClass('alert-container-black');
  158. if(!!option.icon){
  159. var img = new Image();
  160. img.onload = function(){
  161. $content.before(img);
  162. }
  163. img.src = option.icon
  164. $(img).css({
  165. 'width':'45px',
  166. 'height' : 'auto',
  167. 'display':'block',
  168. 'margin' : '10px auto 0 auto'
  169. })
  170. $content.css({
  171. 'padding-top' : '5px'
  172. })
  173. };
  174. };
  175. // 设置定位
  176. $container.css({
  177. 'margin-left' : -parseInt($container.css('width'))/2,
  178. 'margin-top' : -parseInt($container.css('height'))/2,
  179. });
  180. if( option.style == 'actionsheet' ){
  181. $container.css({
  182. 'position' : 'fixed',
  183. 'left' : 0,
  184. 'top' : 'auto',
  185. 'bottom' : '-100%',
  186. 'z-index' : zIndex,
  187. 'margin' : 0,
  188. 'width' : '100%',
  189. 'height' : 'auto',
  190. });
  191. $container.animate({
  192. 'bottom' : '0',
  193. },dialog.time);
  194. };
  195. if(option.style != 'actionsheet' && option.animateType == 'scale'){
  196. $container.addClass('bounceIn');
  197. };
  198. if(option.style != 'actionsheet' && option.animateType == 'linear'){
  199. $container.addClass('linearTop');
  200. };
  201. if(option.actionsheetCloseText){
  202. $buttonBox.append($actionsheetCloseBtn);
  203. $(document).delegate('.alert-btn-sheet,.alert-modal','click',function(){
  204. dialog.close();
  205. })
  206. }
  207. isSelfClose();
  208. };
  209. // 判断是否满足自动关闭的条件
  210. function isSelfClose(){
  211. if(dialog.buttonsLength <= 0 && option.style == 'wap'){
  212. setTimeout(function(){
  213. $container.fadeOut(dialog.time);
  214. $modal.fadeOut(dialog.time);
  215. option.bodyScroll && $('body').css('overflow','auto');
  216. },option.closeTime)
  217. };
  218. };
  219. dialog.toggleAnimate = function(){
  220. if(option.style != 'actionsheet'){
  221. if(option.animateType == 'scale'){
  222. return $container.removeClass('bounceIn').addClass('bounceOut');
  223. }else if(option.animateType == 'linear'){
  224. return $container.removeClass('linearTop').addClass('linearBottom');
  225. }else{
  226. return $container;
  227. };
  228. }else{
  229. return $container.animate({
  230. 'bottom' : '-100%',
  231. },dialog.time);
  232. }
  233. };
  234. dialog.close = function(){
  235. dialog.toggleAnimate().fadeOut(dialog.time);
  236. $modal.fadeOut(dialog.time);
  237. option.bodyScroll && $('body').css('overflow','auto');
  238. };
  239. option.style == 'pc' && $closeBtn.bind(dialog.event,dialog.close);
  240. option.isModalClose && $modal.bind(dialog.event,dialog.close);
  241. dialog.destroy = function(){
  242. dialog.toggleAnimate().fadeOut(dialog.time);
  243. setTimeout(function(){
  244. $container.remove();
  245. $modal.remove();
  246. option.bodyScroll && $('body').css('overflow','auto');
  247. },dialog.time)
  248. };
  249. dialog.show = function(){
  250. $modal.css('z-index',zIndex);
  251. ++zIndex;
  252. $container.css({
  253. 'z-index' : zIndex,
  254. });
  255. if( option.style != 'actionsheet' ){
  256. if(option.animateType == 'scale'){
  257. $container.fadeIn(dialog.time).removeClass('bounceOut').addClass('bounceIn');
  258. }else if(option.animateType == 'linear'){
  259. $container.fadeIn(dialog.time).removeClass('linearBottom').addClass('linearTop');
  260. }else{
  261. $container.fadeIn(dialog.time);
  262. };
  263. if(option.position == 'absolute'){
  264. $container.css({
  265. 'top' : $(window).height()/2 + $(window).scrollTop(),
  266. })
  267. };
  268. }else{
  269. $container.fadeIn(0).animate({
  270. 'bottom' : '0',
  271. },dialog.time);
  272. }
  273. $modal.fadeIn(dialog.time);
  274. option.bodyScroll && option.modal && $('body').css('overflow','hidden');
  275. isSelfClose();
  276. };
  277. dialog.init();
  278. // console.log(dialog);
  279. return dialog
  280. }
  281. if (typeof module !== 'undefined' && typeof exports === 'object') {
  282. module.exports = jqueryAlert;
  283. } else if (typeof define === 'function' && (define.amd || define.cmd)) {
  284. define(function() { return jqueryAlert; });
  285. } else {
  286. this.jqueryAlert = jqueryAlert;
  287. }
  288. }).call(function() {
  289. return this || (typeof window !== 'undefined' ? window : global);
  290. }());