[原生JS]从构建组件到使用组件,依托组件化思想如何一步步写出dialog_demo组件

基于JavaScript,element-ui组件库中dialog实现。

ele-ui: Dialog

什么是组件化思想?

这个我学会了再告诉大家吧~

这个demo怎么做:

从构建组件到使用组件,其实就是通过写出一个可复用性强的class,我们想要使用这个组件,就需要利用这个class不断的构造对象实例出来。

步骤为:

  1. 找出Dialog页面的需求细节
    • 基本的打开关闭对话框功能:div.dialog的display属性更改;
    • 基本的渲染:div.dialog的transition属性:针对opacity透明度渐进、transform偏移滑入。
    • 给这个class赋予更强的复用性,在new的时候允许添加更多参数($root对象形式)params:options

2.1 针对需求细节1,完成HTML,CSS的雏形

DOM结构
- dialog 
  - main
    + header
      + title
      + span.close
    + content
    + footer
      + button.cancel
      + button.confirm
CSS雏形

2.2 针对需求细节1,写JS写到什么境界呢,你一点击就能弹出dialog来,点击取消键确认键也能有效。至此实现页面需求细节第一点;

  1. 根据页面需求细节2继续增加CSS和JS的内容。

  2. 根据页面需求细节3 ,添加params,改动JS内容。

在纸上写出DOM结构,一步步实现步骤2.1雏形:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>dialog对话框页面细节</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      box-shadow: 0 0 2px 0;
      border-radius: 4px;
      padding: 16px;
    }
    .button {
      padding: 5px;
      background-color: transparent;
      outline: none;
      border: 1px solid #ccc;
      border-radius: 2px;
    }
    .button:hover {
      cursor: pointer;
      border-color: lightskyblue;
      color: lightskyblue;
    }
    
    .dialog {
      position: fixed;
      background-color: rgba(0, 0, 0, 0.3);
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }
    .dialog .main {
      background: #fff;
      width: 40%;
      margin: 100px auto;
      border-radius: 6px;
      padding: 20px;
    }
    .dialog .header {
      display: flex;
      align-items: center;
    }
    .dialog .title {
      margin: 0;
    }
    .dialog .close {
      margin-left: auto;
      cursor: pointer;
    }
    .dialog .footer {
      display:flex;
      align-items:center;
      justify-content: flex-end;
    }
    .dialog .footer .button {
      margin-left: 10px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>Dialog组件</h2>
    <button class="button open-dlg">点我打开对话框。</button>
    <div class="dialog">
      <div class="main">
        <div class="header">
          <p>标题</p>
          <span class="iconfont icon-close close">❌</span>
        </div>
        <div class="content">
          <p>这是一句话</p></div>
        <div class="footer">
          <button class="btn-cancel button">取消</button>
          <button class="btn-confirm button">确定</button>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

一步步实现2.2.

先看怎么使用组件,丰满这个组件的内容:

<script>
  class Dialog {
    constructor($root) {

    }
  }
  let $ = s => document.querySelector(s)
  let dialog = new Dialog($('.dialog'))
  $('.open-dlg').onclick = function() {
    dialog.show()
  }
</script>

在为span.close绑定事件时犯了个错误,这还是我上一篇文章提到的,淦。
DOM节点无法像对象实例那样调用原型方法。

  <script>
    class Dialog {
      constructor($root) {
        this.$root = $root
        this.$close = $root.querySelector('.close')

        this.bind()
      }
      bind() {
        let self = this
        this.$close.onclick =function() {
          // self.$root.hide()
          // self.$root.classList.remove('show')
          self.hide()
        }
      }
      show() {
        this.$root.classList.add('show')
      }
      hide() {
        this.$root.classList.remove('show')
      }
    }
    let $ = s => document.querySelector(s)
    let dialog = new Dialog($('.dialog'))
    $('.open-dlg').onclick = function() {
      dialog.show()
    }
  </script>

照着span.close继续绑定;此时按钮生效,需求细节1实现。

<script>
  class Dialog {
    constructor($root) {
      this.$root = $root
      this.$close = $root.querySelector('.close')

      this.bind()
    }
    bind() {
      let self = this
      this.$close.onclick =function() {
        // self.$root.hide()
        // self.$root.classList.remove('show')
        self.hide()
      }
      this.$root.querySelector('.btn-cancel').onclick = function() {
        self.hide()
      }
      this.$root.querySelector('.btn-confirm').onclick = function() {
        self.hide()
      }
    }
    show() {
      this.$root.classList.add('show')
    }
    hide() {
      this.$root.classList.remove('show')
    }
  }
  let $ = s => document.querySelector(s)
  let dialog = new Dialog($('.dialog'))
  $('.open-dlg').onclick = function() {
    dialog.show()
  }
</script>

步骤3,往CSS和JS添加渐近细节

如果有代码对比插件就好了,不赘述。需要注意利用setTimeOut()将效果异步实现,否则JS引擎会进行优化,将所有渐近细节一步到位,这显然不是我们想要的。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>dialog</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0px rgba(0, 0, 0,  0.3);
      padding: 16px;
    }

    .button {
      padding: 8px 16px;
      font-size: 14px;
      font-weight: 500;
      color: #303030;
      border: 1px solid #ccc;
      border-radius: 4px;
      outline: none;
      cursor: pointer;
      /* position: relative; */
      background-color: transparent;
    }
    .button:hover {
    border-color: lightskyblue;
    color: lightskyblue
  }
  .dialog {
    background-color: rgba(0, 0, 0,  0.3);
    /* position: absolute; */
    position: fixed;
    /* width: 100%; */
    /* height: 100%; */
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;

    /*渐近细节*/
    opacity: 0;
    transition: all .3s;
  }
  .dialog.show {
    display: block;
  }
  .dialog .main {
    width: 40%;
    background-color: #fff;
    margin: 30px auto;
    border-radius: 6px;
    padding: 16px;

    /*渐近细节*/
    transform: translateY(-100%);
    transition: all .3s;
  }
  .dialog .header {
    display: flex;
    align-items: center;
  }
  .dialog .header > p {
    margin: 0;
  }
  .dialog .header .close {
    margin-left: auto;
    cursor: pointer;
  }
  .dialog .footer {
    display: flex;
    justify-content: flex-end;
  }
  .dialog .footer .button {
    margin-left: 15px;
  }

