鱼C论坛

 找回密码
 立即注册
查看: 626|回复: 13

[已解决].nav:hover .z-index1

[复制链接]
发表于 2023-3-24 13:35:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
.nav:hover .z-index1{
    display: none;

}


.z-index1是只有当它是.nav的子元素才会生效吗?不是子元素的话为什么没反应
最佳答案
2023-3-25 18:01:43
可以稍微的优化一下,这样写

  1. // 获取第一个具有 image 类的元素
  2. const [selectedImage] = document.querySelectorAll('.image');

  3. // 添加事件委托到父元素上
  4. const parent = document.querySelector('.parent');
  5. parent.addEventListener('click', event => {
  6.   // 获取被点击的子元素,如果不是图片则返回 null
  7.   const image = event.target.closest('.image');
  8.   if (image) {
  9.     // 取消之前选中的图片的选中状态
  10.     const prevSelectedImage = document.querySelector('.selected');
  11.     if (prevSelectedImage) {
  12.       prevSelectedImage.classList.remove('selected');
  13.     }

  14.     // 标记当前选中的图片,并修改其它图片的 z-index 值
  15.     image.classList.add('selected');

  16.     // 获取当前图片的兄弟元素,并修改它们的 z-index 值
  17.     const siblings = Array.from(image.parentElement.children).filter(el => el !== image && el.classList.contains('image'));
  18.     siblings.forEach(sibling => {
  19.       sibling.style.zIndex = '0';
  20.     });

  21.     // 选择前面和后面的兄弟元素,并修改它们的 z-index 值
  22.     let previousImage = image.previousElementSibling;
  23.     while (previousImage && !previousImage.classList.contains('image')) {
  24.       previousImage = previousImage.previousElementSibling;
  25.     }
  26.     if (previousImage) {
  27.       previousImage.style.zIndex = '1';
  28.     }
  29.     let nextImage = image.nextElementSibling;
  30.     while (nextImage && !nextImage.classList.contains('image')) {
  31.       nextImage = nextImage.nextElementSibling;
  32.     }
  33.     if (nextImage) {
  34.       nextImage.style.zIndex = '1';
  35.     }
  36.   }
  37. });
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-24 14:13:13 | 显示全部楼层
是的,这个 CSS 规则只对位于 .nav 元素内的 .z-index1 子元素生效。当你在 .nav 元素上悬停时,对应的 .z-index1 子元素的 display 属性会被设置为 none,使其隐藏。

这个选择器的结构如下:

.nav:hover:表示当悬停在具有 .nav 类名的元素上时。
空格:表示后面的类名应该是前面类名元素的子元素(可以是直接子元素,也可以是间接子元素)。
.z-index1:表示具有 .z-index1 类名的元素。
所以,如果 .z-index1 元素不是 .nav 元素的子元素,这个 CSS 规则不会生效。要让规则适用于任何位置的 .z-index1 元素,可以修改选择器,去掉 .nav:hover 部分,如下所示:

  1. .z-index1:hover {
  2.     display: none;
  3. }
复制代码

这样,无论 .z-index1 元素是否位于 .nav 元素内,只要你悬停在它上面,它就会隐藏。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-24 22:03:25 | 显示全部楼层
isdkz 发表于 2023-3-24 14:13
是的,这个 CSS 规则只对位于 .nav 元素内的 .z-index1 子元素生效。当你在 .nav 元素上悬停时,对应的 .z- ...

可是如果我想让 .nav:hover触发的时候,选取的另一个z-index1执行应该怎么写呢(不是父子关系 js还没学)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-24 22:05:04 | 显示全部楼层
isdkz 发表于 2023-3-24 14:13
是的,这个 CSS 规则只对位于 .nav 元素内的 .z-index1 子元素生效。当你在 .nav 元素上悬停时,对应的 .z- ...

我想实现切换图片的功能,如果点击一个图标的话,可以改变其它图片的z-index的值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-25 16:21:50 | 显示全部楼层
是的,.z-index1 是只有在 .nav 元素内部的子元素才会被选择并生效。如果 .z-index1 不是子元素,那么它不会被 .nav:hover 所选择。

