js 获取 鼠标位置 和获取元素位置

    var body = document.getElementsByTagName("body")[0];
    body.addEventListener("mousemove", outpostion);
    function outpostion() {
        console.log("全局"+event.clientX + "/" + event.clientY); //evevt是全局的, 
    }
        var that = obj;
        //obj.offsetLeft //元素相对于父元素的left
        //obj.offsetTop //元素相对于父元素的top
        console.log(obj.offsetLeft + "/" + obj.offsetTop);

 

 //鼠标指针移动到元素上时触发
    function overShow(obj, str) {
        var showDiv = document.getElementById('showDiv');
        //showDiv.css("left", event.clientX).css("top", event.clientY);
        //showDiv.style.left = event.clientX;
        //showDiv.style.top = event.clientY;

        var that = obj;
        //obj.offsetLeft //元素相对于父元素的left
        //obj.offsetTop //元素相对于父元素的top

        console.log(obj.offsetLeft + "/" + obj.offsetTop);
        showDiv.style.left = (obj.offsetLeft * 2) + 'px';
        showDiv.style.top = obj.offsetTop + 'px';
        console.log("jubu" + event.clientX + "/" + event.clientY);
        showDiv.style.display = 'block';
        showDiv.innerHTML = str;
    }

 

posted @ 2018-12-08 09:47  enych  阅读(8163)  评论(0编辑  收藏  举报