Js浮动页面顶部或鼠标点击处弹出提示层

QQ邮箱在删除邮件时,那个浮动提示层很方便。

为此,我写了一个鼠标移动时修改坐标,这样随时可以弹出该位置的提示层的方法。

改进了一下,如果刚打开页面也要浮动提示,这时候也需要坐标,鼠标位置坐标按照顶层坐标给。

如下:

var globalObj = {
    timer: {},
    mouse_pos: {
        x: 0,
        y: 0
    },
    top_pos: {
        x: 0,
        y: 0
    },
    getMousePosition: function(ev) {
        ev = ev || window.event;
        if (ev.pageX || ev.pageY) {
            return {
                x: ev.pageX,
                y: ev.pageY
            };
        }
        if (!ev.clientX) return this.getTopPosition();
        return {
            x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y: ev.clientY + document.body.scrollTop - document.body.clientTop
        };
    },
    getTopPosition: function() {
        return {
            x: document.body.scrollLeft + document.body.clientWidth / 2,
            y: document.body.scrollTop - document.body.clientTop
        };
    },
    GetPoint: function(ev) {
        var p = this.getMousePosition(ev);
        this.mouse_pos.x = p.x;
        this.mouse_pos.y = p.y;
        p = this.getTopPosition();
        this.top_pos.x = p.x;
        this.top_pos.y = p.y;
    },
    showHtmlTipAboveMouse: function(html_tip, time, id) {
        var pos = {
            x: this.mouse_pos.x,
            y: this.mouse_pos.y - 30
        };
        if (pos.y < 0) pos.y = 0;
        if (pos.x < 0) pos.x = 0;
        if (!id) id = 'showHtmlTipAboveMouse';
        this.showHtmlTip(html_tip, pos, time, id);
    },
    showHtmlTipOnTop: function(html_tip, time, id) {
        var pos = {
            x: this.top_pos.x,
            y: this.top_pos.y
        };
        if (!id) id = 'showHtmlTipOnTop';
        this.showHtmlTip(html_tip, pos, time, id);
    },
    showHtmlTip: function(html_tip, pos, time, id) {
        if (!id) id = 'showHtmlTip';
        $('#' + id).remove();
        if (!time) time = 1;
        else time = parseInt(time);
        var dom = $('<div id="'+id+'"></div>').html(html_tip).css({
            'height': '20px',
            'line-height': '20px',
            'font-size': '12px',
            'padding': '2px 10px',
            'border': 'solid 1px #A8FF24',
            'position': 'absolute',
            'background': '#00A600',
            'margin': '0px',
            'display': 'none',
            'color': '#fff',
            'opacity': '1.0'
        });

        $('body').append(dom);
        var left = pos.x - dom.width() / 2;
        var top = pos.y;
        dom.css({
            display: '',
            top: top + 'px',
            left: left + 'px'
        });
        if (this.timer[id]) {
            clearTimeout(this.timer[id]);
            this.timer[id] = null;
        }
        this.timer[id] = setTimeout(function() {
            dom.fadeOut();
        }, (time * 1000));
    }
}


$(function() {
    globalObj.GetPoint();
    globalObj.showHtmlTipAboveMouse('Mouse浮动提示层---------------------------------------------');
    globalObj.showHtmlTipOnTop('Top浮动提示层');
    $(document).mousemove(function(ev) {
        setTimeout(function() {
            globalObj.GetPoint(ev);
        }, 200);
    });
    $('#main_div').click(function() {
        globalObj.showHtmlTipAboveMouse('Mouse浮动提示层');
        globalObj.showHtmlTipOnTop('Top浮动提示层');
    });
});

 附件中就是代码。

需要jquey的支持。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 JS 实现浮动窗口的详细步骤: 1. HTML 结构 首先,我们需要在 HTML 中添加一个按钮,用于触发浮动窗口的事件。同时,为了方便后续操作,我们需要在 HTML 中添加一个空的 div 元素,用于存放浮动窗口的内容。 ```html <button id="open-btn">打开浮动窗口</button> <div id="float-window"></div> ``` 2. CSS 样式 接下来,我们需要为浮动窗口添加样式。这里我们使用绝对定位来实现浮动效果,同时添加一些基本的样式,如背景颜色、边框样式等。 ```css #float-window { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; height: 300px; background-color: #fff; border: 1px solid #ccc; display: none; /* 默认隐藏 */ } ``` 3. JS 代码 最后,我们需要使用 JS 代码来实现浮动窗口的功能。具体步骤如下: 1. 获取按钮元素和浮动窗口元素,并添加事件监听器。 ```javascript var openBtn = document.getElementById('open-btn'); var floatWindow = document.getElementById('float-window'); openBtn.addEventListener('click', function() { floatWindow.style.display = 'block'; }); ``` 2. 实现拖拽效果。 ```javascript var isDragging = false; // 是否正在拖动 var mouseOffset = { x: 0, y: 0 }; // 鼠标相对于浮动窗口的偏移量 floatWindow.addEventListener('mousedown', function(e) { isDragging = true; mouseOffset.x = e.pageX - floatWindow.offsetLeft; mouseOffset.y = e.pageY - floatWindow.offsetTop; }); document.addEventListener('mousemove', function(e) { if (isDragging) { floatWindow.style.left = e.pageX - mouseOffset.x + 'px'; floatWindow.style.top = e.pageY - mouseOffset.y + 'px'; } }); document.addEventListener('mouseup', function() { isDragging = false; }); ``` 3. 实现关闭效果。 ```javascript var closeBtn = document.createElement('div'); closeBtn.innerHTML = '关闭'; closeBtn.style.position = 'absolute'; closeBtn.style.top = '10px'; closeBtn.style.right = '10px'; floatWindow.appendChild(closeBtn); closeBtn.addEventListener('click', function() { floatWindow.style.display = 'none'; }); ``` 最终的 JS 代码如下: ```javascript var openBtn = document.getElementById('open-btn'); var floatWindow = document.getElementById('float-window'); openBtn.addEventListener('click', function() { floatWindow.style.display = 'block'; }); var isDragging = false; // 是否正在拖动 var mouseOffset = { x: 0, y: 0 }; // 鼠标相对于浮动窗口的偏移量 floatWindow.addEventListener('mousedown', function(e) { isDragging = true; mouseOffset.x = e.pageX - floatWindow.offsetLeft; mouseOffset.y = e.pageY - floatWindow.offsetTop; }); document.addEventListener('mousemove', function(e) { if (isDragging) { floatWindow.style.left = e.pageX - mouseOffset.x + 'px'; floatWindow.style.top = e.pageY - mouseOffset.y + 'px'; } }); document.addEventListener('mouseup', function() { isDragging = false; }); var closeBtn = document.createElement('div'); closeBtn.innerHTML = '关闭'; closeBtn.style.position = 'absolute'; closeBtn.style.top = '10px'; closeBtn.style.right = '10px'; floatWindow.appendChild(closeBtn); closeBtn.addEventListener('click', function() { floatWindow.style.display = 'none'; }); ``` 这样,我们就完成了使用 JS 实现浮动窗口的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值