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= </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:

Recommended Posts:
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- Hide scroll bar, but while still being able to scroll using CSS
- How to change cursor style using jQuery ?
- How to detect and change the content/style of a div using jQuery ?
- How to remove CSS style of <style> tag using JavaScript/jQuery ?
- How to scroll automatically to the Bottom of the Page using jQuery?
- Scroll to the top of the page using JavaScript/jQuery
- How to scroll window using jQuery ?
- How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
- How to scroll automatically to a particular element using JQuery?
- How to scroll the page up or down using anchor element in jQuery ?
- How to scroll to specific element using jQuery ?
- jQuery | scroll() with Examples
- How to determine the direction of a jQuery scroll event?
- How to change style attribute of an element dynamically using JavaScript ?
- How to change the style of alert box using CSS ?
- How to change the font style of a text canvas using Fabric.js ?
- How to change corner style of a canvas-type text using Fabric.js ?
- How to change font style using drop-down list in JavaScript ?
- How to change selected value of a drop-down list using jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