如果您想要在 .nav:hover 触发时选取另一个 .z-index1 元素,可以使用相邻兄弟选择器 + 或者通用兄弟选择器 ~。下面是两个例子:

使用相邻兄弟选择器 + :

.nav:hover + .z-index1 {
    /* 在 .nav 元素后的第一个 .z-index1 元素被选取 */
}
使用通用兄弟选择器 ~ :

.nav:hover ~ .z-index1 {
    /* 所有在 .nav 元素后的 .z-index1 元素都被选取 */
}
如果您想要实现切换图片的功能,可以使用 JavaScript。可以给每个图标添加一个事件监听器,当图标被点击时,修改其它图片的 z-index 值。

下面是一个简单的例子:


  1. // 获取所有图片元素和当前选中的图片元素
  2. const images = document.querySelectorAll('.image');
  3. let selectedImage = null;

  4. // 添加事件监听器
  5. images.forEach(image => {
  6.   image.addEventListener('click', () => {
  7.     // 取消之前选中的图片的选中状态
  8.     if (selectedImage) {
  9.       selectedImage.classList.remove('selected');
  10.     }
  11.     // 标记当前选中的图片,并修改其它图片的 z-index 值
  12.     selectedImage = image;
  13.     selectedImage.classList.add('selected');
  14.     images.forEach(otherImage => {
  15.       if (otherImage !== selectedImage) {
  16.         otherImage.style.zIndex = '0';
  17.       }
  18.     });
  19.   });
  20. });
  21. 这个例子假设每个图片元素都有 image 类名,并且已经使用 CSS 样式设置它们的 z-index 值。当一个图片被点击时,它被标记为选中状态,并将其它图片的 z-index 值设置为 0。
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 17:16:24 | 显示全部楼层
不二猫猫 发表于 2023-3-25 16:21
是的,.z-index1 是只有在 .nav 元素内部的子元素才会被选择并生效。如果 .z-index1 不是子元素,那么它不 ...

还是有点问题,没有生效

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 17:17:22 | 显示全部楼层
这个是代码
屏幕截图 2023-03-25 171503.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 17:21:26 | 显示全部楼层
不二猫猫 发表于 2023-3-25 16:21
是的,.z-index1 是只有在 .nav 元素内部的子元素才会被选择并生效。如果 .z-index1 不是子元素,那么它不 ...


右下角希望实现点击图标可以短暂切换图片
Snipaste_2023-03-25_17-20-45.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 17:23:57 | 显示全部楼层
她与晚风 发表于 2023-3-25 17:21
右下角希望实现点击图标可以短暂切换图片

1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 17:32:27 | 显示全部楼层
不二猫猫 发表于 2023-3-25 16:21
是的,.z-index1 是只有在 .nav 元素内部的子元素才会被选择并生效。如果 .z-index1 不是子元素,那么它不 ...

