简体   繁体   English

只在悬停时显示按钮

[英]Only make a button appear on hover

Can anyone help me out with something really simple but can't seem to find anywhere.任何人都可以帮我解决一些非常简单但似乎找不到任何地方的事情。 I am using scss right now and I want the button to only display when I hover it.我现在正在使用 scss,我希望按钮仅在我悬停时显示。 I have it hidden right now and here is the code.我现在把它隐藏了,这是代码。 I am also using svgs and my button is nested within a block.我也在使用 svgs,我的按钮嵌套在一个块中。 Thanks guys谢谢你们

&__button {
    display: none;
    cursor: pointer;
    border: none;
    outline: none;
    width: 32px;
    height: 32px;
    background: url("../../assets/Icons\ -\ Thin\ Delete.svg") center center no-repeat;
    &:hover {
      background: url("../../assets/Icons\ -\ Thin\ Delete\ On\ Hover.svg") center center no-repeat;
    }
  }

Use visibility: hidden for default state and visibility: visible for hover state.( https://developer.mozilla.org/en-US/docs/Web/CSS/visibility )使用visibility: hidden默认状态visibility: hiddenvisibility: visible悬停状态visibility: visible 。( https://developer.mozilla.org/en-US/docs/Web/CSS/visibility

However, you have to nest your button inside another div.但是,您必须将按钮嵌套在另一个 div 中。 Hover states don't work when an element is hidden.当元素被隐藏时,悬停状态不起作用。 ( Why isn't CSS visibility working? ) 为什么 CSS 可见性不起作用?

Example code:示例代码:

HTML HTML

<div class="button-container">
    <button>
        Hover
    </button>
</div>

CSS CSS

.button-container button {
    visibility: hidden;
    ...
}

.button-container:hover button {
    visibility: visible;
}

or in SCSS或在 SCSS

.button-container {
    button {
        visibility: hidden;
    }

    &:hover {
        button {
            visibility: visible;
        }
    }
}

I think it's better simply use opacity.我认为最好简单地使用不透明度。

NOTE: Obviously, it's not visible but still affects layout as normal注意:显然,它不可见,但仍会正常影响布局

   .button
    {
      opacity:0
    }
    .button:hover
    {
      opacity:1
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM