在編輯大量專業(yè)的知識時我們往往需要對一些比較生僻的詞匯進行解釋或是對一些引用內容進行介紹,如果直接在文本中寫就的話,就很容易打亂文本整體的連貫性,這一節(jié)的wordpress開發(fā)就教大家如何實現(xiàn)簡單的Tooltip提示框。
- 代碼來源:詳情

效果如下:
在 WordPress 的文章編輯器登鸛雀樓里面只要輸入白日依山盡,黃河入海流。欲窮千里目,更上一層樓。如下格式的短代碼
[tooltip tip="提示內容"]
代碼部署:
在主題根目錄下的 functions.php
文件中的<?php
下面添加以下代碼:
// [tooltip tip=""]
add_shortcode('tooltip', 'shortcode_tooltip');
function shortcode_tooltip($attrs, $content = null) {
$return = '';
extract(shortcode_atts(array(
'tip' => "",
), $attrs));
ob_start(); ?>
<span class="tooltip"><span class="tooltip-icon">?</span><span class="tip-content"><span class="tip-content-inner"><?php echo $tip; ?></span></span></span>
<?php
$return = ob_get_clean();
return $return;
}
在主題根目錄下的style.css底部添加以下樣式代碼:
.tooltip{
position: relative;
display: inline-block;
margin-left: 5px;
margin-right: 5px;
height: 16px;
line-height: 16px;
vertical-align: middle;
}
.tooltip-icon{
display: block;
width: 14px;
height: 14px;
line-height: 14px;
border: 1px solid #999;
border-radius: 50%;
font-size: 12px;
font-weight: 700;
font-family: "caption", Arial;
text-align: center;
}
.tip-content{
z-index: 999999;
display: none;
position: absolute;
left: -5px;
bottom: 30px;
width: 240px;
}
.tip-content-inner{
position: absolute;
bottom: 0;
left: 0;
display: block;
width: auto;
max-width: 200px;
padding: 10px;
line-height: 20px;
border: 1px solid #ccc;
background: #fff;
line-height: 20px;
color: #333;
font-size: 16px;
}
.tip-content-inner:before{
content: "";
position: absolute;
left: 7px;
bottom: -24px;
border-style: solid solid solid solid;
border-color: #ccc transparent transparent transparent;
border-width: 12px 6px;
}
.tip-content-inner:after{
content: "";
position: absolute;
left: 8px;
bottom: -20px;
border-style: solid solid solid solid;
border-color: #fff transparent transparent transparent;
border-width: 10px 5px;
}
.tooltip:hover > .tip-content{
display: block;
}
使用:
在文章編輯器里面只要輸入如下格式的短代碼:
[tooltip tip=”提示內容”]
提示:
– PS0: 那個圓圈是用 css3 實現(xiàn)的,所以 IE8 下面會變成方框……需要支持 IE8 的朋友自己改成背景圖方式吧。
– PS1: tip 內容用了 2 個容器的目的是為了讓 tip 內容顯示能 width:auto 效果,也就是說 .tip-content 的 width 起到 max-width 效果,然后 .tip-content-inner 就有了類似 max-width 的屬性效果了
相關:
擴展:
如果你擔心自己的原創(chuàng)內容被被輕易的復制,那么在被人復制時加一個提示會是一個不錯的選擇,參考這一篇wordpres開發(fā)教程來增添這一小功能吧。