小提琴:http: //jsfiddle.net/ue7pq/
和等效的 jQuery 小提琴:
html:
<div class="project-box">
<img src="img/ifix.jpg" class="thumbnail img-responsive">
<div class="hover-box">
<h2>TITLE</h2>
<p>Description of logos that we've made</p>
</div>
</div>
js:
window.onload = function(){
var project = document.getElementsByClassName('project-box');
img = project[0].getElementsByTagName('img');
box = document.querySelectorAll('div.project-box div.hover-box');
imgWidth = img[0].offsetWidth;
imgHeight = img[0].offsetHeight;
console.log(imgWidth);
console.log(imgHeight);
box.style.width = imgWidth+'px';
box.style.height = imgHeight+'px';
};
我正在尝试使悬停框与 img 具有相同的宽度和高度。我试图将其保留为纯 javascript 我犯了先学习 jQuery 而不是 JavaScript 的错误,所以现在我试图自学 JavaScript 有一些问题。控制台日志说悬停框未定义
console.log 中的错误消息:
Uncaught ReferenceError: hoverBox is not defined main.js: 4
350
180
Uncaught TypeError: Cannot set property 'width' of undefined