Enable scrolling-triggered animations

I need to add animation to my header when the user scrolls on the page. Here is the code I am using:

$(document).scroll(function () {
var value = $(this).scrollTop();
if (value > 150) {
    $( "body" ).addClass( "scroll" );
    $( "header.head" ).animate({top:'-15px'}); }
else {
    $( "body" ).removeClass( "scroll" );
    $( "header.head" ).animate({top:'0px'}); }
    }
});

When the user reaches the Y coordinate of 150, the body receives a new class called ("scroll") and the header moves up by -15px with animation.

However, I am facing an issue where if I try to reset the header position in the else statement like this:

$( "header.head" ).animate({top:'0px'}); }

the script stops working altogether. How can I fix this issue?

Answer №1

An additional curly brace '}' was inadvertently included, and the stop() function should be used as shown in the code below

Give this a try

$(document).scroll(function (e) {
var value = $(this).scrollTop();
if (value > 150) {
    $( "body" ).addClass( "scroll" );
    $( "header.head" ).stop().animate({top:'-15px'}); 
 }
else {
    $( "body" ).addClass( "scroll" );
    $("header.head" ).stop().animate({top:'0px'});
 }

});

I hope this solution is beneficial to you. Thank you.

Answer №2

There seems to be a syntax error with an extra }

$(document).scroll(function () {/*begin anonymous function*/
    var value = $(this).scrollTop();
    if (value > 150) {/*start if statement*/
       $( "body" ).addClass( "scroll" );
       $( "header.head" ).animate({top:'-15px'}); /*end if*/}
    else {/*else statement begins*/
       $( "body" ).removeClass( "scroll" );
       $( "header.head" ).animate({top:'0px'}); /*end else*/}
    /*end of the anonymous function*/}
/*extra closing brace*/});

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

Guide to integrating search and clear icons into the bootstrap search bar

I am working on a project that requires me to add search and clear icons to a Bootstrap search box, similar to the design shown in the image below: Does anyone have suggestions on how to achieve this? ...

Tips for customizing the appearance of popup windows

I want to enhance the appearance of my popup window by applying a different format for opening it. How can I style it so that it looks visually appealing when the popup window opens? You can find below the source code I am working with: HTML: <div onM ...

When a checkbox is clicked, the click event will result in the checkbox becoming unchecked

Hello and thank you for taking the time to provide your advice on this matter. I am facing an issue with a page that contains a gridview featuring checkboxes in both the HeaderTemplate and columns below. The checkboxes within the columns are assigned a cl ...

Spread out the items within an inline block

Is there a way to create space between icons that are in an inline block without them touching each other? One solution could be to add a container around each icon, center the icons within the containers, and then place the containers in the block. You c ...

"Addressing the issue of white space between the status bar and header in Cordova

I have tried numerous solutions but I am still unable to remove the white space between the header and status bar, as shown in the attached image. https://i.sstatic.net/a9xTG.jpg My current setup involves using Intel XDK version 3987 with Cordova plugin ...

Creating a Triangular Button with Icon Design with Bootstrap 4

I'm attempting to design a CSS triangular button that is positioned in the top left corner of a div using Bootstrap 4. Here's my concept as crafted with Photoshop: https://i.sstatic.net/cbUWL.png However, after numerous attempts, this is the c ...

The CSS float property and text alignment only apply when elements are displayed inline

I am facing an issue with my ASP .net core 2.1 app. I have customized the scaffolded crud details page, but I am encountering hurdles with the dl, dt, and dd elements. The text inside dt elements, which display the field names, is appearing next to them o ...

Attempting to perform a cross-domain POST request in Internet Explorer 9

I've been attempting to execute a cross-domain POST request in IE9, and here's the code I'm using: $.support.cors = true; var data = {"userid":uid,"email":email,"password":password}; if (isIE () && isIE () <= 9) { $.ajax({ ...

Determine if an element is being hovered over using jQuery

How do I determine if the cursor is hovering over an element using jQuery or JS? I attempted to use $('#id').is(':hover'), but it doesn't seem to be functioning as expected. It's worth mentioning that I am calling this line ...

Guide to dynamically add tr element using CSS to a table

While using the $.each function in jQuery, I am retrieving data and adding it to a table in the following manner: $.each(segment, function (index, innerSegment) { var tr; tr = $('<tr/>'); tr.append("<td>" + segment[i].Airline.Air ...

Stack pictures on a slender DIV

I am trying to create a vertical line inside a DIV container. After that, I want to add an image on top of the vertical line (see attached picture for reference). This is what my current source code looks like: <div style="background-color:gray;width ...

Dealing with Unwanted Navbar Opacity in Bootstrap 4

Currently working on a React.js project using Bootstrap 4. The issue I'm facing involves two elements - navbar and alert, both implemented from Bootstrap4. Strangely, when setting position:fixed for both elements (overriding CSS), they become semi-tra ...

Flex items refusing to wrap in a flexbox with a column-direction orientation

In my code, I am using an outerWrapper, innerWrapper, and children. The outerWrapper has a fixed height of 300px and is styled with display: flex. Similarly, the innerWrapper also has a style of display: flex with flex-direction: column. However, when I a ...

Issues with AJAX file uploading functionality are preventing users from successfully uploading files

I need help with uploading text files via AJAX on a browser. I have tried implementing the code, but it seems like the file is not being uploaded successfully as var_dump($files) displays array(0). HTML: <form id="uploadsql" action="upload.js" method= ...

Exploring Ways to Implement Unobtrusive AJAX in ASP.NET Core Razor Pages Prior to Form Submission

I am attempting to prevent the button from being clicked when I send the ajax request. The only thing that seems to be triggered is the console.log. I initially thought that I could achieve this by using the beforeSubmit option in ajax, but Unobtrusive AJA ...

Achieving Bottom or Center Alignment for a Div within Another Div Using Bootstrap 5

I've been struggling to position my Title and CTA Button at the bottom (or center) of the main image div. I'm using Bootstrap 5.1 and have tried various d-flex methods without success. You can view my code on jsfiddle. Thank you in advance for a ...

Choosing a Element following the Execution of a jQuery appendTo Method

My goal is to dynamically add a set of elements to a div using jQuery and then reference them. It's strange that even though I can see these elements in the DOM in firebug, jQuery doesn't seem to recognize them. Here is the HTML code for the div ...

When looking for attribute values in jQuery, do you utilize quotation marks in your search?

Suppose I have a hyperlink structured as follows: <a data-btn="login"></a> If I desire to target this specific link using jQuery based on its data attribute, how should I proceed? var a = $("[data-btn='login']"); //Should I use th ...

Stylish dropdown menu design inspired by W3CSS website

I recently implemented a CSS dropdown menu example that I found on a website, but it's not working with my code. I double-checked for typos but couldn't find any. Even when I tried to directly copy the code and replace the content with my own, i ...

jQuery alert for AJAX form

I am experiencing an issue with my ajaxform. Within my project, I have a PHP file named captcha.php which is responsible for generating a captcha: <?php session_start(); $random_number = rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9); $_ ...