//Por tanto, deberías usar document.documentElement en lugar de document.firstChild para obtener el //elemento raíz. var box1=document.getElementById("box1"); document.addEventListener("mousemove", function(e){ var st=document.documentElement.scrollTop; //console.log(st); // Obtener las coordenadas del mouse var left= e.clientX; var top= e.clientY; box1.style.left=left+st+"px"; box1.style.top=top+st+"px"; }); }; // <box id="box1"></box> /* #box1{ width: 100px; height: 100px; background-color: blueviolet; position: absolute; } */
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter