Home  >  Article  >  Web Front-end  >  Introduction to encapsulation examples of js custom bullet box plug-in

Introduction to encapsulation examples of js custom bullet box plug-in

零下一度
零下一度Original
2017-07-16 15:15:521673browse

First, let’s sort out our thoughts. Native javascript actually implements the alert() method, but that will temporarily interrupt the running of the program and is enough to make you ugly! So put aside these and think about it carefully, in fact, the pop-up frame is two p-layers, and a masking layer (mask layer) floating underneath, covering all elements, and it is best to be translucent. The other is the main part of the pop-up box. Generally, it needs to be centered horizontally and vertically, and usually contains a title. The main content needs to be customizable. If it is a modal box, there is usually a confirm/cancel button. Finally, there are some animation effects when popping up and closing.

Pop-up layer prompt information, this is the most common requirement in mobile front-end development. You may think of some popular pop-up box plug-ins, such as the classic artDialog, the cool Sweetalert, etc..

But slowly you will find that customization requirements are usually higher. General pop-up plug-ins may only meet most of the requirements. Customization is not as time-consuming as manually encapsulating one that suits your own development habits. Pop-up component, so that subsequent development efficiency will be greatly improved.

So you can encapsulate one yourself and put it in the public js in the project. If you can write it yourself, try not to use plug-ins....

Some default attribute values

pass through a foreach loop, similar to the incoming opts Inheriting the defaultOpts attribute, the before() method executed before calling the pop-up box is equivalent to some preparation work

var defaultOpts = {
        title: '',//标题
        content: '',//内容 文字 || html
        height: 50,//默认屏幕(父级)的50%
        width: 80,//默认屏幕(父级)的80%
        type: 'alert-default',//弹框类型
        effect: 'fadeIn',//出现效果,默认下跌落
        delayTime: 500,//效果延时时间,默认.5s
        autoClose: false,//自动关闭
        autoTime: 2000, //自动关闭时间默认2s
        autoEffect: 'default',//关闭效果
        ok: '确定',
        okCallback: function(){},//确定回调
        cancel: '取消',
        cancelCallback: function(){},//取消回调
        before : function() {
          console.log('before')
        }, 
        close: function() {
          console.log('close')
        },
        blankclose: false//空白处点击关闭
      }

    for (i in defaultOpts) {
      if (opts[i] === undefined) {
        opts[i] = defaultOpts[i]
      }
    }
  
  opts.before && opts.before()

dom structure

definition An array object, which contains the DOM element of the pop-up box. alert-mask is the full-screen mask layer, and alert-content is the main content area of ​​the pop-up box. Finally, the array is converted to html through the .join('') function, and then used The append() method of jquery is appended to the end of the body node.


