原生js自定义一个拖拽分割容器组件效果代码

代码语言:html

所属分类:拖放

代码描述:原生js自定义一个拖拽分割容器组件效果代码

代码标签: 原生 js 自定义 拖拽 分割 容器 组件

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />
    <title>BFW NEW PAGE</title>

</head>
<body>
  <split-panel direction="row">
  <div slot="1">
      <h1> 我是左边</h1>
            <p>文本</p>
      <p>文本</p>
      <p>文本</p>
      <p>文本</p>
      </div>
  <div slot="2">
    <div class="info-panel">
      <h1>右边</h1>
      <p>文本</p>
      <p>文本</p>
      <p>文本</p>
      <p>文本</p>
    </div>
  </div>
</split-panel>

       <script  >
/*
  Not required for the calendar UI! Just a web component for playing with the size of it.
  https://dev.to/ndesmic/how-to-make-a-resizable-panel-control-with-web-components-2cpa
*/

function fireEvent(
  element,
  eventName,
  data,
  bubbles = true,
  cancelable = true
) {
  const event = document.createEvent("HTMLEvents");
  event.initEvent(eventName, bubbles, cancelable); // event type,bubbling,cancelable
  event.data = data;
  return element.dispatchEvent(event);
}

class WcSplitPanel extends HTMLElement {
  static observedAttributes = ["direction"];
  #direction = "row";
  #isResizing = false;
  constructor() {
    super();
    this.bind(this);
  }
  bind(element) {
    element.attachEvents = element.attachEvents.bind(element);
    element.render = element.render.bind(element);
    element.cacheDom = element.cacheDom.bind(element);
    element.pointerdown = element.pointerdown.bind(element);
    element.resizeDrag = element.resizeDrag.bind(element);
  }
  render() {
    this.attachShadow({ mode: "open" });
    this.shadowRoot.innerHTML = `
            <style>
                :host{ display: grid; }
                :host([resizing]){ user-select: none; }
                :host([resizing][direction=row]){ cursor: col-resize; }
                :host([direction=row]) { grid-template-columns: var(--first-size, 1fr) max-content var(--second-size, 1fr); }
                :host([direction=row]) #median { inline-size: 0.5rem; grid-column: 2 / 3; }
                :host([direction=row]) #median:hover { cursor: col-resize; }
                :host([direction=row]) #slot1 { grid-column: 1 / 2; grid-row: 1 / 1; }
                :host([direction=row]) #slot2 { grid-column: 3 / 4; grid-row: 1 / 1; }

                :host([resizing][direction=col]){ cursor: row-resize; }
                :host([direction=column]) { grid-template-rows: var(--first-size, 1fr) max-content var(--second-size, 1fr); }
                :host([direction=column]) #median { block-size: 0.5rem; grid-row: 2 / 3; }
                :host([direction=column]) #median:hover { cursor: row-resize; }
                :host([direction=column]) #slot1 { grid-row: 1 / 2; grid-col.........完整代码请登录后点击上方下载按钮下载查看

网友评论0