懂了哥!!!!
这个通用兄弟选择器只能选取后面的子元素,我移动了一些代码位置就好了...
那有没有可以选择前面的兄弟元素的方法呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2023-3-25 17:56:33 | 显示全部楼层
她与晚风 发表于 2023-3-25 17:32
懂了哥!!!!
这个通用兄弟选择器只能选取后面的子元素,我移动了一些代码位置就好了...
那有没有可以选择 ...
  1. const images = document.querySelectorAll('.image');
  2. let selectedImage = null;

  3. // 添加事件监听器
  4. images.forEach(image => {
  5.   image.addEventListener('click', () => {
  6.     // 取消之前选中的图片的选中状态
  7.     if (selectedImage) {
  8.       selectedImage.classList.remove('selected');
  9.     }
  10.     // 标记当前选中的图片,并修改其它图片的 z-index 值
  11.     selectedImage = image;
  12.     selectedImage.classList.add('selected');
  13.     images.forEach(otherImage => {
  14.       if (otherImage !== selectedImage) {
  15.         otherImage.style.zIndex = '0';
  16.       } else {
  17.         // 选择前面的兄弟元素
  18.         let previousImage = selectedImage.previousElementSibling;
  19.         while (previousImage && !previousImage.classList.contains('image')) {
  20.           previousImage = previousImage.previousElementSibling;
  21.         }
  22.         if (previousImage) {
  23.           previousImage.style.zIndex = '1';
  24.         }
  25.       }
  26.     });
  27.   });
  28. });
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-25 17:58:10 | 显示全部楼层
选择前面和后面的兄弟元素

  1. const images = document.querySelectorAll('.image');
  2. let selectedImage = null;

  3. // 添加事件监听器
  4. images.forEach(image => {
  5.   image.addEventListener('click', () => {
  6.     // 取消之前选中的图片的选中状态
  7.     if (selectedImage) {
  8.       selectedImage.classList.remove('selected');
  9.     }
  10.     // 标记当前选中的图片,并修改其它图片的 z-index 值
  11.     selectedImage = image;
  12.     selectedImage.classList.add('selected');
  13.     images.forEach(otherImage => {
  14.       if (otherImage !== selectedImage) {
  15.         otherImage.style.zIndex = '0';
  16.       } else {
  17.         // 选择前面和后面的兄弟元素
  18.         let previousImage = selectedImage.previousElementSibling;
  19.         while (previousImage && !previousImage.classList.contains('image')) {
  20.           previousImage = previousImage.previousElementSibling;
  21.         }
  22.         if (previousImage) {
  23.           previousImage.style.zIndex = '1';
  24.         }
  25.         let nextImage = selectedImage.nextElementSibling;
  26.         while (nextImage && !nextImage.classList.contains('image')) {
  27.           nextImage = nextImage.nextElementSibling;
  28.         }
  29.         if (nextImage) {
  30.           nextImage.style.zIndex = '1';
  31.         }
  32.       }
  33.     });
  34.   });
  35. });
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-25 18:01:43 | 显示全部楼层    本楼为最佳答案   
可以稍微的优化一下,这样写

  1. // 获取第一个具有 image 类的元素
  2. const [selectedImage] = document.querySelectorAll('.image');

  3. // 添加事件委托到父元素上
  4. const parent = document.querySelector('.parent');
  5. parent.addEventListener('click', event => {
  6.   // 获取被点击的子元素,如果不是图片则返回 null
  7.   const image = event.target.closest('.image');
  8.   if (image) {
  9.     // 取消之前选中的图片的选中状态
  10.     const prevSelectedImage = document.querySelector('.selected');
  11.     if (prevSelectedImage) {
  12.       prevSelectedImage.classList.remove('selected');
  13.     }

  14.     // 标记当前选中的图片,并修改其它图片的 z-index 值
  15.     image.classList.add('selected');

  16.     // 获取当前图片的兄弟元素,并修改它们的 z-index 值
  17.     const siblings = Array.from(image.parentElement.children).filter(el => el !== image && el.classList.contains('image'));
  18.     siblings.forEach(sibling => {
  19.       sibling.style.zIndex = '0';
  20.     });

  21.     // 选择前面和后面的兄弟元素,并修改它们的 z-index 值
  22.     let previousImage = image.previousElementSibling;
  23.     while (previousImage && !previousImage.classList.contains('image')) {
  24.       previousImage = previousImage.previousElementSibling;
  25.     }
  26.     if (previousImage) {
  27.       previousImage.style.zIndex = '1';
  28.     }
  29.     let nextImage = image.nextElementSibling;
  30.     while (nextImage && !nextImage.classList.contains('image')) {
  31.       nextImage = nextImage.nextElementSibling;
  32.     }
  33.     if (nextImage) {
  34.       nextImage.style.zIndex = '1';
  35.     }
  36.   }
  37. });
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-25 18:12:38 | 显示全部楼层
不二猫猫 发表于 2023-3-25 17:58
选择前面和后面的兄弟元素

ok,还没学到js
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表