【发布时间】:2019-12-24 16:11:25
【问题描述】:
我正在尝试确保 javascript 显示屏幕上每个图像的图像标题。我似乎已经知道如何将标题转换为文本节点,但我不知道如何确保文本仅在图像在屏幕上时显示,而在图像离开屏幕时消失。
请仅使用纯 JavaScript。 谢谢。
ri.addEventListener("scroll", function iT() {
const iA = Array.from(document.querySelectorAll("#right img"));
aP = document.querySelector(".aboutProject");
iA.forEach(function(item) {
const iT = item.title;
const iP = item.getBoundingClientRect().top;
const wH = window.innerHeight / 1.2;
if (iP < wH && iP >= 0) {
const h = document.createElement("H4");
const t = document.createTextNode(iT);
h.appendChild(t);
aP.appendChild(h);
}
});
});
* {
padding: 0;
margin: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
body {
background-color: rgb(254, 238, 228);
max-width: 100vw;
min-height: auto;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
}
.aboutProject {
display: grid;
align-content: center;
justify-items: center;
}
#right {
width: 50vw;
height: 100vh;
display: grid;
align-items: center;
justify-content: center;
overflow-x: hidden;
overflow-y: auto;
}
#right a img {
height: 92.5vh;
width: 50vw;
object-fit: cover;
object-position: center;
}
<div class="container">
<div class="aboutProject">
<h4>title comes here</h4>
</div>
<div id="right">
<a href="project6" id="lastClone"><img src="https://www.metoffice.gov.uk/binaries/content/gallery/metofficegovuk/hero-images/weather/cloud/cumulus-cloud.jpg" title="project 6clone" /></a>
<a href="project1"><img src="https://cdn1.epicgames.com/ue/product/Screenshot/StoreUltraDynamicSkyscreenshot1-1920x1080-f5d2cdb93df61507a2e584354de459ca-1920x1080-a431ce7a2eb0a4eb720b25de2ba0aca3.png" title="project 1" /></a>
<a href="project2"><img src="https://www.rolls-roycemotorcars.com/content/dam/rrmc/marketUK/rollsroycemotorcars_com/2-6-4-under-the-stars/page-properties/rolls-royce-under-the-stars-hero-d.jpg" title="project 2" /></a>
<a href="project3"><img src="https://www.citynews1130.com/wp-content/blogs.dir/sites/9/2018/05/27/iStock-697020460.jpg" title="project 3" /></a>
<a href="project4"><img src="https://lh6.googleusercontent.com/GlbCESxjIY2pjzV9B9hwoySVG6sJRErg_ylUlHt2XUm2PWhDpDWBZMOkUL0s4wMImdG8LP8Xm6KfB3KPVddCiBGSJO9psgZ13DkPSjNUngDg1iyGkQvxOQXdLDp1PXyF9b-lVSQ" title="project 4" /></a>
<a href="project5"><img src="https://www.esa.int/var/esa/storage/images/esa_multimedia/images/2019/07/moon_seen_from_space_station/19494773-1-eng-GB/Moon_seen_from_Space_Station_pillars.jpg" title="project 5" /></a>
<a href="project6"><img src="https://www.metoffice.gov.uk/binaries/content/gallery/metofficegovuk/hero-images/weather/cloud/cumulus-cloud.jpg" title="project 6" /></a>
<a href="project1" id="firstClone"><img src="https://cdn1.epicgames.com/ue/product/Screenshot/StoreUltraDynamicSkyscreenshot1-1920x1080-f5d2cdb93df61507a2e584354de459ca-1920x1080-a431ce7a2eb0a4eb720b25de2ba0aca3.png" title="project 1clone" /></a>
</div>
</div>
【问题讨论】:
-
如果您使用 jQuery,请尝试使用 inview 插件 github.com/protonet/jquery.inview。它可以节省时间。
标签: javascript foreach addeventlistener appendchild