Turn off scrolling and animations temporarily before enabling scrolling once more

(Here's my first question, please be kind!)

I'm on a mission to disable scrolling, animate a div, and then re-enable scrolling. I've managed to achieve the first two tasks with flying colors, but unfortunately, getting it to scroll again has proven to be quite challenging.

Currently, I am utilizing lockScroll() and unlockScroll() functions created by the talented JeanValjean over at How to programmatically disable page scrolling with jQuery

Any assistance would be greatly appreciated. For a demo, please visit http://jsfiddle.net/Chris_James/1xxL5dnp/6/

jQuery(document).ready(function(){
$(window).scroll(function(){
    var p = $( ".testi" );
    var offset = p.offset();
    if ($(this).scrollTop() > offset.top - $(window).height()/2) {
        lockScroll();
        $('.testi').addClass( 'testishow' );
        setTimeout(function(){
            $('.testimonial').fadeIn('fast');
            unlockScroll();
        },700);
    }
})

});

Answer №1

When the jQuery document is ready, a function is called to handle window scroll events. This function checks if an element with the class "testi" is in view by comparing its offset position with the window height. If the element is within half of the window's height from the top, certain animations are triggered. The element with the class "testi" is given a new class "testishow" and then a testimonial section fades in gradually. After a delay of 700 milliseconds, the scroll lock is removed.

Answer №2

Following the suggestion by Good.luck, you can utilize callbacks to unlock scrolling (Although I was just a few seconds too late...). It seems unnecessary to declare a separate function solely for unlocking scroll.

The lock/unlockScroll() methods appear to be a bit cumbersome. Instead, I would recommend implementing cubbius's solution using an "overflow: hidden" style for the html element. Create a function based on your current scroll event and unlock it with:

$(window).off("scroll touchmove mousewheel", function () {
   $(window).on("click", yourScrollMethod);
})

Answer №3

In order to solve this issue, I simply added a class called "scrolllocked" to the if statement and checked for its presence using &&. It's as easy as that. Check out the solution here

if ($(this).scrollTop() > offset.top - $(window).height()/2 && !p.hasClass("scrollLocked")) {
        lockScroll();
        p.addClass("scrollLocked");
        $('.testi').addClass( 'testishow' );

Answer №4

If you want to wait for an animation to finish before executing a function, utilize function callbacks like so:

$('.box').slideDown('slow', function(){
    showText();
});

In this scenario, the showText() function will only be called once the slideDown animation is completed.

UPDATE: Check out this JSFiddle for a live 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

Obtaining text color in Selenium using Python

My objective is to extract the text color from each cell in a single column table. The HTML structure for a cell is as follows: <table class="table table-bordered table-hover"> <tbody> <tr> <td class="ta ...

Setting a static width for a specific cell in HTML5 - What's the best way to go about it?

When working with HTML5, how do I define a specific width for a table cell? Many of the <col> properties that were present in HTML4 are no longer applicable in HTML5. Just to clarify, I am aware that I can use <td style="width:150px">, but thi ...

Establishing a connection to an active process within Winappdriver with the utilization of JavaScript

As someone who is fairly new to working with JS and WinAppDriver, I am currently facing a challenge with testing a Windows-based "Click Once" application built on .Net. To launch this application, I have to navigate to a website through Internet Explorer a ...

jQuery's AJAX functions failing to trigger callbacks following a file upload

I am facing a challenge with an AJAX call that is designed to upload a file from an input form to a server: $(function(){ $("form").submit(function(){ var infile = $('#pickedfile'); var actFile = infile[0].files[0]; ...

Prevent Loading of Nested States in UI-Router

Imagine having two states configured using angularjs ui-router: .state('branding', { url: "/{branding}", controller: 'BrandingCtrl', templateUrl: '/app/Branding.html' }) .state('branding.index', { ur ...

django.http.request.RawPostDataException: It is not possible to retrieve the body content once the request's data stream has been read

I encountered an error while making an API, it's showing django.http.request.RawPostDataException: You cannot access body after reading from the request's data stream. I believe you can't access data a second time. Here is my code: def get_p ...

What is the best way to create an input field along with a span element using the :before pseudo class?

My first question on StackOverflow is regarding adding a pseudo-class before an input type button for animation (http://codepen.io/anon/pen/emodKG). I understand that it's not possible to directly add something before an input, so I attempted to inclu ...

What are the steps to lift non-React statics using TypeScript and styled-components?

In my code, I have defined three static properties (Header, Body, and Footer) for a Dialog component. However, when I wrap the Dialog component in styled-components, TypeScript throws an error. The error message states: Property 'Header' does no ...

Pictures are not aligning correctly within a div element on Safari

When viewing the images in the images div on Chrome, Firefox, and IE, they appear to be vertically aligned and centered properly. However, there seems to be an issue with Safari as the pictures are being pushed below or out of the images div, causing them ...

Handling Exceptions in Node.js and Express

Having recently delved into the world of javascript and node, I've been working on creating an application using node.js and express. While I've implemented appropriate error callbacks in my code, there are instances where the node.js server abr ...

Troubleshooting Floating Divs in ExtJs TreePanel Component

I am currently utilizing the treepanel object in ExtJs and I am trying to implement two divs side by side within the treepanel items. Is it feasible to have one of them set with a fluid width and the other with an auto width? Despite my HTML and CSS codes ...

What is the manual way to activate Ratchet's push feature?

I am facing an issue with a link that triggers a search function. Currently, the link is working as expected. However, I am having trouble getting the search to be executed by pressing the 'enter' button. Here is my code snippet: $('#sea ...

Using C# Razor Pages to send checkbox values via AJAX requests

In my model class, I have the following: public class DataModel { public bool Display { get; set; } } After deserializing a FormData object to a JSON object, I am posting the value from a checkbox like this: this.FormToJSON = form => { const ...

The code signal's challenge to find the common character count is a fun

During my iteration through both arrays, I am successfully passing most of the tests. However, there is one test that presents a challenge as it continues to nest the loop against the first loop even after the matched element has been removed. Input: s1: ...

The uppermost part is malfunctioning

Below is the snippet of code: $('> li', this).each(function (index) { var top_space = $(this).css('padding-top'); $(this).prepend('<div></div>'); $('> div', this).css({ position ...

Using jQuery's .live('click') method will only bind to the initial element found on the webpage

I developed a custom jQuery plugin that attaches to 8 different elements on a webpage. Within each element, I intended to utilize the .live() method to bind a click event to a link, triggering a form submission via ajax. However, I encountered an issue whe ...

Exploring the World with AngularJS and Google Maps API through ngGeolocation

Having some difficulty displaying my geolocation on Google Maps API using a marker. I am utilizing the ng controller ngGeolocation and the http://angular-ui.github.io/angular-google-maps/ Previously, I hardcoded the marker and map location without any is ...

Adjust the color of the bootstrap navbar upon resizing the screen

Attempting to modify the background color for the navbar menu items on smaller screen sizes. An issue arises in the following scenario: Browser window is resized until hamburger menu appears Hamburger menu is clicked to display menu i ...

The ng-class styling isn't being implemented

I am facing an issue with updating the text color inside a div tag based on values from an array within an ng-repeat. While the value is correctly set during debugging, the color does not display properly. <div ng-class="sel.fav == 'Y' ? &apo ...

Ensuring Form Submission is Successful: A Guide to JQuery/Ajax Form Validation and the Importance of Validating Forms and Ajax Operations

I’m currently facing an issue with the form validator I'm using and need help resolving it. The form validator in question can be found at http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/. My problem ...