不同樣式tooltip對話框小三角的css實現
這篇文章總結的非常好全面,轉帖於此,感謝原文作者。
開發過程中已經遇到過好多次需要手動實現 tooltip
樣式了,這裏就總結下,以便未來更好的複製粘貼。
如上圖,常見的 tooltip
總共有 3
種類型,純背景色無邊框、有邊框、包含背景圖,其中的小三角可能是純色、尖尖有弧度。
下邊介紹四種常用的方法來實現 tooltip
。
貼圖
簡單方便快捷,一個三角形的圖定下位即可。在女朋友的幫助下,用 AI
成功畫了三種三角形。
下邊我們只需要把三角形貼到矩形下邊即可。
-
純背景色三角形
-
.wxml
-
<view class="tooltip">
-
<view class="tooltip-text">我是一句提示內容</view>
-
<image class="tooltip-triangle" src="https://windliangblog.oss-cn-beijing.aliyuncs.com/tooltip-01.png" />
-
</view>
-
-
.wxss
-
.tooltip {
-
width: 400rpx;
-
margin: 100rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
height: 60rpx;
-
line-height: 60rpx;
-
background: #F5F8FF;
-
color: #494949;
-
border-radius: 5rpx;
-
padding: 0 20rpx;
-
}
-
-
.tooltip-triangle {
-
position: absolute;
-
width: 30rpx;
-
height: 30rpx;
-
top: 60rpx;
-
left: 200rpx;
-
}
-
-
帶邊框的三角形和三角形圓角
-
.wxml
-
<view class="tooltip">
-
<view class="tooltip-text">我是一句提示內容</view>
-
<image class="tooltip-triangle" src="https://windliangblog.oss-cn-beijing.aliyuncs.com/tooltip-02.png" />
-
</view>
-
-
.wxss
-
.tooltip {
-
width: 400rpx;
-
margin: 100rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
height: 60rpx;
-
line-height: 60rpx;
-
background: #f5f8ff;
-
color: #494949;
-
border-radius: 5rpx;
-
padding: 0 20rpx;
-
border: 2px solid #002fa7;
-
}
-
-
.tooltip-triangle {
-
position: absolute;
-
width: 30rpx;
-
height: 30rpx;
-
top: 62rpx;
-
left: 200rpx;
-
}
發現原有的
border
沒有蓋住,然後從重新做了一張上邊延伸背景色的圖。圓角的三角同理,換下
image
的src
即可。 -
利用 border
不知道是誰第一個想到的這種方案,是真的很神奇。我們經常寫 border
,可有沒有想過它的四個角的連接處是什麼樣的?
讓我們將連接處放大:
會發現每條邊其實是一個梯形,然後互相接起來。那麼如果 border
中內容的寬高都是 0
會怎麼樣呢?
-
.border {
-
border-width: 4px;
-
border-color: #F00 #0F0 #00F #0FF;
-
border-style: solid;
-
width: 0px;
-
height: 0px;
-
}
三角形出現了!我們只需要將左邊下邊右邊的 border
顏色設置爲透明就是我們要的三角形了,border-color: #F00 transparent transparent transparent;
此外,雖然底部 boder
設置爲透明瞭,但是還佔據高度,我們可以將它的 width
設爲 0
:border-bottom-width: 0
。
然後我們只需要將之前使用的圖片替換掉即可。
-
.wxml
-
<view class="tooltip">
-
<view class="tooltip-text">我是一句提示內容</view>
-
<view class="tooltip-triangle"></view>
-
</view>
-
-
.wxss
-
.tooltip {
-
max-width: 400rpx;
-
margin-left: 20rpx;
-
position: relative;
-
}
-
.tooltip-text {
-
padding: 15rpx;
-
background: #002FA7;
-
color: #fff;
-
border-radius: 5rpx;
-
}
-
-
.tooltip-triangle {
-
position: absolute;
-
top: 62rpx;
-
left: 200rpx;
-
border-width: 30rpx;
-
border-color: #002FA7 transparent transparent transparent;
-
border-style: solid;
-
width: 0px;
-
height: 0px;
-
}
效果如下:
三角形形狀的話,我們可以通過 border-width
屬性去調整高低胖瘦。
-
帶邊框三角
上邊的矩形和三角形都沒有邊框,如果是有邊框的,下邊這種該怎麼實現呢?
其實很簡單,我們只需要在原有三角形的位置寫一個一樣的三角形,然後顏色設置爲對話框的背景色,向上偏移一定位置即可。
把覆蓋的三角形顏色設置爲紅色,這樣看起來就很明顯了,如下圖:
代碼如下:
-
.wxml
-
<view class="tooltip">
-
<view class="tooltip-text">我是一句提示內容</view>
-
<view class="tooltip-triangle-top"></view>
-
<view class="tooltip-triangle"></view>
-
</view>
-
-
.wxss
-
.tooltip {
-
max-width: 400rpx;
-
margin-left: 20rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
padding: 15rpx;
-
background: #fff;
-
border-radius: 5rpx;
-
border: 5rpx solid #002FA7;
-
}
-
-
.tooltip-triangle-top {
-
position: absolute;
-
top: 71rpx;
-
left: 200rpx;
-
border-width: 30rpx;
-
border-left-width: 20rpx;
-
border-right-width: 20rpx;
-
border-color: #FFF transparent transparent transparent;
-
border-style: solid;
-
width: 0px;
-
height: 0px;
-
z-index: 10;
-
}
-
-
.tooltip-triangle {
-
position: absolute;
-
top: 76rpx;
-
left: 200rpx;
-
border-width: 30rpx;
-
border-left-width: 20rpx;
-
border-right-width: 20rpx;
-
border-color: #002FA7 transparent transparent transparent;
-
border-style: solid;
-
width: 0px;
-
height: 0px;
-
}
矩形旋轉
只需要兩個同樣位置的矩形,然後旋轉上邊的矩形即可。旋轉過來的三角形的長邊就是原來矩形的長,三角形邊長比是1
比 1
比根號 2
。所以原有矩形的長寬比應該爲根號 2
比 1
。即,width = 1.41 * height
。
代碼的話,我們用僞元素矩形旋轉,另一個矩形設置 overflow:hidden
即可。
-
.wxml
-
<view class="tooltip">
-
<view class="tooltip-text">我是一句提示內容</view>
-
<view class="tooltip-triangle"></view>
-
</view>
-
-
.wxss
-
.tooltip {
-
max-width: 400rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
padding: 15rpx;
-
background: #002FA7;
-
border-radius: 5rpx;
-
color: #FFF;
-
}
-
-
-
.tooltip-triangle {
-
position: relative;
-
left: 150rpx;
-
width: calc(30rpx * 1.41);
-
height: 30rpx;
-
overflow: hidden;
-
}
-
-
-
.tooltip-triangle::before {
-
content: '';
-
width: 100%;
-
height: 100%;
-
background: #002FA7;
-
display: block;
-
transform: rotate(-45deg);
-
transform-origin: left top;
-
}
由於我們三角形是由矩形生成的,所以帶邊框的 tooltip
相對 border
的方法就容易多了。
我們只需要給僞元素設置邊框即可。
-
.wxss
-
.tooltip {
-
max-width: 400rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
padding: 15rpx;
-
background: #f5f8ff;
-
color: #494949;
-
border-radius: 5rpx;
-
border: 4rpx solid #002fa7;
-
}
-
-
-
.tooltip-triangle {
-
position: relative;
-
left: 150rpx;
-
width: calc(30rpx * 1.41);
-
height: 30rpx;
-
overflow: hidden;
-
}
-
-
-
.tooltip-triangle::before {
-
content: '';
-
border: 4rpx solid #002fa7;
-
background: #f5f8ff;
-
width: 100%;
-
height: 100%;
-
display: block;
-
transform: rotate(-45deg);
-
transform-origin: left top;
-
box-sizing: border-box;
-
border-radius: 8rpx;
-
}
此時出現了一個問題,上邊矩形的 border
露了出來。
這裏用一個 trick
的方法,我們在原有矩形上邊加一個 border
蓋住上邊矩形的邊框。
-
.wxss 添加下邊的屬性
-
.tooltip-triangle {
-
border-top: 4rpx solid #f5f8ff;
-
bottom: 8rpx;
-
}
此外,帶弧角的三角形,我們也只需要在僞元素矩形上設置圓角即可。
-
.wxss 添加下邊的屬性
-
.tooltip-triangle::before
-
border-radius: 8rpx;
-
}
clip-path
下邊這種 tooltip
類型,小三角延伸了背景圖片(背景圖片 url
可能不是固定的),上邊幾種方法都是無能爲力的。
此時就需要 clip-path
屬性了,我們可以在 clippy 快速生成我們需要的多邊形路徑。
https://bennettfeely.com/clippy/
polygon
就是畫多邊形,然後給定各個點的座標即可,代碼中各個顏色和圖片中的各個點是對應的。
然後我們把上邊的代碼複製過來即可。
-
.wxml
-
<view class="tooltip">
-
<image src="https://windliangblog.oss-cn-beijing.aliyuncs.com/meituan4.jpg" class="tooltip-text"></image>
-
</view>
-
-
.wxss
-
.tooltip {
-
max-width: 400rpx;
-
position: relative;
-
}
-
-
.tooltip-text {
-
width: 400rpx;
-
height: 200rpx;
-
overflow: hidden;
-
clip-path: polygon(0% 0%, 100% 0%, 100% 80%, 70% 80%, 63% 100%, 55% 80%, 1% 80%);
-
}
使用 clip-path
的話 border
和圓角就比較難搞了,因爲最下邊的邊其實是被截掉了。
在 Web
頁面中可以使用 SVG
來實現想要的效果,可以 參考這裏 的一個回答。
https://stackoverflow.com/questions/31854185/how-to-add-border-in-my-clip-path-polygon-css-style
但在小程序中我沒想到好方法,歡迎大家和我交流。
總
上邊主要介紹了貼圖、border
、矩形旋轉、clip-path
四種方法。日常開發中,border
方案基本滿足需要了,偷懶的話也可以直接找設計要圖。大家還用過其他方法嗎?