原生js+css实现可配置弹出方向的文字popover弹出层效果代码

代码语言:html

所属分类:弹出层

代码描述:原生js+css实现可配置弹出方向的文字popover弹出层效果代码,可配置弹出的方向,还有是否悬浮弹出还是点击弹出。

代码标签: 原生 js css 可配置 弹出 方向 文字 弹出层 popover

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

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  
  
<style>
button {
  anchor-name: --anchor-btn;

  font-size: 1rem;
  border-radius: 2rem;
  height: 2rem;
  width: 2rem;
  display: grid;
  border: 2px solid black;
  place-content: center;
}

[popover] {
  --tether-offset: 1px;
  --tether-size: 8px;

  position-anchor: --anchor-btn;
  position: absolute;
  position-area: top;
  position-try: --bottom, --left, --right;

  margin: 0 0 0.5rem 0;
  clip-path: inset(var(--tether-offset)) margin-box;

  /* need this to see the tether */
  overflow: visible;

  max-width: 300px;

  background: black;
  color: white;
  border: none;
  padding: 1rem;
  border-radius: 1rem;

  &::before {
    content: "";
    position: absolute;
    z-index: -1;
    inset: calc(-1 * var(--tether-size)) calc(50% - var(--tether-size));
    background: inherit;
    clip-path: polygon(
      0 var(--tether-size),
      50% 0,
      100% var(--tether-size),
      100% calc(100% - var(--tether-size)),
      50% 100%,
      0 calc(100% - var(--tether-size))
    );
  }

  &::after {
    content: "";
    position: absolute;
    z-index: -1;
    inset: calc(50% - var(--tether-size)) calc(-1 * var(--tether-size));
    background: inherit;
    clip-path: polygon(
      0 var(--tether-size),
      var(--tether-size) 0,
      calc(100% - var(--tether-size)) 0,
      100% 50%,
      calc(100% - var(--tether-size)) 100%,
      var(--tether-size) 100%
    );
  }
}

@position-try --bottom {
  position-area: bottom;
  margin: 0.5rem 0 0 0;
}
@position-try --left {
  position-area: left;
  margin: 0 0.5rem 0 0;
}
@position-try --right {
  position-area: right;
  margin: 0 0 0 0.5rem;
}

.sr-only {
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

html,
body,
main {
  height: 100%;
  margin: 0;
}

main {
  display: flex;
  align-items: center;
  justify-content: center;

  &:has(input#left:checked) {
    align-items: center;
    justify-content: start;
  }
  &:has(input#right:checked) {
    align-items: center;
    justify-content: end;
  }
  &:has(input#top:checked) {
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0