How to change style of elements on scroll using jQuery ? Last Updated : 11 Jun, 2020 Comments Improve Suggest changes Like Article Like Report We have given an HTML document containing some CSS properties, and the task is to change the CSS property to a particular element after scrolling the page using jQuery. To change the style of element on scroll, we get the number of pixels by which the content of an element has been scrolled horizontally or vertically. The following example illustrates how to change the style of elements as they are scrolled. One way to change the style of elements as they get scrolled is to change the class to which the elements belong. Example: HTML <!DOCTYPE html> <html> <head> <title> How to change style of elements on scroll using jQuery? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <style> .classinitial { height: 200px; background-color: rgba(255, 255, 255, 0.5); position: fixed; top: 200; width: 100%; transition: all 0.5s; background-clip: border-box; border-width: 5px; border-style: solid; } .classfinal { height: 100px; background-color: rgba(0, 0, 0, 0.8); position: fixed; top: 200; width: 100%; transition: all 0.5s; border-width: 8px; border-style: solid; } .wrapper { height: 2000px; padding-top: 200px; color: green; text-align: center; font-size: larger; font-weight: bold; } </style> </head> <body> <header class="classinitial"></header> <div class="wrapper"> Geeks For Geeks </div> <script> $(function () { var header = $(".classinitial"); $(window).scroll(function () { var scroll = $(window).scrollTop(); if (scroll >= 155) { header.removeClass('classinitial') .addClass("classfinal"); } else { header.removeClass("classfinal") .addClass('classinitial'); } }); }); </script> </body> </html> The above code adds style to both the initial class and final class (called classinitial and classfinal according to the code) with the help of CSS styling properties. Below is the JavaScript code that helps to change the class of the header element present in the html code. The jQuery code flips the class of the header element from classinitial to classfinal if the page is vertically scrolled by 155 pixels. And it flips the class from classfinal to classinitial otherwise. For this purpose the jQuery methods addClass() and removeClass() are used in the above code. Here the scrollTop() function is used to get the number of pixels by which the element is scrolled and is it saved inside the variable named scroll. Output: Comment More infoAdvertise with us Next Article How to change style of elements on scroll using jQuery ? M MugdhaBehere Follow Improve Article Tags : Web Technologies JQuery jQuery-Questions Similar Reads How to scroll the page up or down using anchor element in jQuery ? The problem is to include a slide effect whenever we click a local anchor and we want to scroll up or down the page accordingly. Earlier we can do it natively by using CSS property. Syntax: a { scroll-behavior: smooth; } Now with the help of jQuery, we can do it by using the following two methods: j 2 min read How to scroll to specific element using jQuery ? Many times, in our website we want to scroll automatically to a section of the webpage when we click on a button or a heading in a navbar or a list. So, to achieve this automatic scrolling to the required element, we need to take the help of jQuery. Using jQuery, we can achieve this in a very simple 3 min read How to change cursor style using jQuery ? The cursor style is used to specify the mouse cursor to be displayed while pointing on an element. Cursor Value: alias: This property is used to display the cursorâs indication of something is to be created. all-scroll: In this property, the cursor indicates scrolling. auto: This is the default prop 5 min read How to Get and Set Scroll Position of an Element using JavaScript ? In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript.Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties.We create an HTML div element with an id of 3 min read How to animate scrollTop using jQuery ? The scrollTop property in JavaScript is used to set or return the vertical scrollbar position of any element. Setting the scrollTop to any value will make the page scroll to that location. The scroll by default takes place immediately and abruptly scrolls to the value. This scrolling can be animated 3 min read Like