/*渐近细节*/
  .dialog.appear {
    opacity: 1;
  }
  .dialog.appear .main {
    transform: translateY(40px);
  }
  </style>
</head>
<body>
  <div class="container">
    <h2>Dialog组件</h2>
    <button class="button open-dlg">点我打开对话框。</button>
    <div class="dialog">
      <div class="main">
        <div class="header">
          <p>标题</p>
          <span class="iconfont icon-close close">❌</span>
        </div>
        <div class="content">
          <p>这是一句话</p></div>
        <div class="footer">
          <button class="btn-cancel button">取消</button>
          <button class="btn-confirm button">确定</button>
        </div>
      </div>
    </div>
  </div>
  
  <script>
    class Dialog {
      constructor($root, options) {
        this.$root = $root
        this.$close = $root.querySelector('.close')
        
        this.options = options

        this.bind()
      }
      bind() {
        let self = this
        this.$close.onclick =function() {
          // self.$root.hide()
          // self.$root.classList.remove('show')
          self.hide()
        }
        this.$root.querySelector('.btn-cancel').onclick = function() {
          self.hide()
        }
        this.$root.querySelector('.btn-confirm').onclick = function() {
          self.hide()
        }
      }
      show() {
        this.$root.classList.add('show')
        setTimeout(() => this.$root.classList.add('appear'))
      }
      hide() {
        this.$root.classList.remove('appear')
        setTimeout(() => this.$root.classList.remove('show'), 400)
      }
    }
    let $ = s => document.querySelector(s)
    let dialog = new Dialog($('.dialog'))
    $('.open-dlg').onclick = function() {
      dialog.show()
    }
  </script>
</body>
</html>

实现步骤4 构建组件时传入对象

对使用组件的方法进行修改:

