简体   繁体   English

getBoundingClientRect() 返回 0,我做错了什么?

[英]getBoundingClientRect() is returning 0's, what am i doing wrong?

I'm trying to make an <img> element move to another element ( <a> ) by first getting the position with getBoundingClientRect() and then to use those values to set the <img> element position on mouseover.我试图通过首先使用getBoundingClientRect()获取 position 然后使用这些值在鼠标悬停时设置<img>元素 position 来使<img>元素移动到另一个元素( <a> )。 But when I check the logs it's just spitting 0's.但是当我检查日志时,它只是吐出 0。 What am I doing wrong / overlooking?我做错了什么/忽略了什么?

 var clickers = document.querySelectorAll('.clicker'); var imgPreviewEle = document.getElementById('artistPreview'); clickers.forEach(function(clicker) { clicker.addEventListener('mouseover', function hover() { var elem = clicker.getBoundingClientRect(); console.log(elem); imgPreviewEle.style.display = "block"; imgPreviewEle.style.left = elem.left + 300 + 'px'; imgPreviewEle.style.top = elem.top - 300 + 'px'; }); }); clickers.forEach(function(clicker) { clicker.addEventListener('mouseleave', function hover() { var elem = clicker.getBoundingClientRect(); console.log(elem); imgPreviewEle.style.display = "none"; }); });
 .clicker { width: 100%; border-top: 2px solid transparent; border-bottom: 2px solid rgba(18.4, 31, 31, .5); } #artistPreview { z-index: 1010; position: fixed; clip: rect(290px, 600px, 620px, 0px); }
 <img id="artistPreview" src="" /> <div class="container"> <ul class="dropdown"> <li onMouseOver="show('A')" onMouseOut="hide('A')"> <a href="javascript:void(0)" style="font-weight: 600;">A</a> <ul id="A"> <a class="clicker" href="javascript:void(0)">ABBA</a> </ul> </li> </ul> </div>

I made some changes and left some comments.我做了一些更改并留下了一些评论。 Let me know if you need more info:如果您需要更多信息,请告诉我:

 var clickers = document.querySelectorAll('.clicker'); var imgPreviewEle = document.getElementById('artistPreview'); // Hide the image from the start imgPreviewEle.style.display = "none"; clickers.forEach(function(clicker) { clicker.addEventListener('mouseover', function hover() { var elem = clicker.getBoundingClientRect(); imgPreviewEle.style.display = "block"; // Make sure the image appears nearby // Looks like the -300px you had here pushed the image off screen. imgPreviewEle.style.left = (elem.left + elem.width) + 'px'; imgPreviewEle.style.top = (elem.top + elem.height) + 'px'; console.log(elem); }); }); clickers.forEach(function(clicker) { clicker.addEventListener('mouseleave', function hover() { var elem = clicker.getBoundingClientRect(); console.log(elem); imgPreviewEle.style.display = "none"; }); });
 .clicker { width: 100%; border-top: 2px solid transparent; border-bottom: 2px solid rgba(18.4, 31, 31, .5); } #artistPreview { z-index: 1010; position: fixed; /* The image should not cover the mouseover field. */ pointer-events: none; }
 <. The image will need an SRC to be visible: > <img id="artistPreview" src="http.//placekitten.com/g/80/80" /> <div class="container"> <ul class="dropdown"> <: Removed some event handler refs that did not have definitions in code: > <li> <a href="javascript;void(0)" style="font-weight: 600;">A</a> <ul id="A"> <a class="clicker" href="javascript:void(0)">ABBA</a> </ul> </li> </ul> </div>

I am having similar problems.我有类似的问题。 I cannot identify what is causing the problem.我无法确定导致问题的原因。 I actually get values when i use the body but any other element inside of the body shows 0当我使用身体时,我实际上得到了值,但身体内的任何其他元素都显示为 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM