1. 程式人生 > >CSS3滑鼠放上按鈕懸浮框提示效果

CSS3滑鼠放上按鈕懸浮框提示效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        body {
            text-align: center;
        }

        p {
            margin
: 100px
; }
.tip { display: inline-block; position: relative; } .tip:before, .tip:after { opacity: 0; /*透明度為完全透明*/ position: absolute; z-index: 1000; /*設為最上層*/ /*滑鼠放上元素上時的動畫,滑鼠放上後效果在.tip-*:hover:before, .tip-*:hover:after中設定; 0.3s:規定完成過渡效果需要多少秒或毫秒,ease:規定慢速開始,然後變快,然後慢速結束的過渡效果*/
transition: 0.3s ease; -webkit-transition: 0.3s ease; -moz-transition: 0.3s ease; }
.tip:before { content: ''; border: 6px solid transparent; } .tip:after { content: attr(data-tip); /*後去要提示的文字*/ padding
: 5px
; white-space: nowrap; /*強制不換行*/ background-color: #000000; color: #ffffff; }
.tip:hover:before, .tip:hover:after { opacity: 1; /*滑鼠放上時透明度為完全顯示*/ z-index: 1000; } /*top*/ .tip-top:before { bottom: 100%; left: 50%; border-top-color: rgba(0, 0, 0, 0.8); /*小三角效果*/ margin-left: -3px; margin-bottom: -12px; } .tip-top:after { bottom: 100%; left: 50%; margin-left: -6px; } .tip-top:hover:before { margin-bottom: -6px; } .tip-top:hover:after { margin-bottom: 6px; } /*bottom*/ .tip-bottom:before { top: 100%; left: 50%; border-bottom-color: rgba(0, 0, 0, 0.8); margin-left: -3px; margin-top: -9px; } .tip-bottom:after { top: 100%; left: 50%; margin-left: -6px; margin-top: 3px; } .tip-bottom:hover:before { margin-top: -3px; } .tip-bottom:hover:after { margin-top: 9px; } /*right*/ .tip-right:before { top: 50%; left: 100%; border-right-color: rgba(0, 0, 0, 0.8); margin-left: -9px; margin-top: -3px; } .tip-right:after { top: 50%; left: 100%; margin-left: 3px; margin-top: -6px; } .tip-right:hover:before { margin-left: -3px; } .tip-right:hover:after { margin-left: 9px; } /*left*/ .tip-left:before { top: 50%; left: 0%; border-left-color: rgba(0, 0, 0, 0.8); margin-left: 0px; margin-top: -3px; } .tip-left:after { top: 50%; right: 100%; margin-right: 0px; margin-top: -6px; } .tip-left:hover:before { margin-left: -6px; } .tip-left:hover:after { margin-right: 6px; }
</style> </head> <body> <p> <button class="tip tip-top" data-tip="我是上邊提示">上邊提示</button> </p> <p> <button class="tip tip-bottom" data-tip="我是下邊提示">下邊提示</button> </p> <p> <button class="tip tip-right" data-tip="我是右邊提示">右邊提示</button> </p> <p> <button class="tip tip-left" data-tip="我是左邊提示">左邊提示</button> </p> </body> </html>