一步步实现2.2.
先看怎么使用组件,丰满这个组件的内容:

<script>
  class Dialog {
    constructor($root) {

    }
  }
  let $ = s => document.querySelector(s)
  let dialog = new Dialog($('.dialog'))
  $('.open-dlg').onclick = function() {
    dialog.show()
  }
</script>

修正为传入onOk()、 onCancel();

<script>
  class Dialog {
    constructor($root, options) {

    }
  }
  let $ = s => document.querySelector(s)
  let dialog = new Dialog(document.querySelector('.dialog'), {
    onOk() {
      console.log('用户点了ok')
    },
    onCancel() {
      console.log('用户点了取消')
    }
  })
  $('.open-dlg').onclick = function() {
    dialog.show()
  }
</script>

最后这个组件就彻底成型了。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>dialog</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0px rgba(0, 0, 0,  0.3);
      padding: 16px;
    }

    .button {
      padding: 8px 16px;
      font-size: 14px;
      font-weight: 500;
      color: #303030;
      border: 1px solid #ccc;
      border-radius: 4px;
      outline: none;
      cursor: pointer;
      /* position: relative; */
      background-color: transparent;
    }
    .button:hover {
    border-color: lightskyblue;
    color: lightskyblue
  }
  .dialog {
    background-color: rgba(0, 0, 0,  0.3);
    /* position: absolute; */
    position: fixed;
    /* width: 100%; */
    /* height: 100%; */
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;

    /*渐近细节*/
    opacity: 0;
    transition: all .3s;
  }
  .dialog.show {
    display: block;
  }
  .dialog .main {
    width: 40%;
    background-color: #fff;
    margin: 30px auto;
    border-radius: 6px;
    padding: 16px;

    /*渐近细节*/
    transform: translateY(-100%);
    transition: all .3s;
  }
  .dialog .header {
    display: flex;
    align-items: center;
  }
  .dialog .header > p {
    margin: 0;
  }
  .dialog .header .close {
    margin-left: auto;
    cursor: pointer;
  }
  .dialog .footer {
    display: flex;
    justify-content: flex-end;
  }
  .dialog .footer .button {
    margin-left: 15px;
  }

/*渐近细节*/
  .dialog.appear {
    opacity: 1;
  }
  .dialog.appear .main {
    transform: translateY(40px);
  }
  </style>
</head>
<body>
  <div class="container">
    <h2>Dialog组件</h2>
    <button class="button open-dlg">点我打开对话框。</button>
    <div class="dialog">
      <div class="main">
        <div class="header">
          <p>标题</p>
          <span class="iconfont icon-close close">❌</span>
        </div>
        <div class="content">
          <p>这是一句话</p></div>
        <div class="footer">
          <button class="btn-cancel button">取消</button>
          <button class="btn-confirm button">确定</button>
        </div>
      </div>
    </div>
  </div>
  
  <script>
    class Dialog {
      constructor($root, options) {
        this.$root = $root
        this.$close = $root.querySelector('.close')
        
        this.options = options
        this.onOk = options.onOk || function(){}
        this.onCancel = options.onCancel || function(){}

        this.bind()
      }
      bind() {
        let self = this
        this.$close.onclick =function() {
          // self.$root.hide()
          // self.$root.classList.remove('show')
          self.hide()
          self.onCancel()
        }
        this.$root.querySelector('.btn-cancel').onclick = function() {
          self.hide()
          self.onCancel()
        }
        this.$root.querySelector('.btn-confirm').onclick = function() {
          self.hide()
          self.onOk();
        }
      }
      show() {
        this.$root.classList.add('show')
        setTimeout(() => this.$root.classList.add('appear'))
      }
      hide() {
        this.$root.classList.remove('appear')
        setTimeout(() => this.$root.classList.remove('show'), 400)
      }
    }
    let $ = s => document.querySelector(s)
    let dialog = new Dialog($('.dialog'), {
      onOk() {
        console.log('用户点了ok')
      },
      onCancel() {
        console.log('用户点了cancel')
      }
    })
    $('.open-dlg').onclick = function() {
      dialog.show()
    }
  </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容