javascript - css 中的 if 或 case 语句

标签 javascript html css drop-down-menu multi-level

我需要一些帮助来编写或找出是否可以在 css 中为悬停功能编写 if 或 case 语句。悬停用于显示具有共享类父名称的链接的下拉列表。

基本逻辑是,如果一个类的悬停功能被激活/打开,则停用/关闭共享父类中的另一个悬停功能。

在我看来伪代码是这样的:

if (.drop: hover .last) /*when the cursor hovers, do below*/ then 

.drop: hover .last{display:block;} 

.drop:hover .first{display: none;} 

.drop:hover .second{display: none;} 

.drop:hover .third{display: none;}  
etc... 

 elseif (.drop:hover .first) then 

.drop:hover .first{display: block;} 

.drop:hover .last{display: none;} 

.drop:hover .second{display: none;}

 .drop:hover .third{display: none;} 

endif

最佳答案

如果我明白你的意思,你需要使用 :not 来忽略 li:hover。 注意:使用不透明度代替 display: none 因为第二个将 li 移到左侧并丢失 :hover

看下面的代码:

HTML

<ul>
  <li>Link 1</li>
  <li>Link 2</li>
  <li>Link 3</li>
</ul>

CSS

ul {
  display: inline-block; 
}

ul > li {
  display: inline-block;
  padding: 5px 15px;
}
ul:hover > li:not(:hover) {
  opacity: 0;
}

更完整的功能: https://codepen.io/ralfting/pen/qKqBvM

关于javascript - css 中的 if 或 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50748530/

相关文章:

javascript - 如何在HTML中的输入文本框中添加span

javascript - jquery mobile 单页大小上限

javascript - 防止 raphael 中 div 悬停触发

html - 将鼠标悬停在 li 上时,如何让我的链接改变颜色?

css - 在CSS中居中一个div

jquery - 使用 jQuery 添加动态内容时 DIV 不扩展

html - html页面中固定侧边栏的样式

css - 对选定样本使用背景刻度图像,无法在图像上完成

javascript - AngularJS - 如何过滤一堆特定的 ID?

javascript - 如何随机选择三张图像,然后用新图像将它们淡出?