【问题标题】:How do I have both :hover and transition-delay?我如何同时拥有 :hover 和 transition-delay?
【发布时间】:2022-01-17 21:03:07
【问题描述】:

我有一个按钮,里面有一些 CSS:

button {
    visibility: visible;
    padding: 12px;
    background-color: var(--dracula-accent);
    border-radius: 50%;
    margin-bottom: 2em;
}

我想在上面放一些悬停效果

button:hover {
    background-color: #6272a4;
}

但是悬停5秒后,我希望按钮改变它的大小和内容

button:hover {
    background-color: #000000;
    width: 100px;
    content: "click me"
    transition-delay: 5s;
}

但是拥有所有这些是行不通的。我不想使用 Javascript,我只在做 CSS。

【问题讨论】:

    标签: css hover css-transitions


    【解决方案1】:

    首先,如果您想在悬停和鼠标悬停时使用不同的延迟值,您需要同时在 buttonbutton:hover 上设置过渡设置。

    其次,如果您想对width 等属性进行转换,则在button 上指定其初始值会更好。

    最后,您可以使用::after 伪元素来达到预期的效果:

    你的 HTML

    <button>click me</button>
    

    你的 CSS

    button {
      padding: 12px;
      border-radius: 50%;
      margin-bottom: 2em;
      position: relative;
      transition-delay: 0s;
      background-color: #6272a4;
      width: 100px;
    }
    
    button::after {
      content:"test";
      position: absolute;
      top:0;
      left:0;
      right: 0;
      bottom: 0;
      padding: 12px;
      border-radius: 50%;
      background-color: red;
    }
    
    button:hover::after  {
      display: none;
    }
    
    button:hover  {
      background-color: #000000;
      width: 250px;
      transition-delay: 2s;
      color: red;
    }
    
    

    您可以根据需要添加过渡时间和缓和度。

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      相关资源
      最近更新 更多