有人会告诉我为什么:悬停不检测正确的 child 只将整体视为一个元素,即使我区分了更多的 child 并且一直在同一屏幕上更改我,如何解决?当我尝试将鼠标悬停在元素“.image”上时,我需要这样做 大元素开关 z-index 为正确的元素。
在线验证码:https://jsfiddle.net/2zr6pj9u/1/
HTML
CSS
.galery {
width: 100%;
height: 80vh;
lề trên: 20px;
hiển thị: linh hoạt;
flex-flow: row nowrap;
đường viền: 1px màu đen;
}
.small-img {
hiển thị: linh hoạt;
hướng flex: cột;
margin-right: 2px;
}
.hình ảnh {
width: 100%;
height: 20vh;
}
hình ảnh {
width: 100%;
chiều cao: 100%;
}
.big-img {
hiển thị: linh hoạt;
position: relative;
width: 100%;
chiều cao: 100%;
}
.big-image {
hiển thị: linh hoạt;
position: absolute;
chiều cao: 100%;
width: 100%;
}
#nr1 {
chỉ số z: 1;
}
#nr2 {
z-index: 2;
}
#nr3 {
chỉ số z: 3;
}
#nr4 {
chỉ số z: 4;
}
.small-img:first-child:hover ~ .big-img #nr1 {
chỉ số z: 5;
}
.small-img:nth-child(2):hover ~ .big-img #nr2 {
chỉ số z: 5;
}
.small-img:nth-child(3):hover ~ .big-img #nr3 {
chỉ số z: 5;
}
.small-img:last-child:hover ~ .big-img #nr4 {
chỉ số z: 5;
}
如果不使用 Javascript,您将无法实现这一点。该问题概述如下。
Có hai câu hỏi:
- 您在错误的元素上设置了
:con thứ n
. con thứ n
适用于您定位的元素。在您的代码中,您始终以 .small-img
div 而非 .hình ảnh
div 为目标 - 这正是您想要的。
CSS
.small-img .image:first-child:hover ~ .big-img #nr1{
chỉ số z: 5;
}
.small-img .image:nth-child(2):hover ~ .big-img #nr2{
chỉ số z: 5;
}
.small-img .image:nth-child(3):hover ~ .big-img #nr3{
chỉ số z: 5;
}
.small-img .image:last-child:hover ~ .big-img #nr4{
chỉ số z: 5;
}
这是您应该拥有的内容,以便您可以定位每个 .hình ảnh
子级。
- 这就是问题所在。现在我们定位正确的子 div (
.hình ảnh
),我们无法在 CSS 中移动到父 div (.small-img
) 之外,然后定位兄弟 .big-img
div. big-img
div 不是 .hình ảnh
div 的直接兄弟,因此我们不能定位它。
如果您想更新标记,这里有一个解决方案:
.grid-container {
hiển thị: lưới;
grid-gap: 0;
grid-template-columns: 25% 75%;
grid-template-rows: 100px 100px 100px 100px;
grid-template-areas: "small1 big" "small2 big" "small3 big" "small4 big";
}
.grid-container .small-image:nth-child(1) {
nền: màu vàng;
grid-area: small1;
}
.grid-container .small-image:nth-child(1):hover~#limage-1 {
chỉ số z: 10;
}
.grid-container .small-image:nth-child(2) {
background: red;
grid-area: small2;
}
.grid-container .small-image:nth-child(2):hover~#limage-2 {
chỉ số z: 10;
}
.grid-container .small-image:nth-child(3) {
grid-area: small3;
nền: màu xanh;
}
.grid-container .small-image:nth-child(3):hover~#limage-3 {
chỉ số z: 10;
}
.grid-container .small-image:nth-child(4) {
grid-area: small4;
nền: màu tím;
}
.grid-container .small-image:nth-child(4):hover~#limage-4 {
chỉ số z: 10;
}
.grid-container .large-image {
position: relative;
grid-area: big;
}
.grid-container .large-image#limage-1 {
nền: màu vàng;
chỉ số z: 9;
}
.grid-container .large-image#limage-2 {
background: red;
chỉ số z: 1;
}
.grid-container .large-image#limage-3 {
nền: màu xanh;
chỉ số z: 1;
}
.grid-container .large-image#limage-4 {
nền: màu tím;
chỉ số z: 1;
}
密码本 https://codepen.io/chrislafrombois/pen/gEbLRE
Tôi là một lập trình viên xuất sắc, rất giỏi!