tooltip實現(xiàn)

生成tooltip完整流程:觸發(fā)顯示事件 -> 創(chuàng)建tooltip -> 調(diào)整tooltip位置 -> 添加動畫 -> 顯示tooltip -> 觸發(fā)隱藏事件 -> 隱藏tooltip -> 刪除tooltip

獲取位置:top left

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
    <style type="text/css"> 
    </style>
</head>
<body>
    <div id="outer"  style="width:500px;height: 500px;background-color: blue;margin: 50px auto;text-align: center;">
        <div id="div1" style="width:300px;height: 300px;background-color: red;margin: 100px;float: left;"><p id="show-message"></p></div>
    </div>
<script type="text/javascript">
    $("#div1").on('click',function(){
        var $element = $("#div1");
        var text = "click";
        $("#show-message").html(text);
        var offset = $element.offset();
        console.log(offset);
        var bound = $element.get(0).getBoundingClientRect();
        console.log(bound);
    })
</script>
</body>
</html>

根據(jù)彈出框大小,計算出顯示位置的top,left

1、bottom時
    1.1、top為定位元素(pos)的top+ 定位元素(pos)的高度
    1.2、left為定位元素(pos)的Left – 定位元素(pos)的寬度/2 – tooltip元素寬度/2
2、top時
    2.1、top為定位元素(pos) 的top-tooltip元素的高度
    2.2、left為定位元素(pos)的left – 定位元素(pos)的寬度/2 – tooltip元素寬度/2
3、left時
    3.1、top為定位元素(pos)的top – 定位元素(pos)的高度/2 – tooltip元素高度/2
    3.2、left為定位元素(pos)的left – tooltip元素的寬度
4、right時
    4.1、top為定位元素(pos)的top – 定位元素(pos)高度/2 – tooltip元素高度/2
    4.2、left為定位元素(pos)的left + 定位元素(pos)寬度
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容