每次都要重讀 code 有點煩,所以直接寫在這裡。
簡述
這邊只會貼往下彈出的版本,如果要其他方向參考這裡,稍微改一下就 OK 了。
- 非 hover 的版本
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   |  @keyframes tooltips-vert {   to {     opacity: 0.9;     transform: translate(-50%, 0);   } }
  [tooltip] {   position: relative; }
  [tooltip]::before, [tooltip]::after {   font-size: 0.9em;   line-height: 1;   user-select: none;   pointer-events: none;   position: absolute;   opacity: 0;   animation: tooltips-vert 300ms ease-out forwards; }
  [tooltip]::before {   content: "";   border: 5px solid transparent;   z-index: 1001; }
  [tooltip]::after {   content: attr(tooltip);   font-family: Helvetica, sans-serif;   text-align: center;   min-width: 3em;   max-width: 21em;   overflow: hidden;   white-space: nowrap;   text-overflow: ellipsis;   padding: 1ch 1.5ch;   border-radius: 0.3ch;   box-shadow: 0 1em 2em -0.5em rgba(0, 0, 0, 0.35);   background-color: #333;   color: #fff;   z-index: 1000; }
  [tooltip]::before {   top: 100%;   border-top-width: 0;   border-bottom-color: #333; }
  [tooltip]::after {   top: calc(100% + 5px); }
  [tooltip]::before, [tooltip]::after {   left: 50%;   transform: translate(-50%, 0.5em); }
 
  | 
 
- hover 版本
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   | @keyframes tooltips-vert {   to {     opacity: 0.9;     transform: translate(-50%, 0);   } }
  [tooltip] {   position: relative; }
  [tooltip]::before, [tooltip]::after {      display: none;   font-size: 0.9em;   line-height: 1;   user-select: none;   pointer-events: none;   position: absolute;   opacity: 0; }
  [tooltip]::before {   content: "";   border: 5px solid transparent;   z-index: 1001; }
  [tooltip]::after {   content: attr(tooltip);   font-family: Helvetica, sans-serif;   text-align: center;   min-width: 3em;   max-width: 21em;   overflow: hidden;   white-space: nowrap;   text-overflow: ellipsis;   padding: 1ch 1.5ch;   border-radius: 0.3ch;   box-shadow: 0 1em 2em -0.5em rgba(0, 0, 0, 0.35);   background-color: #333;   color: #fff;   z-index: 1000; }
  [tooltip]::before {   top: 100%;   border-top-width: 0;   border-bottom-color: #333; }
  [tooltip]::after {   top: calc(100% + 5px); }
  [tooltip]::before, [tooltip]::after {   left: 50%;   transform: translate(-50%, 0.5em); }
  [tooltip]:hover::before, [tooltip]:hover::after {   display: block; }
  [tooltip]:hover::before, [tooltip]:hover::after {   animation: tooltips-vert 300ms ease-out forwards; }
  |