【问题标题】:Hovering over child and parent to trigger change将鼠标悬停在子级和父级上以触发更改
【发布时间】:2019-12-11 15:46:31
【问题描述】:

我正在尝试制作一个带有按钮和自定义精灵图像的输入字段。

输入应该改变边框颜色并显示带有 悬停时的图标。此外,同时悬停输入和 按钮,图标应该改变。

现在图标随着 input:focus 的变化而变化,同时关闭输入的边框。

如果有人能指导我正确的方向,将不胜感激。

到目前为止,这是我想出的:

HTML

<form class="b-editor-item col-3">
  <input type="text" placeholder="D" class="b-editor-d-input" />
  <button type="button" class="b-editor-button-delete">
       <div class="b-editor-icon-trash"></div>
  </button>
</form>

SCSS

input[type="text"] {
  border: 1px solid transparent;
}

.b-editor {
  &-item {
    display: flex;
    align-items: center;
  }

  &-button-delete {
    border: none;
    height: 24px;
    width: 24px;
    overflow: hidden;
    background-color: transparent;
  }

  &-d-input {
    color: black;
    background-color: #eaeaea;
    border-radius: 6px;
    width: 90%;
    margin-right: -3.25rem;
    height: 2rem;
  }

  &-icon-trash {
    background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1044597/trash.png);
    background-repeat: no-repeat;
    background-position: -28px 0;
    background-size: 300%;
    height: 20px;
    width: 20px;
    padding: 5px 2px;
  }

  &-d-input:hover + &-button-delete:hover > &-icon-trash,
  &-d-input:focus + &-button-delete:hover > &-icon-trash {
    background-position: -52px 0;
  }

  &-d-input + &-button-delete {
    display: none;
  }
  &-d-input:hover + &-button-delete,
  &-d-input:focus + &-button-delete,
  &-d-input:hover + &-button-delete:hover {
    display: block;
  }

  &-d-input:hover,
  &-d-input:hover + &-button-delete:hover {
    border: 1px solid #00c8aa !important;
  }
}

【问题讨论】:

  • 所以你想让你的图标在悬停时被着色?
  • 有颜色,但它是通过移动精灵图像来处理的。基本上我需要在悬停时触发规则;同时悬停子元素和父元素。

标签: html css sass hover styling


【解决方案1】:

尝试添加这两条规则:

button.b-editor-button-delete:hover {
    display: block;
}

button.b-editor-button-delete:hover .b-editor-icon-trash {
    background-position: -52px 0;
}

【讨论】:

  • 规则正在起作用,剩下要做的就是使边框保持绿色而不是回到透明状态。在我将问题标记为已解决之前,我会一直玩到明天。如果您对如何执行此操作有任何建议,您可以通过codepen.io/organyx/pen/LYENqVQ 随时查看
  • 这不会像您预期的那样工作。您将不得不更改您的 HTML 嵌套,然后调整您的 css
  • 你说得对,我改变了嵌套并简化了规则。我把它复杂化了。这是我用codepen.io/organyx/pen/KKwMgZY替换它的内容@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 2022-08-20
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多