CSS:懸浮提示文本

https://github.com/haizlin/fe-interview/issues/2008

Way 1⃣️

<div class="wrap">
  我是一個元素
  <div class="tips">
    這是懸浮提示文字
  </div>
</div>
.wrap {
  position: relative;
  display: inline-block;
  margin: 4em;
}

.tips {
  position: absolute;
  top: -2em;
  left: 50%;
  display: none;
  white-space: nowrap;
  transform: translate(-50%, 0);
}

.wrap:hover .tips {
  display: block;
}

效果圖

Way 2⃣️

content: attr():獲取自定義標籤的內容。

<div class="tips-demo" data-tips="提示文本">演示文本</div>
.tips-demo {
  position: relative;
}

.tips-demo:after {
  content: attr(data-tips);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  white-space: nowrap;
  opacity: 0;
  transform: translateY(-150%);
  transition: .1s;
}

.tips-demo:hover:after {
  opacity: 1;
  transform: translateY(-100%);
}

效果圖

Way 3⃣️

<div class="haveTips" tips='hello world'>Hi</div>
div.haveTips {
  width: 150px;
  text-align: center;
  background-color: #F7F5F1;
  color: #BBA985;
  font-family: sans-serif;
  padding: 1em;
  border-radius: 10px;
  position: relative;
  cursor: pointer;
}

div.haveTips::after {
  content: attr(tips);
  padding: 5px;
  background-color: #B4A078;
  box-shadow: 0 0 5px #B4A078;
  color: white;
  min-width: 5em;
  max-width: 100%;
  text-align: center;
  border-radius: 5px;

  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translate(-50%, -10px);

  opacity: 0;
  transition: all 0.5s;
}

div.haveTips:hover::after {
  opacity: 1;
}

div.haveTips::before {
  content: '';

  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-top-color: #B4A078;

  position: absolute;
  top: -10px;
  left: calc(50% - 6px);

  opacity: 0;
  transition: all 0.5s;
}

div.haveTips:hover::before {
  opacity: 1;
}

效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章