英文:
CSS - All tiles not shown
问题
I am trying to develop a gallery wherin mouse hover shows an image that can be clicked to open. I got everything to work but once I added the links, things got a bit messy. Not It is only showing the last tile. Heres the code for your reference:
HTML:
<div id="gallery">
<a href="#"><div class="tile">
<img src="pictures/6.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/10.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/21.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/22.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/23.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/28.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/34.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/35.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/84.jpg" />
</div></a>
CSS:
body {
background-color: rgb(10, 10, 10);
height: 100vh;
margin: 0px;
overflow: hidden;
}
#gallery {
height: 140vmax;
width: 140vmax;
position: absolute;
}
.tile {
border-radius: 1vmax;
position: absolute;
transition: transform 800ms ease;
}
.tile:hover {
transform: scale(1.1);
}
.tile:hover > img {
opacity: 1;
transform: scale(1.01);
}
.tile > img {
height: 100%;
width: 100%;
object-fit: cover;
border-radius: inherit;
opacity: 0;
transition: opacity 800ms ease,
transform 800ms ease;
}
.tile:nth-child(1) {
background-color: rgb(255, 238, 88);
height: 14%;
width: 20%;
left: 5%;
top: 5%;
}
.tile:nth-child(2) {
background-color: rgb(66, 165, 245);
height: 24%;
width: 14%;
left: 42%;
top: 12%;
}
.tile:nth-child(3) {
background-color: rgb(239, 83, 80);
height: 18%;
width: 16%;
left: 12%;
top: 34%;
}
.tile:nth-child(4) {
background-color: rgb(102, 187, 106);
height: 14%;
width: 12%;
left: 45%;
top: 48%;
}
.tile:nth-child(5) {
background-color: rgb(171, 71, 188);
height: 16%;
width: 32%;
left: 8%;
top: 70%;
}
.tile:nth-child(6) {
background-color: rgb(255, 167, 38);
height: 24%;
width: 24%;
left: 68%;
top: 8%;
}
.tile:nth-child(7) {
background-color: rgb(63, 81, 181);
height: 16%;
width: 20%;
left: 50%;
top: 74%;
}
.tile:nth-child(8) {
background-color: rgb(141, 110, 99);
height: 24%;
width: 18%;
left: 72%;
top: 42%;
}
.tile:nth-child(9) {
background-color: rgb(250, 250, 250);
height: 10%;
width: 8%;
left: 84%;
top: 84%;
}
a {
text-decoration: none;
color: white;
}
JS:
document.addEventListener("DOMContentLoaded", function(event) {
const gallery = document.getElementById("gallery");
window.onmousemove = e => {
const mouseX = e.clientX,
mouseY = e.clientY;
const xDecimal = mouseX / window.innerWidth,
yDecimal = mouseY / window.innerHeight;
const maxX = gallery.offsetWidth - window.innerWidth,
maxY = gallery.offsetHeight - window.innerHeight;
const panX = maxX * xDecimal * -1,
panY = maxY * yDecimal * -1;
gallery.animate({
transform: translate(${panX}px, ${panY}px)
}, {
duration: 4000,
fill: "forwards",
easing: "ease"
})
}
});
I need all the tile to show at their respective places and if you want to see how it should actually look, you can just remove all the link tags.
It would also be helpful if someone can tell me how I can automate adding new images using an algorithm instead of 'nth child'.
Thanks in advance!!!
英文:
I am trying to develop a gallery wherin mouse hover shows an image that can be clicked to open. I got everything to work but once I added the links, things got a bit messy. Not It is only showing the last tile. Heres the code for your reference:
HTML:
<div id="gallery">
<a href="#"><div class="tile">
<img src="pictures/6.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/10.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/21.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/22.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/23.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/28.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/34.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/35.jpg" />
</div></a>
<a href="#"><div class="tile">
<img src="pictures/84.jpg" />
</div></a>
</div>
CSS:
body {
background-color: rgb(10, 10, 10);
height: 100vh;
margin: 0px;
overflow: hidden;
}
#gallery {
height: 140vmax;
width: 140vmax;
position: absolute;
}
.tile {
border-radius: 1vmax;
position: absolute;
transition: transform 800ms ease;
}
.tile:hover {
transform: scale(1.1);
}
.tile:hover > img {
opacity: 1;
transform: scale(1.01);
}
.tile > img {
height: 100%;
width: 100%;
object-fit: cover;
border-radius: inherit;
opacity: 0;
transition: opacity 800ms ease,
transform 800ms ease;
}
.tile:nth-child(1) {
background-color: rgb(255, 238, 88);
height: 14%;
width: 20%;
left: 5%;
top: 5%;
}
.tile:nth-child(2) {
background-color: rgb(66, 165, 245);
height: 24%;
width: 14%;
left: 42%;
top: 12%;
}
.tile:nth-child(3) {
background-color: rgb(239, 83, 80);
height: 18%;
width: 16%;
left: 12%;
top: 34%;
}
.tile:nth-child(4) {
background-color: rgb(102, 187, 106);
height: 14%;
width: 12%;
left: 45%;
top: 48%;
}
.tile:nth-child(5) {
background-color: rgb(171, 71, 188);
height: 16%;
width: 32%;
left: 8%;
top: 70%;
}
.tile:nth-child(6) {
background-color: rgb(255, 167, 38);
height: 24%;
width: 24%;
left: 68%;
top: 8%;
}
.tile:nth-child(7) {
background-color: rgb(63, 81, 181);
height: 16%;
width: 20%;
left: 50%;
top: 74%;
}
.tile:nth-child(8) {
background-color: rgb(141, 110, 99);
height: 24%;
width: 18%;
left: 72%;
top: 42%;
}
.tile:nth-child(9) {
background-color: rgb(250, 250, 250);
height: 10%;
width: 8%;
left: 84%;
top: 84%;
}
a {
text-decoration: none;
color: white;
}
JS:
document.addEventListener("DOMContentLoaded", function(event) {
const gallery = document.getElementById("gallery");
window.onmousemove = e => {
const mouseX = e.clientX,
mouseY = e.clientY;
const xDecimal = mouseX / window.innerWidth,
yDecimal = mouseY / window.innerHeight;
const maxX = gallery.offsetWidth - window.innerWidth,
maxY = gallery.offsetHeight - window.innerHeight;
const panX = maxX * xDecimal * -1,
panY = maxY * yDecimal * -1;
gallery.animate({
transform: `translate(${panX}px, ${panY}px)`
}, {
duration: 4000,
fill: "forwards",
easing: "ease"
})
}
});
I need all the tile to show at their respective places and if you want to see how it should actually look, you can just remove all the link tags.
It would also be helpful if someone can tell me how I can automate adding new images using an algorithm instead of 'nth child'.
Thanks in advance!!!
答案1
得分: 0
问题出在CSS中,特别是nth-child
选择器。这个选择器是计算父元素#gallery
的子元素数量,但是根据你的HTML结构,每个.tile
实际上都包裹在一个<a>
标签中,所以每个.tile
实际上是其父<a>
标签的第一个且唯一的子元素。
更好的方法是将.tile
类应用于<a>
标签本身,这样CSS就可以直接应用于#gallery
的每个单独的链接子元素。
在CSS中,你可以继续使用nth-child()
以保持简单,或者如果你想更多控制,也可以动态生成类(如tile-1
,tile-2
等)并为这些类定义样式。
如果你有数百张图片需要加载,类似这样的JavaScript代码可以解决你的问题:
window.onload = function() {
const gallery = document.getElementById("gallery");
for(let i = 1; i <= 200; i++) {
const tile = document.createElement("a");
tile.href = "#";
tile.classList.add("tile");
const img = document.createElement("img");
img.src = `pictures/${i}.jpg`;
tile.appendChild(img);
gallery.appendChild(tile);
}
};
我必须指出,这段代码假定pictures文件夹下的.jpg文件命名为1.jpg,2.jpg...等等。如果你有不同的命名约定,你应该相应地修改img.src=...
这一行。
英文:
The problem lies in the CSS, in particular the nth-child
selector. This selector is counting children of the parent element #gallery
, but with your HTML structure where every .tile
is wrapped with an <a>
tag, every .tile
is actually the first and only child of its parent <a>
tag.
A better approach would be to apply the .tile
class to the <a>
tag itself, which would allow the CSS to apply to every individual link as a direct child of #gallery
.
In the CSS, you could keep using nth-child()
for simplicity, or if you want to have more control, you could also generate classes dynamically (like tile-1
, tile-2
, etc.) and define styles for those classes.
If you have hundreds of images that you would like to load a JavaScript code similar to this will solve your problem:
window.onload = function() {
const gallery = document.getElementById("gallery");
for(let i = 1; i <= 200; i++) {
const tile = document.createElement("a");
tile.href = "#";
tile.classList.add("tile");
const img = document.createElement("img");
img.src = `pictures/${i}.jpg`;
tile.appendChild(img);
gallery.appendChild(tile);
}
};
I have to point out that this code assumes that .jpg files under the pictures folder is named as 1.jpg, 2.jpg... and so on. If you have a different naming convention, you should alter the img.src=...
line accordingly.
评论