zoukankan      html  css  js  c++  java
  • 使用原生js创建自定义标签

    使用原生js创建自定义标签

    1. 效果图

    2. 代码

      <!DOCTYPE html>
      <html lang="en">
      
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
      </head>
      
      <body>
          <div style="height: 100px;"></div>
          <popup-info img="img/alt.png" text="提示信息">
      </body>
      <script>
          class PopUpInfo extends HTMLElement {
              constructor() {
                  super();
      
                  // 创建文本框
                  var info = document.createElement('span');
                  info.setAttribute('class', 'info');
                  // 获取自定义标签的text属性
                  var text = this.getAttribute('text');
                  info.textContent = text;
      
                  // 创建图片元素
                  var imgUrl;
                  if (this.hasAttribute('img')) {
                      imgUrl = this.getAttribute('img');
                  } else {
                      imgUrl = 'img/default.png';
                  }
                  var img = document.createElement('img');
                  img.src = imgUrl;
      
                  var icon = document.createElement('span');
                  icon.setAttribute('class', 'icon');
                  icon.appendChild(img);
      
                  // 创建css样式
                  var style = document.createElement('style');
                  style.textContent = 
                  `
                      .wrapper {
                          position: relative;
                      }
                      .info {
                          font-size: 0.8rem;
                           200px;
                          display: inline-block;
                          border: 1px solid black;
                          padding: 10px;
                          background: white;
                          border-radius: 10px;
                          opacity: 0;
                          transition: 0.6s all;
                          position: absolute;
                          bottom: 20px;
                          left: 10px;
                          z-index: 3;
                      }
                      img {
                           1.2rem;
                      }
                      .icon:hover + .info, .icon:focus + .info {
                          opacity: 1;
                      }
                  `
      
                  // 创建根元素,作用其实是将分离的css和html聚合起来
                  var shadow = this.attachShadow({ mode: 'open' });
                  // 创建一个span标签包裹内容
                  var wrapper = document.createElement('span');
                  wrapper.setAttribute('class', 'wrapper');
                  
                  // 将创建的style节点追加到影子节点中
                  shadow.appendChild(style);
                  // 依次将html按照层级关系添加
                  shadow.appendChild(wrapper);
                  wrapper.appendChild(icon);
                  wrapper.appendChild(info);
              }
          }
      
          // 定义组件
          customElements.define('popup-info', PopUpInfo);
      
      </script>
      
      </html>
      
  • 相关阅读:
    [转]lftp的致命错误:证书验证:不信任
    github每次push都需要密码以及用户名的解决办法
    Fedora最小化安装后没有ifconfig命令
    [转载]MySql常用命令总结
    chrome浏览器强制采用https加密链接
    红帽系列linux自行配置本地yum源
    linux 下dd命令直接清除分区表(不用再fdisk一个一个的删除啦)
    linux分区工具fdisk的使用
    Java多线程实现......(1,继承Thread类)
    第一篇文章--我为什么要写博客?
  • 原文地址:https://www.cnblogs.com/ye-hcj/p/10352272.html
Copyright © 2011-2022 走看看