js achieve pop-up box following the mouse

Is a new day surfing the web, see this effect in the bing search page:

That pop-up box and move with the movement of the mouse. Thinking about is:

  • Onmousemove function call, the current location of the mouse gives pop-up box to

    //html
    <div id="alert">
        <div class="img">
            <img src="./img/trees-576751_640.png" alt="">
        </div>
        Look At Me
    </div>
    
    <div id="clickMe">Touch Me</div>
    /*css*/
    #clickMe {
        width: 100px;
        height: 40px;
        margin: 100px auto;
        cursor: pointer;
        background-color: brown;
        color: white;
        text-align: center;
        line-height: 40px;
        border-radius: 50%;
    }
    
    #alert {
        width: 200px;
        height: 200px;
        text-align: center;
        background-color: tomato;
        font-size: 20px;
        position: absolute;
        display: none;
    }
    .img{
        width: 80%;
        height: 35%;
        margin: 20% auto;
        border: 2px dotted #ffffff;
    }
    .img img{
        width: 100%;
        height: 100%;
    }
    //javascript
    var click = document.getElementById("clickMe")
    var alert = document.getElementById("alert")
    
    click.onmousemove = function (e) {
        alert.style.left = e.clientX + 20 + "px"
        alert.style.top = e.clientY + 10 + "px"
        alert.style.display = "block";
    }
    
    click.onmouseout = function (e) {
        alert.style.display = "none";
    }

    Results are as follows:

Guess you like

Origin www.cnblogs.com/dairyDeng/p/12014940.html