【JS/TS鼠标气泡跟随】文本提示 / 操作提示

适用于任何类型项目:vue、react、angular、js、ts、jsp、jquery


1、功能封装:

TypeScript 复制代码
export function useMouseActionTip(text: string, parentEl: HTMLElement, offset?: XY) {
    function mousemove(e: MouseEvent) {
        const offsetX = offset?.x || 16;
        const offsetY = offset?.y || 16;
        cursorEl.style.left = e.pageX + offsetX + "px";
        cursorEl.style.top = e.pageY + offsetY + "px";
    }
    function mouseenter() {
        cursorEl.style.display = "block";
    }
    function mouseout() {
        cursorEl.style.display = "none";
    }
    const cursorEl = document.createElement("div");
    cursorEl.className = "_mouse_action_tip";
    cursorEl.innerHTML = text;
    cursorEl.style.display = "block";

    setStyle();
    document.body.appendChild(cursorEl);
    document.addEventListener("mousemove", mousemove);
    parentEl.addEventListener("mouseenter", mouseenter);
    parentEl.addEventListener("mouseout", mouseout);

    return {
        destroy() {
            document.removeEventListener("mousemove", mousemove);
            setStyle();
            parentEl.removeEventListener("mouseenter", mouseenter);
            parentEl.removeEventListener("mouseout", mouseout);
            document.body.removeChild(cursorEl);
        },
    };
}
export type UseMouseActionTip = ReturnType<typeof useMouseActionTip>;
let hasStyle = false;
function setStyle() {
    if (hasStyle) {
        return;
    }
    hasStyle = true;
    const styleElement = document.createElement("style");
    styleElement.textContent = `
    ._mouse_action_tip {
        position: fixed;
        display: flex;
        padding: 5px 10px;
        background-color: #33383e;
        color: #e8eaec;
        border-radius: 4px;
        pointer-events: none;
        z-index: 9999;
        box-shadow: rgba(255, 255, 255, 0.3) 0px 0px 3px, rgba(255, 255, 255, 0.3) 0px 0px 3px;
        font-size: 14px;
      }
    `;
    document.head.appendChild(styleElement);
}

2、使用:

TypeScript 复制代码
let mouseTip: UseMouseActionTip | undefined;
watch(
    () => editorStore.currentSubTool,
    () => {
            if (editorStore.currentSubTool === SubTools.Measure) {
                mouseTip = useMouseActionTip(
                    t("Editor.Tip.Hold_down_and_drag_the_mouse"),
                    editorStore.canvas().canvas.upperCanvasEl
                );
            } else {
                mouseTip?.destroy();
                mouseTip = undefined;
            }
    }
);
相关推荐
Cache技术分享7 分钟前
162. Java Lambda 表达式 - Consumer 的链式组合
前端·后端
是晓晓吖11 分钟前
为什么在Tab中取不到content.js给window设置的变量/函数?
前端·chrome
日月晨曦14 分钟前
JS闭包:变量的"守护者"与"储物间"
前端·javascript
袁煦丞15 分钟前
轻量级网络大佬Nginx打开公网自由之路:cpolar内网穿透实验室第625个成功挑战
前端·程序员·远程工作
日月晨曦15 分钟前
TypeScript:让JavaScript穿上西装革履
前端·typescript
cvpv16 分钟前
优雅!太优雅!斯巴拉西!怎么让AI写出最优雅的代码
前端·typescript·trae
遗悲风19 分钟前
html抽奖功能
前端·html
PineappleCoder30 分钟前
为什么说发布 - 订阅是代码的 “万能胶水”?解耦逻辑全解析
前端·javascript·算法
我叫黑大帅34 分钟前
微信小程序分包:告别加载慢,像拆快递一样简单!
前端·微信小程序
今禾38 分钟前
深入解析HTTP协议:从OSI模型到HTTP/3.0的演进与实战优化
前端·http·面试