简体   繁体   English

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

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

I have a button and have some CSS in it:我有一个按钮,里面有一些 CSS:

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

I want to put some hover effects on it我想在上面放一些 hover 效果

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

But after 5 seconds of hovering, I want the button to change its size and content但是悬停5秒后,我希望按钮改变它的大小和内容

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

But having all of them doesn't work.但是拥有所有这些是行不通的。 I don't wanna use Javascript, I'm doing CSS only.我不想使用 Javascript,我只在做 CSS。

First of all, you need to put your transition setting on both button and button:hover , if you want different delay values on hover and mouseout.首先,如果您想在 hover 和 mouseout 上设置不同的延迟值,您需要在buttonbutton:hover上设置转换设置。

Secondly, if you want to have a transition on a property such as width , it works better if you specify its initial value on button .其次,如果您想对诸如width之类的属性进行过渡,则在button上指定其初始值会更好。

And finally, you can achieve the desired result by using the ::after pseudo-element:最后,您可以通过使用::after伪元素来实现所需的结果:

Your HTML你的 HTML

<button>click me</button>

Your CSS你的 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;
}

You can add transition time and ease if you want to.如果需要,您可以添加过渡时间和缓和。

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

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