CSS僞元素:after|::after實現提示效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>window.getComputedStyle</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: lightcoral;
            border: solid 1px #999; /*BFC原理去掉margin重疊*/
        }
        div[data-desc]::after {
            content: attr(data-desc);
            display: block;
            width: 50px;
            height: 50px;
            background-color: blue;
            margin: 50% auto;
        }
    </style>
</head>
<body>
    <div data-desc="">
        :after與::after基本上是一樣的,一個是CSS2的語法,一個是CSS3的語法
    </div>
    <script>
        var divEle = document.getElementsByTagName("div")[0];
        var divStyle = window.getComputedStyle(divEle, null); //null是啥
        divEle.setAttribute("data-desc", divStyle.width + ":" + divStyle.height);
        console.log(divStyle.width);

        //獲取僞元素的style
        var divAfterStyle = window.getComputedStyle(divEle, "after");
        console.log(divAfterStyle.width);

        //css:after和:before規則不是DOM的一部分,因此不能使用js的DOM方法進行獲取。
        addEvent(divEle, "mouseenter", function (e) {
            var e = e || window.event;
            // e.target.classList.add("hover");
            //e.target也拿不到 ::after 僞元素
            e.target.setAttribute("data-desc", divAfterStyle.width + ":" + divAfterStyle.height);
        });
        addEvent(divEle, "mouseout", function (e) {
            var e = e || window.event;
            // e.target.classList.remove("hover");
            e.target.setAttribute("data-desc", divStyle.width + ":" + divStyle.height);
        });

        function addEvent(dom, type, fn, useCapture) {
            if (document.addEventListener) { //所有主流瀏覽器,除了 IE 8 及更早 IE版本
                dom.addEventListener(type, fn, useCapture);
            } if (document.attachEvent) { // IE 8 及更早 IE 版本
                dom.attachEvent("on" + type, fn);
            } else {
                dom["on" + type] = fn;
            }
        }

        function removeEvent(dom, type, fn, useCapture) {
            if (document.removeEventListener) { //所有主流瀏覽器,除了 IE 8 及更早 IE版本
                dom.removeEventListener(type, fn, useCapture);
            } if (document.detachEvent) { // IE 8 及更早 IE 版本
                dom.detachEvent("on" + type, fn);
            } else {
                dom["on" + type] = null;
            }
        }
    </script>

    <p>這是上面代碼的實現<br />
        我們有一些 <span data-descr="collection of words and punctuation">文字</span> 有一些
        <span data-descr="small popups which also hide again">提示</span><br />
        把鼠標放上去<span data-descr="not to be taken literally">看看</span>.
    </p>
</body>
<style>
    span[data-descr] {
        position: relative;
        text-decoration: underline;
        color: #00F;
        cursor: help;
    }

    span[data-descr]:hover::after {
        content: attr(data-descr);
        position: absolute;
        left: 0;
        top: 24px;
        min-width: 200px;
        border: 1px #aaaaaa solid;
        border-radius: 10px;
        background-color: #ffffcc;
        padding: 12px;
        color: #000000;
        font-size: 14px;
        z-index: 1;
    }
</style>
</html>

在這裏插入圖片描述

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