自定义 alert 弹窗

亦凉 2022-03-28 14:12 393阅读 0赞

1.css样式

li-alert.css

  1. @-webkit-keyframes opacity {
  2. 0% {
  3. opacity: 0; /*初始状态 透明度为0*/
  4. }
  5. 50% {
  6. opacity: 0; /*中间状态 透明度为0*/
  7. }
  8. 100% {
  9. opacity: 1; /*结尾状态 透明度为1*/
  10. }
  11. }
  12. .li-opacity {
  13. -webkit-animation-name: opacity; /*动画名称*/
  14. -webkit-animation-iteration-count: 1; /*动画次数*/
  15. -webkit-animation-delay: 0s; /*延迟时间*/
  16. }
  17. .alert-mask {
  18. position: fixed;
  19. height: 100%;
  20. width: 100%;
  21. left: 0%;
  22. top: 0%;
  23. z-index: 9998;
  24. background-color: rgba(0,0,0,.7);
  25. }
  26. .alert-content {
  27. position: fixed;
  28. box-sizing: border-box;
  29. border-radius: 4px;
  30. z-index: 9999;
  31. -webkit-transition: .4s;
  32. -moz-transition: .4s;
  33. transition: .4s;
  34. display: none;
  35. }
  36. .alert-show {
  37. display: block;
  38. }
  39. .alert-default {
  40. background-color: white;
  41. }

2.弹窗函数封装

li-alert.js

  1. /**
  2. * 常用的弹框组件
  3. * @author helijun
  4. * @return {[]} [depend jquery]
  5. */
  6. ;(function($){
  7. $.alert = function(opts){
  8. var defaultOpts = {
  9. title: '',//标题
  10. content: '',//内容 文字 || html
  11. height: 50,//默认屏幕(父级)的50%
  12. width: 80,//默认屏幕(父级)的80%
  13. type: 'alert-default',//弹框类型
  14. effect: 'fadeIn',//出现效果,默认下跌落
  15. delayTime: 500,//效果延时时间,默认.5s
  16. autoClose: false,//自动关闭
  17. autoTime: 2000, //自动关闭时间默认2s
  18. autoEffect: 'default',//关闭效果
  19. ok: '确定',
  20. okCallback: function(){},//确定回调
  21. cancel: '取消',
  22. cancelCallback: function(){},//取消回调
  23. before : function() {
  24. console.log('before')
  25. },
  26. close: function() {
  27. console.log('close')
  28. },
  29. blankclose: false//空白处点击关闭
  30. }
  31. for (i in defaultOpts) {
  32. if (opts[i] === undefined) {
  33. opts[i] = defaultOpts[i]
  34. }
  35. }
  36. opts.before && opts.before()
  37. var alertHtml = [
  38. '<section class="alert-main" id="alertMain">',
  39. '<div class="alert-mask li-opacity" id="alertMask"></div>',
  40. '<div class="alert-content '+ opts.type +'" id="alertContent">',
  41. opts.content +'</div>',
  42. '</section>'
  43. ]
  44. $('body').append(alertHtml.join(''))
  45. console.log('alertHtml',alertHtml.join(''))
  46. var $alertContent = $('#alertContent'),
  47. $alertMain = $('#alertMain');
  48. $alertContent.css({
  49. 'height': opts.height + '%',
  50. 'top': (100 - opts.height)/2 + '%',
  51. 'width': opts.width + '%',
  52. 'left': (100 - opts.width)/2 + '%'
  53. })
  54. $('.li-opacity').css({
  55. '-webkit-animation-duration' : opts.delayTime/1000 + 's'
  56. })
  57. var effect = {
  58. 'fadeIn': 'top',
  59. 'fadeInStart': '-100%',
  60. 'fadeInValue': (100 - opts.height)/2 + '%',
  61. 'sideLeft': 'left',
  62. 'sideLeftStart': '-100%',
  63. 'sideLeftValue': (100 - opts.width)/2 + '%',
  64. 'scale': '-webkit-transform',
  65. 'scaleStart': 'scale(0)',
  66. 'scaleValue': 'scale(1)',
  67. 'info': '-webkit-transform',
  68. 'infoStart': 'scale(1.2)',
  69. 'infoValue': 'scale(1)'
  70. }
  71. setTimeout(function(){
  72. $alertContent.css(effect[opts.effect],effect[opts.effect + 'Start']).addClass('alert-show')
  73. setTimeout(function(){
  74. $alertContent.css(effect[opts.effect], effect[opts.effect + 'Value'])
  75. opts.close && opts.close()
  76. },10)
  77. },opts.delayTime)
  78. if(opts.blankclose) {
  79. $('.alert-main :not(.alert-content)').on('click',function(event){
  80. $alertMain.remove()
  81. opts.close && opts.close()
  82. event.stopPropagation()
  83. event.preventDefault()
  84. })
  85. }
  86. if(opts.autoClose && opts.autoTime > 0) {
  87. setTimeout(function(){$alertMain.remove()},opts.autoTime)
  88. opts.close && opts.close()
  89. }
  90. }
  91. })(jQuery)

3.页面调用

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="apple-mobile-web-app-capable" content="yes">
  6. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  7. <!-- 视口,屏幕的宽度1:1,不允许缩放 -->
  8. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  9. <!-- 浏览器不缓存,每次都去服务器拉取 -->
  10. <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  11. <meta http-equiv="Pragma" content="no-cache" />
  12. <meta http-equiv="Expires" content="0" />
  13. <!-- 不自动识别手机号码 -->
  14. <meta name="format-detection" content="telephone=no" >
  15. <title>测试弹框组件</title>
  16. <link rel="stylesheet" type="text/css" href="css/li-alert.css">
  17. </head>
  18. <body>
  19. <p class="alert" data-flag="fadeIn">fadeIn</p>
  20. <p class="alert" data-flag="sideLeft">sideLeft</p>
  21. <p class="alert" data-flag="scale">scale</p>
  22. <p class="alert" data-flag="info">info</p>
  23. </body>
  24. <script src="js/jquery-3.2.1.min.js"></script>
  25. <script src="js/li-alert.js"></script>
  26. <script>
  27. $('.alert').on('click',function(event){
  28. var txt = "hello World";
  29. $.alert({
  30. content: '<h1 style="display:flex;justify-content:center;">我是弹框</h1>' + '<p>' + txt +'</p>',
  31. effect: $(event.currentTarget).attr('data-flag'),
  32. blankclose: true,
  33. //autoClose: true
  34. })
  35. });
  36. </script>
  37. </html>

.

发表评论

表情:
评论列表 (有 0 条评论,393人围观)

还没有评论,来说两句吧...

相关阅读

    相关 封装alert样式

    开发过程中,有些样式也是要注重美观的,对此,我们可以对一些基层方法做一些样式处理,是效果看起来更加的符合整体样式。 function alertInfo(message)\{