冲突的悬停伪类

时间:2018-08-30 05:55:28

标签: css css-selectors hover

我有一种情况,当元素的父对象悬停在元素上时,我都使用悬停伪类修改元素。不幸的是,它们具有冲突的属性(都试图定义属性transform),这就是为什么-我认为-当鼠标悬停在按钮上但缩放光标时缩放不起作用。是否可以仅使用CSS进行解决?

body {
  overflow: hidden;
 }

#action-panel {
    align-items: center;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 20px 20px 20px 20px;
    position: absolute;
    right: 0;
    transform: translateY(-50%);
    top: 50%;
}

#action-panel:hover .action-button {
    opacity: 1;
    transform: translateX(0);
}

.action-button {
    background-color: lightblue;
    border-radius: 25px;
    height: 50px;
    margin-bottom: 20px;
    opacity: 0;
    transition: opacity 200ms ease-in-out, transform 200ms ease-in-out;
    transform: translateX(calc(100% + 20px));
    width: 50px;
}

.action-button:hover {
    cursor: pointer;
    transform: translateX(0) scale(1.2);
}

.action-button span {
    font-size: 30px;
    left: 50%;
    position: relative;
    transform: translate(-50%, -50%);
    top: 50%;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div id="action-panel">
    <div class="action-button"><span class="material-icons">3d_rotation</span></div>
    <div class="action-button"><span class="material-icons">compare_arrows</span></div>
    <div class="action-button"></div>
</div>

注意:

如果无法从代码中分辨出来,则需要将光标移到右边缘的中间以查看我所描述的效果。

1 个答案:

答案 0 :(得分:2)

通过在其前面添加#action-panel来增加以下样式的特殊性。在您的代码中,parent的悬浮样式覆盖了child的悬浮样式。

 #action-panel .action-button:hover {
  cursor: pointer;
  transform: translateX(0) scale(1.2);
}

body {
  overflow: hidden;
}

#action-panel {
  align-items: center;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 20px 20px 20px 20px;
  position: absolute;
  right: 0;
  transform: translateY(-50%);
  top: 50%;
}

#action-panel:hover .action-button {
  opacity: 1;
  transform: translateX(0);
}

.action-button {
  background-color: lightblue;
  border-radius: 25px;
  height: 50px;
  margin-bottom: 20px;
  opacity: 0;
  transition: opacity 200ms ease-in-out, transform 200ms ease-in-out;
  transform: translateX(calc(100% + 20px));
  width: 50px;
}

#action-panel .action-button:hover {
  cursor: pointer;
  transform: translateX(0) scale(1.2);
}

.action-button span {
  font-size: 30px;
  left: 50%;
  position: relative;
  transform: translate(-50%, -50%);
  top: 50%;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div id="action-panel">
  <div class="action-button"><span class="material-icons">3d_rotation</span></div>
  <div class="action-button"><span class="material-icons">compare_arrows</span></div>
  <div class="action-button"></div>
</div>