var alertHtml = [
        &#39;<section class="alert-main" id="alertMain">&#39;,
          &#39;<p class="alert-mask li-opacity" id="alertMask"></p>&#39;,
          &#39;<p class="alert-content &#39;+ opts.type +&#39;" id="alertContent">&#39;,
          opts.content +&#39;</p>&#39;,
        &#39;</section>&#39;
      ]

    $(&#39;body&#39;).append(alertHtml.join(&#39;&#39;))

Set the height and width, horizontally and vertically centered

I use fixed positioning here, and height is the height passed in (Percentage), the distance between top and the top of the screen is 100-the incoming height/2. This achieves vertical centering, and the same goes for the width. This method of horizontal and vertical centering is also what I think is the simplest and most practical after long-term practice. It is compatible with various screen sizes. Of course, there are many methods, you can try it yourself


##

var $alertContent = $(&#39;#alertContent&#39;),
      $alertMain = $(&#39;#alertMain&#39;);

    $alertContent.css({
      &#39;height&#39;: opts.height + &#39;%&#39;,
      &#39;top&#39;: (100 - opts.height)/2 + &#39;%&#39;,
      &#39;width&#39;: opts.width + &#39;%&#39;,
      &#39;left&#39;: (100 - opts.width)/2 + &#39;%&#39;
    })

    $(&#39;.li-opacity&#39;).css({
      &#39;-webkit-animation-duration&#39; : opts.delayTime/1000 + &#39;s&#39;
    })

The last sentence is to assign an animation execution time to the mask layer to achieve the fade-in effect. For details, see the CSS below @-webkit-keyframes opacity

Bomb frame effect

I have implemented four effects here, namely fadeIn falling and sideLeft flying from the left Enter, scale amplification, and info prompt information. As you can see, I defined a collection object, placed the corresponding css attributes respectively, and then assigned values ​​uniformly through two setTimeout functions


var effect = {
      &#39;fadeIn&#39;: &#39;top&#39;,
      &#39;fadeInStart&#39;: &#39;-100%&#39;,
      &#39;fadeInValue&#39;: (100 - opts.height)/2 + &#39;%&#39;,
      &#39;sideLeft&#39;: &#39;left&#39;,
      &#39;sideLeftStart&#39;: &#39;-100%&#39;,
      &#39;sideLeftValue&#39;: (100 - opts.width)/2 + &#39;%&#39;,
      &#39;scale&#39;: &#39;-webkit-transform&#39;,
      &#39;scaleStart&#39;: &#39;scale(0)&#39;,
      &#39;scaleValue&#39;: &#39;scale(1)&#39;,
      &#39;info&#39;: &#39;-webkit-transform&#39;,
      &#39;infoStart&#39;: &#39;scale(1.2)&#39;,
      &#39;infoValue&#39;: &#39;scale(1)&#39;
    }

    setTimeout(function(){
      $alertContent.css(effect[opts.effect],effect[opts.effect + &#39;Start&#39;]).addClass(&#39;alert-show&#39;)

      setTimeout(function(){
        $alertContent.css(effect[opts.effect], effect[opts.effect + &#39;Value&#39;])
        opts.close && opts.close()
      },10)
    },opts.delayTime)

Click on the blank space Close

Usually the requirement is to click on the blank space of the pop-up box to close the pop-up box. This can be done with a click event. The focus is on the previous selector. jquery has given us so much convenience... . Of course, in the end, in order to prevent clicking on other elements of the page and prevent events from bubbling up, the default behavior of the component is...


if(opts.blankclose) {
      $(&#39;.alert-main :not(.alert-content)&#39;).on(&#39;click&#39;,function(event){
        $alertMain.remove()
        opts.close && opts.close()
        event.stopPropagation()
        event.preventDefault()
      })
    }

Automatically close

When autoClose is true and autoTime is greater than zero, use a delay event to automatically close the pop-up box


if(opts.autoClose && opts.autoTime > 0) {
      setTimeout(function(){$alertMain.remove()},opts.autoTime)
      opts.close && opts.close()
    }

Demo:

css


@-webkit-keyframes opacity {
      0% {
        opacity: 0; /*初始状态 透明度为0*/
      }
      50% {
        opacity: 0; /*中间状态 透明度为0*/
      }
      100% {
        opacity: 1; /*结尾状态 透明度为1*/
      }
    }

    .li-opacity {
      -webkit-animation-name: opacity; /*动画名称*/
      -webkit-animation-iteration-count: 1; /*动画次数*/
      -webkit-animation-delay: 0s; /*延迟时间*/
    }
    .alert-mask {
      position: fixed;
      height: 100%;
      width: 100%;
      left: 0%;
      top: 0%;
      z-index: 9998;
      background-color: rgba(0,0,0,.7);
    }
    .alert-content {
      position: fixed;
      box-sizing: border-box;
      border-radius: 4px;
      z-index: 9999;
      -webkit-transition: .4s;
      -moz-transition: .4s;
      transition: .4s;
      display: none;
    }
    .alert-show {
      display: block;
    }
    .alert-default {
      background-color: white;
    }

html

<p class="alert" data-flag="fadeIn">fadeIn</p>
<p class="alert" data-flag="sideLeft">sideLeft</p>
<p class="alert" data-flag="scale">scale</p>
<p class="alert" data-flag="info">info</p>

js

require.config({
  jquery:&#39;component/jquery/jquery-3.1.0.min&#39;,
  liAlert: &#39;li/li-alert&#39;,//常用弹框组件
})

require([&#39;jquery&#39;],function($){
    require([&#39;liAlert&#39;],function(){
      $(&#39;.alert&#39;).on(&#39;click&#39;,function(event){
        $.alert({
          content: &#39;<h1 style="display:flex;justify-content:center;">我是弹框</h1>&#39;,
          effect: $(event.currentTarget).attr(&#39;data-flag&#39;),
          blankclose: true,
          //autoClose: true
        })
      })
    })
  })

Rendering

The above is the detailed content of Introduction to encapsulation examples of js custom bullet box plug-in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn