Put Transition effect on div which is added class as scroll

Asked

Viewed 43 times

1

I’m trying to add a Transition in a div where I add a class according to the scroll, but it’s not working. Could someone try to help me understand why?

 window.addEventListener('scroll', function(){
  var pageScrollY = window.scrollY

  var msgChat = document.querySelector('.container-msg-chat');
                        
  if(pageScrollY >= 1000){
     msgChat.classList.add('msg-open');
  }else if(msgChat.classList.contains('msg-open')){
     msgChat.classList.remove('msg-open');
  }
});
a.container-icone--chat{
    position: fixed;
    bottom: 21px;
    right: 21px;
    background-color: #f96720;
    background-image: url('images/home/chat/icon-chat.png');
    background-position-x: 13px;
    background-position-y: center;
    background-repeat: no-repeat;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);
    transition: background-color 1s ease;
}
a.container-icone--chat:hover{
    background-color: #fff;
    background-position-x: -49px;
}
a.container-msg-chat{
    position: fixed;
    bottom: 100px;
    background-color: #fff;
    right: 21px;
    width: 125px;    
    padding: 15px;
    text-align: center;
    border-radius: 10px;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);
    display: none;
    transition: all 5s ease;
}
a.container-msg-chat:before{
    content: '';
    display: inline-block;
    position: absolute;
    bottom: -9px;
    right: 27px;
    border-top: 5px solid #fff;
    border-right: 5px solid transparent;
    border-bottom: 5px solid transparent;
    border-left: 5px solid transparent;
}
.btn-chat{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-indent: -9999px;
}
a.container-msg-chat.msg-open{
    display: block!important;
}
.container{
     height: 5000px;
}
<div class="container"></div>

<a href="#" class="container-msg-chat">¿Necesita ayuda?</a>
<a href="#" class="container-icone--chat">
    <span class="btn-chat">Chat</span>
</a>

  • Your question is not very clear Filype!

  • Hi @Leandrade, all right? I’m trying to put a Transition in the container-msg-chat, but it’s not happening when I add the msg-open class via js.

1 answer

0

The transition does not work with the property display. In this case you can use visibility and opacity to create the effect. For this, just make the modifications below:

a.container-msg-chat{
    position: fixed;
    bottom: 100px;
    background-color: #fff;
    right: 21px;
    width: 125px;    
    padding: 15px;
    text-align: center;
    border-radius: 10px;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);
    transition: opacity 5s ease;

    /* Modificado */
    display: block;

    /* Adicionado */
    opacity: 0;
    visibility: hidden;
}

a.container-msg-chat.msg-open{
    visibility: visible;
    opacity: 1
}

Example:

window.addEventListener('scroll', function(){
  var pageScrollY = window.scrollY

  var msgChat = document.querySelector('.container-msg-chat');
                        
  if(pageScrollY >= 1000){
     msgChat.classList.add('msg-open');
  }else if(msgChat.classList.contains('msg-open')){
     msgChat.classList.remove('msg-open');
  }
});
a.container-icone--chat{
    position: fixed;
    bottom: 21px;
    right: 21px;
    background-color: #f96720;
    background-image: url('images/home/chat/icon-chat.png');
    background-position-x: 13px;
    background-position-y: center;
    background-repeat: no-repeat;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);
    transition: background-color 1s ease;
}
a.container-icone--chat:hover{
    background-color: #fff;
    background-position-x: -49px;
}
a.container-msg-chat{
    position: fixed;
    bottom: 100px;
    background-color: #fff;
    right: 21px;
    width: 125px;    
    padding: 15px;
    text-align: center;
    border-radius: 10px;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);
    display: block;
    transition: opacity 5s ease;
    opacity: 0;
    visibility: hidden;
}
a.container-msg-chat:before{
    content: '';
    display: inline-block;
    position: absolute;
    bottom: -9px;
    right: 27px;
    border-top: 5px solid #fff;
    border-right: 5px solid transparent;
    border-bottom: 5px solid transparent;
    border-left: 5px solid transparent;
}
.btn-chat{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-indent: -9999px;
}
a.container-msg-chat.msg-open{
    visibility: visible;
    opacity: 1
}
.container{
     height: 5000px;
}
<div class="container"></div>

<a href="#" class="container-msg-chat">¿Necesita ayuda?</a>
<a href="#" class="container-icone--chat">
    <span class="btn-chat">Chat</span>
</a>

  • Thank you very much. It worked.

  • @Filypeferreira Good that it worked. If the answer helped, don’t forget to mark it as solved, so other users can easily find it.

Browser other questions tagged

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