Changing classes upon scrolling using waypoints

I am currently experimenting with adding a unique class to individual sections using jquery waypoints. The goal is to apply the 'fixed' class when a section reaches the top of the viewport, and then remove it for the previous section.

To maintain the height of the fixed div within the viewport, I have included a push element between each section.

The desired effect is similar to a reverse full-page curtain reveal.

For reference, you can view the implementation in this fiddle.

Markup

<section></section>
<div class="push"></div>
<section></section>
<div class="push"></div>
<section></section>
<div class="push"></div>
<section></section>

CSS

section {
height: 100vh;
z-index: 2;
}

.fixed {
position: fixed;
top:0;
width: 100%;
}

.push {
width: 100%;
height: 100vh;
position: relative;
z-index: 1;
}

Jquery

$('section').each(function(){
        var sectionElement = $(this);
        var inview = new Waypoint.Inview({
            element: sectionElement[0],
            entered: function(direction) {
                if (direction === 'down') {
                    $('section').removeClass('fixed');
                    sectionElement.addClass('fixed');
                    sectionElement.prev().hide();
                } else {
                    sectionElement.removeClass('fixed');
                    sectionElement.prev().addClass('fixed');

                }
            },
        });
    });

Answer №1

Here's a way to accomplish this:

.push {
max-width: 100%;
min-height: 100vh;
position: absolute;
z-index: 2;
}

Click here for the example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with MVC 4 Asynchronous File Upload: Controller Always Receiving Null Value

I'm encountering an issue while attempting to upload a file asynchronously from ajax to my controller. I am passing 3 variables - PictureId, PictureName, and PictureFile. The problem specifically lies with the "PictureFile" variable as it consistently ...

The functionality of jQuery Repeater is not functioning properly within a partial that was generated via ajax request

While working on a form with multiple steps generated in ajax within a single page, I encountered an issue with using jquery.repeater in one of the intermediate steps. The code specific to the repeater is encapsulated in a dedicated partial. However, I f ...

What is the best way to add color to a Table Header?

I'm working on a HTML/CSS table and I want the header row to have a specific background color. Below is my code: <table class="contentTable"> <tr class="contentTableHeader"> <th>No.< ...

You cannot nest a map function within another map function in React

Having some trouble applying the map function in HTML using React. Below is the code snippet: response = [ data : { name: 'john', title: 'john doe', images: { slider: { desktop: 'link1', mo ...

Preventing the window from resizing while an AJAX query is in progress

I have a script in jQuery that serves as a search tool and also has the capability to resize an element to match the screen height minus 40 pixels whenever the window is resized. However, I am looking for a way to turn off the resizing feature when a searc ...

Tips for utilizing code submitted by a user to extract meaningful errors

As I work on developing a mod for the online game Cookie Clicker, I've implemented a feature that allows users to input their own code to enhance the functionality of my mod. However, before users save their code using my custom editor, I want to run ...

Styling ReactJS Components with CSS Styling

I am a complete beginner to React and I seem to be facing an issue with rendering CSS properly. The react class, App, that I have should display a Card with a progress tracker on it. var App = React.createClass({ render: function () { var pushNotific ...

Activate the ion-navbar button when submitting from the modal page

import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import {ModalPage} from '../modal-page/modal-page'; @Component({ selector: 'page-page2', templateUrl: 'pa ...

What could be the reason for my ng-init not functioning properly?

<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}"> Using JSP, I set initial values in the body tag. However, in the controller, I have the following code: console.debug($scope.user + " "+$scope.month} The issue is that only ...

Is it possible to create a webpage where the header and footer remain static, while the content in the body can be dynamically changed

I am in the process of creating a webpage where main.html will load a page with a static header and footer that remains unchanged when navigation links are clicked. I'd like to achieve this using Javascript, with the body content being sourced from an ...

Exploring Array Values within Array of Objects for Specific Value

Here is an array of emojis for you to consider: allEmojis = { "Smileys-People": [ { "index": 1, "display": true, "name": "grinning face", "sh ...

Optimal approach for vertically aligning elements in CSS

I'm looking to arrange my header ('Sail away today with Starboard Rentals.') and link buttons on top of each other. I want the navigation buttons to be positioned below the h1 on the lower half of the 'home-jumbo' div. What's ...

What are some methods for incorporating functions and libraries from Node.js into my JavaScript code?

Does anyone know how to connect functions from Node.js to JavaScript? I am attempting to use the mongoose library in JavaScript, but the browser does not support the "require" function. Can someone please assist me with this? ...

Dividing a string into an array and displaying it in a table using Laravel

Retrieving a string from the database and using the explode function to split the values. Below is the code snippet: $data = DoctorRegistration::select('products') ->where('doctorid','=',$doctorid) ->get(); ...

Invoking a plugin function within a page function results in failure

After setting up a plugin named helper.js inside plugins/ directory, I encountered an issue where the plugin's functions did not work when called inside another function. Below is my code snippet for helper.js: import Vue from 'vue' imp ...

Access exclusive content by subscribing now!

How can I return a reference to a subject from a service without allowing the receiver to call .next() on the subject? Let's say there is a service with a subject that triggers new events. class ExampleService { private exampleSubject = new Subjec ...

Can one access the method definition within a Visual Studio Code setup for an AngularJS project?

I'm on a quest to locate the method definition within my AngularJS project, but alas, I am struggling to discover a quick and easy shortcut for this task. My attempts with Ctrl + Click only led me to the initial occurrence of the variable's decla ...

Is there a way for me to detect variations in the input and subsequently alter the text?

I'm facing a situation with my HTML code: <label for="date">START DATE: <input id="date" name="date" type="number"> <span>days</span> <label> I am looking for a way to change the text inside the span element based o ...

Refresh the data table following the submission of a form

I've been looking around, but I haven't found a solution that works for me. I have a datatable with an edit button in one of the columns. When a user clicks on the edit button, a modal pops up allowing them to change an entry in the datatable. Af ...

What could be causing an undefined error when running Javascript with Python and Selenium?

My goal is to utilize Javascript for retrieving a table body element on a webpage. Upon executing the script immediately, I receive an undefined response. However, if I introduce a few seconds delay, it functions correctly. def fetch_row_list(browser): ...