美文网首页
可以在屏幕上移动的悬浮窗

可以在屏幕上移动的悬浮窗

作者: 小睿同学 | 来源:发表于2019-03-04 11:22 被阅读0次

悬浮窗上有按钮,可以关掉悬浮窗

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        *{
            margin: 0px;
            padding: 0px;
        }

        #c{
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: yellow;
            left: 100px;
            top: 100px;
        }
    </style>
</head>
<body>
<div id="c">
<button id="close">close</button>
</div>
<script>
    window.onload=function() {
        let dx = 1;
        let dy = 1;
        let fd;
        let m = document.getElementById("c");
        function fn() {
            fd=setInterval(piaofu,30)
        }
        function piaofu() {
            let left = Number.parseInt(window.getComputedStyle(m, null).left);
            let right = Number.parseInt(window.getComputedStyle(m, null).right);
            let top = Number.parseInt(window.getComputedStyle(m, null).top);
            let buttom = Number.parseInt(window.getComputedStyle(m, null).bottom);
            if (left <= 0 || right <= 0) {
                dx = -dx;
            }
            if (top <= 0 || buttom <= 0) {
                dy = -dy;
            }
            m.style.left = (left + dx) + "px";
            m.style.top = (top + dy) + "px";
        }
        fn();
        m.onmouseover=function () {
            clearInterval(fd);
        }
        m.onmouseout=fn;
        document.getElementById("close").onclick=function () {
            m.style.display="none";
        }
    }
</script>
</body>
</html>

相关文章

网友评论

      本文标题:可以在屏幕上移动的悬浮窗

      本文链接:https://www.haomeiwen.com/subject/mhmbuqtx.html