获取鼠标当前元素

var   last_element=null; 
 track_mouse = (e) => {
        const get_current_element = (e) => {
            let x = e.clientX, y = e.clientY;
            return document.elementFromPoint(x, y);
        }
        const highlight = (element) => {
            element.style.outline = '#f00 solid 2px'
        }
        const remove_height_light = (element) => {
            element.style.removeProperty('outline')
        }
        let elementMouseIsOver = get_current_element(e)
        if (elementMouseIsOver === last_element) {
            //当鼠标在元素内部移动的时候
            return
        }
        if (last_element != null) {
            remove_height_light(last_element)
        }
        highlight(elementMouseIsOver)
        last_element = elementMouseIsOver
    }
 window.addEventListener("mousemove",track_mouse,true)

posted @ 2020-10-14 12:00  公众号python学习开发  阅读(227)  评论(0编辑  收藏  举报