繁体   English   中英

我如何同时拥有:hover 和转换延迟?

[英]How do I have both :hover and transition-delay?

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

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

我想在上面放一些 hover 效果

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

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

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

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

首先,如果您想在 hover 和 mouseout 上设置不同的延迟值,您需要在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;
}

如果需要,您可以添加过渡时间和缓和。

暂无
暂无

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

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