利用css‘content實現指令式tooltip文字提示

直接上圖

分析執行流程

  1. 鼠標移入節點
  2. 檢測是該節點是否存在開啓實現tooltip實現的標識(類名,屬性等)
  3. 檢測主題、位置(類名,屬性等)
  4. 生成 / 顯示氣泡

借鑑他人

讓咱們先來看看element-ui的tooltip樣式javascript

很明顯,氣泡的位置是經過javascript加上去的css


讓咱們來定幾個指望

  1. 不用javascript,純css實現;
  2. 不用添加新元素(用after、before僞元素)
  3. 不用類名匹配,直接用屬性選擇器[attr]
  4. 支持默認樣式(標籤沒定義主題、位置的時候)
  5. 指令式(直接在標籤訂義便可,接下來交給css匹配)
  6. 實現氣泡的主題、位置
  7. sass預處理器開發

html定義指令規範

指令式聲明:html

<button tooltip='我是內容鴨' effect='light' placement='top-left'>上左</button>
複製代碼
  • tooltip — 氣泡顯示的內容;
  • effect — 氣泡的主題(dark / light),默認dark;
  • placement — 氣泡相對於父元素的位置(top / top-left/ top-right / right / right-top/ right-bottom...),默認top;

先寫幾個按鈕

樣式借鑑element-ui 前端

<div class="container">
  <div class="top">
    <button tooltip="上邊" placement="top-left" effect="light">上左</button>
    <button tooltip="上左上左" placement="top">上邊</button>
    <button tooltip="上右" placement="top-right">上右</button>
  </div>
  <div class="left">
    <button tooltip="左上左上左上左上左上左上左上左上左上左上" placement="left-top">左上</button>
    <button tooltip="左邊" placement="left" effect="light">左邊</button>
    <button tooltip="左右" placement="left-bottom">左下</button>
  </div>
  <div class="right">
    <button tooltip="右上右上右上右上右上右上右上右上" placement="right-top">右上</button>
    <button tooltip="右邊" placement="right" effect="light">右邊</button>
    <button tooltip="右下" placement="right-bottom">右下</button>
  </div>
  <div class="bottom">
    <button tooltip="下左下左" placement="bottom-left">下左</button>
    <button tooltip="下邊" placement="bottom" effect="light">下邊</button>
    <button tooltip="下右" placement="bottom-right">下右</button>
  </div>
</div>
複製代碼

css核心代碼邏輯實現

hover監聽鼠標移入、移出,[tooltip]匹配有該屬性的標籤,after爲氣泡,before爲三角形:java

/* 匹配有tooltip屬性的元素 */
[tooltip] {
  position: relative;

  /* 氣泡默認樣式 */
  &::after {
    display: none;
    content: attr(tooltip);
  }

  /* 三角形默認樣式 */
  &::before {
    display: none;
    content: '';
  }

  /* 鼠標移入該元素,顯示氣泡、三角形 */
  &:hover {

    &::after {
      display: block;
    }

    &::before {
      display: block;
    }
  }
}
複製代碼

如今鼠標移入以後便有效果 element-ui


爲了方便看到效果,測試能夠把氣泡、三角形默認爲block

/* 氣泡默認樣式 */
&::after {
  display: block;
  content: attr(tooltip);
}

/* 三角形默認樣式 */
&::before {
  display: block;
  content: '';
}
複製代碼

目前效果以下:sass

設置氣泡、三角形的默認樣式

核心顯示固然是設置絕對定位了:bash

/* 氣泡默認樣式 */
&::after {
  display: block;
  content: attr(tooltip);
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 8px 15px;
  max-width: 200px;
  border-radius: 4px;
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.4);
  z-index: 100;
  @extend .tooltip-theme-dark; /* 繼承默認主題(黑底白字) */
 }

/* 三角形默認樣式 */
&::before {
  display: block;
  content: '';
  position: absolute;
  border: 5px solid transparent;
  z-index: 100;
  @extend .triangle-theme-dark; /* 繼承默認主題(黑底) */
}
複製代碼

