How to change the color of the scroll bar by JQuery

0

I want to change the color of the scroll bar only when the user scrolls the screen. I tried the code below but it did not work. How do I?

$('body').scroll(function(){
    setTimeout(function(){
        $("body::-webkit-scrollbar").css("background-color", "#333");
        console.log('fsdfs');
    },500);
});
    
asked by anonymous 04.06.2018 / 04:40

1 answer

0

JavaScript can not change pseudo-elements, but you can create a class in CSS and add to body when scrolling:

$(window).scroll(function(){
   $("body").addClass("scroller");
});
.scroller::-webkit-scrollbar {
    width: 17px; /* largura do scroll */
}
.scroller::-webkit-scrollbar-track{
   background: #eee; /* cor do fundo */
}
.scroller::-webkit-scrollbar-thumb{
    background: blue;  /* cor do scroll */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Role a tela
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
    
04.06.2018 / 05:11