[Day06] - 新拟物风按钮(四) - 事件处理

昨天文末时 , 请 邦友 设定的 3 项属性不知道各位做出来了吗 ?

如果没有 , 可以到 neuomorphic-button-05.js 这里来参考作法 /images/emoticon/emoticon07.gif

如果 邦友 有其他作法 , 欢迎大家到昨天的留言区 , 留言分享您的作法 /images/emoticon/emoticon12.gif


事件分析

基础的 HTML 按钮 上有一个 onclick 事件 , 用来设定点击後 , 要触发哪一个函式

我们做的 neuomorphic-button 也是一个按钮 , 当然我们需要有一个 点击事件 , 让引用元件的人 , 能注册点击後的处理函式

下面 , 我们就将 点击事件 , 追加到 neuomorphic-button 元件上吧 !

常用的点击事件

开始实作之前 , 我们来确认一下 , 在页面中工程师常用那些手法设定 "click" 事件呢 ?

  1. <button>onclick 属性
<button onclick="clickHandler()" />
  1. EventTarget.addEventListener()
button.addEventListener('click', clickHandler )

也就是今天完成时 , 上方的 2 种事件 都可以在 neuomorphic-button 元件上使用

实作开始

one 将昨天的进度取出

class NeuomorphicButton extends HTMLElement {

  constructor() {

    super();

    const fontAwesomeStyle = `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">`

    // neumorphism 产生器 : https://neumorphism.io/#e0e0e0
    const styleStr = `[neumorphism 按钮样式]`;

    const icon = this.getAttribute('icon') || 'fas fa-wifi'
    const size = this.getAttribute('size') || 70
    const color = this.getAttribute('color') || '#6a9bd8'
    const bgColor = this.getAttribute('bg-color') || '#ebf5fc'

    const htmlStr = `
        <label>
          <input type="checkbox" name="">
          <div class="icon-box" style="width: ${size}px;height: ${size}px;background-color:${bgColor}">
              <i class="${icon}" style="font-size: ${size * 0.6}px;color:${color}"></i>
          </div>
        </label>
    `

    this.attachShadow({mode: 'open'}).innerHTML = fontAwesomeStyle + styleStr + htmlStr
  }
}

window.customElements.define('neuomorphic-button', NeuomorphicButton);

two 在 checkbox 加上 addEventListener 监听 checkbox 的变化

const checkbox = label.querySelector('input[type="checkbox"]')
checkbox.addEventListener('change', function (e) {

  console.log('checkbox 被点到')
});

three 产生一个新的 Event 并将其 dispatch 到元素上

const self = this

const checkbox = label.querySelector('input[type="checkbox"]')
checkbox.addEventListener('change', function (e) {

  // detail 就是 CustomEvent 内的参数
  const clickEvent = new CustomEvent('check', {composed: true, detail: {check: this.checked}});
  self.dispatchEvent(clickEvent)
});

之後我们注册 addEventListener('check' 到元素上 , 我们就可以看到事件的触发

// 注册范例 
document.querySelector('neuomorphic-button').addEventListener('check', e => console.log('注册 & 监听 check 事件 !!!', e.detail.check))

Pink Tree EventTarget.addEventListener() 的部分已解决 , 接下来处理 <button oncheck="checkHandler(e)" /> 的部分

four 处理 oncheck 属性

const self = this
+ const oncheck = this.getAttribute('oncheck')

const checkbox = label.querySelector('input[type="checkbox"]')
checkbox.addEventListener('change', function (e) {

      ...之前的设定
      
+      eval(oncheck)
    });

之後我们注册 oncheck="check(e)" 到元素上 , 我们就可以看到事件的触发

// 注册范例 
<neuomorphic-button icon="fas fa-lightbulb" oncheck="check(e)"></neuomorphic-button>

<script>
  function check(e) {
    console.log('oncheck 事件触发 !!!', e)
  }
</script>

成果

如果想直接体验成果 , 请到 web-component-addEventListener.html 查看

参考资料 :


<<:  DAY10 MongoDB 聚合(Aggregate)种类介绍

>>:  【第十天 - Two-pointer 介绍】

Day01 - 挑战前言

前言 今天是我挑战的第一天,这次挑战其实是一个偶然状况下,资深的前辈看到我正好开着铁人挑战赛的报名页...

Ubuntu巡航记(1) -- 在Windows作业系统下安装Ubuntu

前言 机器学习的套件许多都不能在Windows作业系统内顺利安装,就算能安装也要费一番手脚,因此,兴...

[Day 18] 针对网页的单元测试(四)

再写关於我们的页面 接下来我们要写 关於我们 跟 首页, 我们做简单一点就好, 主要是为了做测试而已...

来一道色香味俱全的 JavaScript 吧

终於 进入好想工作室第 131 天 终於 我也迎来了传说中的 it 铁人赛 思考了很久铁人赛的主题要...

Day27 - 在 Next.js 如何正确地使用 dynamic import

前言 在 Next.js 有一个很棒的优点是在 /pages 中的页面预设 next build 时...