目前效果以下:微信

定製氣泡、三角形主題色

定義好各兩種主題:學習

$white: #fff;
$black: #313131;

/* 氣泡主題 */
.tooltip-theme-dark {
  color: $white;
  background-color: $black;
}

.tooltip-theme-light {
  color: $black;
  background-color: $white;
  border: 1px solid $black;
}

/* 三角形主題 */
.triangle-theme-dark {
  border-top-color: $black;
}

.triangle-theme-light {
  border-top-color: $black; /* 暫時跟dark同樣 */
}
複製代碼

定製氣泡、三角形位置(只示例一個方向)

/* 氣泡位置 */

/*----上----*/
.tooltip-placement-top {
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translate(-50%, 0);
}

.tooltip-placement-top-right {
  bottom: calc(100% + 10px);
  left: 100%;
  transform: translate(-100%, 0)
}

.tooltip-placement-top-left {
  bottom: calc(100% + 10px);
  left: 0;
  transform: translate(0, 0)
}

/* 三角形位置 */

/*----上----*/
.triangle-placement-top {
  bottom: calc(100% + 5px);
  left: 50%;
  transform: translate(-50%, 0);
}

.triangle-placement-top-left {
  bottom: calc(100% + 5px);
  left: 10px;
}

.triangle-placement-top-right {
  bottom: calc(100% + 5px);
  right: 10px;
}
複製代碼

捕捉位置、主題

這裏也算最核心的代碼了,用屬性選擇器來匹配標籤上的值,而後設置不一樣的樣式

匹配氣泡、三角形主題:

&[effect="light"] {
  &::after {
    @extend .tooltip-theme-light;
  }

  &::before {
    @extend .triangle-theme-light;
  }
}
複製代碼

匹配氣泡、三角形位置,12種位置:

@each $placement in top,top-right,top-left,
right,right-top,right-bottom,
bottom,bottom-right,bottom-left,
left,left-top,left-bottom {
  &[placement="#{$placement}"] {
    &::after {
      @extend .tooltip-placement-#{$placement};
    }

    &::before {
      @extend .triangle-placement-#{$placement};
    }
  }
}
複製代碼

標籤不存在placement 屬性 / 爲空的時候,默認繼承top位置:

&:not([placement]),
&[placement=""] {
  &::after {
    @extend .tooltip-placement-top;
  }

  &::before {
    @extend .triangle-placement-top;
  }
}
複製代碼

目前效果以下:

讓咱們把文字放長,把氣泡、三角形的默認樣式加上display:none看看:

加個動畫

分四個方向,上下左右,四個動畫:

@keyframes anime-top {
  from {
    opacity: .5;
    bottom: 150%;
  }
}

@keyframes anime-bottom {
  from {
    opacity: .5;
    top: 150%;
  }
}

@keyframes anime-left {
  from {
    opacity: .5;
    right: 150%;
  }
}

@keyframes anime-right {
  from {
    opacity: .5;
    left: 150%;
  }
}
複製代碼

匹配氣泡位置從而來肯定執行哪一個動畫,用[attr^=]選擇,如 上左、上右也能匹配到:

/* 設置動畫 */
@each $placement in top,
 right,
bottom,
left {
  &[placement^="#{$placement}"] {

    &::after,
    &::before {
      animation: anime-#{$placement} 300ms ease-out forwards;
    }
  }
}
複製代碼

最終效果以下:

附上codepen地址codepen.io/anon/pen/yR…

最後

若是你以爲這篇文章不錯,請別忘記點個關注哦~😊

交流

公衆號「前端宇宙情報局」,將不定時更新最新、實用的前端技巧/技術性文章,對了偶爾還會有互聯網中的趣事趣聞🍻

關注公衆號,回覆"1"獲取微信羣聊二維碼,一塊兒學習、一塊兒交流、一塊兒摸魚🌊

相關文章
相關標籤/搜索