How to add delay to Display:block

Asked

Viewed 1,048 times

0

I’m creating a website, and I wish there was a "button" with the Whatsapp icon on it, and by hovering the mouse over that button, it expanded and showed the number I put there. Currently the code is like this:

HTML:

<div id="whatsapp"></div>    
<div id="whatsnumber">(99)99999-9999</div>

CSS:

#whatsapp{
position:fixed;
width:40px;
height:40px;
margin-top:calc(50vh - 55px);
background:url(img/whatsapp.png);
background-color: #25D366;
background-position: center center;
background-size: cover;

}
#whatsapp:hover{
width:50px;
transition-duration: 0.2s;
}

#whatsnumber{
display:none;
font-size:2rem;
position: fixed;
margin-top:calc(50vh - 55px);
margin-left: 40px;
width:max-width;
height:40px;
background: #25D366;
line-height: 40px;
border-right: 10px solid #25D366;
color: white;
}

#whatsapp:hover ~ #whatsnumber{
display: block;
}

#whatsnumber:hover{
display: block;
}

#whatsapp:not(hover){
width:40px;
transition-duration: 0.2s;
}

The idea is that there are two Divs, one (#Whatsapp) that when passing the mouse over will expand, and another (#whatsnumber) that when passing the mouse over should wait a second and then appear.

I tried using Transition-delay to make "display:block" take effect after a second, but it’s not working.

How to solve?

  • Would you like a way to increase your div by going over it?

2 answers

2


Follows an alternative.

div {
  clear: both;
}

.icon {
  position: absolute;
  top: 0px;
  left: 0px;  
  height: 32px;
  width: 32px;
  background-repeat: no-repeat;
  background-size: calc(100% - 4px);
  background-position: center;
}

.social {
  position: relative;
  float: left;
  width: 32px;
  padding-left: 32px;
  box-sizing: border-box;
  overflow: hidden;  
  line-height: 32px;
  border-radius: 5px;
  box-shadow: 0px 0px 5px black;
  white-space: nowrap;
  transition: width 0.5s linear;
  margin: 3px 0px;
}

.social:hover {
  width: 150px;
}

.icon-whatsapp {
  background-image: url('https://image.flaticon.com/icons/svg/220/220236.svg');
}

.icon-skype {
  background-image: url('https://image.flaticon.com/icons/svg/220/220235.svg');
}

.icon-tweet {
  background-image: url('https://image.flaticon.com/icons/svg/220/220233.svg');
}

.icon-facebook {
  background-image: url('https://image.flaticon.com/icons/svg/220/220200.svg');
}
<div class="social">
  <div class="icon icon-whatsapp"></div>
  (99)99999-9999
</div>
<div class="social">
  <div class="icon icon-skype"></div>
  (99)99999-9999
</div>
<div class="social">
  <div class="icon icon-facebook"></div>
  (99)99999-9999
</div>
<div class="social">
  <div class="icon icon-tweet"></div>
  (99)99999-9999
</div>

0

Cannot add transition to events: display and visibility.

The explanation is:

Both visibility: Hidden; and display: None; do not respond to Event handlers, while opacity allows handlers to be called.

Then you can get to the expected result using opacity and the transition (transition) with 1 second:

#whatsapp{
position:fixed;
width:40px;
height:40px;
margin-top:calc(50vh - 55px);
background:url(img/whatsapp.png);
background-color: #25D366;
background-position: center center;
background-size: cover;
transition: all 1s;
}
#whatsapp:hover{
width:50px;
}

#whatsnumber{
opacity:0;
font-size:2rem;
position: fixed;
margin-top:calc(50vh - 55px);
margin-left: 40px;
width:max-width;
height:40px;
background: #25D366;
line-height: 40px;
border-right: 10px solid #25D366;
color: white;
transition: all 1s;
}

#whatsapp:hover ~ #whatsnumber{
opacity: 1;
}

#whatsnumber:hover{
opacity: 1;
}

#whatsapp:not(hover){
width:40px;
}
<div id="whatsapp"></div>    
<div id="whatsnumber">(99)99999-9999</div>

More information on this question: Transition does not work in Javascript

Browser other questions tagged

You are not signed in. Login or sign up in order to post.