"Implement a smooth scrolling animation on the page to automatically move a div element upward and lock

I am attempting to create a unique animation for a header div that involves moving it to the top of the screen when the user scrolls down. Once the header div reaches the top, I want it to stay fixed in that position. As the header div moves upwards, it will pass over a banner div. To enhance this transition, I have implemented a fading effect on the banner div based on the proximity of the header div to the top.

When the header div is fixed at the top and the banner div has completely faded out, I would like a reverse scroll action to trigger an animation that moves the header div back to its original position (margin-top: 400;) while simultaneously causing the banner div to fade back into view.

You can access my code on JSFiddle here: https://jsfiddle.net/xm33Laag/

check out the JSFiddle

Unfortunately, the functionality on the JSFiddle does not match that of my local version :S

While I can successfully move the header div to the top, I struggle to animate it back down. Additionally, I aspire for my animation to resemble the fluidity demonstrated in this example:

My preference is to refrain from using fullPage.js as I only require this specific function without the additional features it offers.

In the ideal example provided above, the animation initiates smoothly with a single scroll, lacking any initial jitters before the movement begins - a quality I truly admire!

Currently, my animation requires an initial scroll action to display 100px of abrupt movement before transitioning into the animated sequence. My aim is to seamlessly integrate these actions into one cohesive movement.

Thank you!

Answer №1

After experimenting with the layout, I decided to make some adjustments to the CSS by shifting the main content below the top margin instead of the .header. This approach made it easier for me to code.

For a live demonstration, you can check out this example: http://codepen.io/anon/pen/azrwog?editors=001

I also made significant changes to the JavaScript, here's the modified version:

var previous = 0, delta, initial = true, stopped;

$(window).scroll(function() {

clearTimeout(stopped);
var fromtop = $(window).scrollTop();
if (fromtop-previous > 0) delta = -1;
else delta = 1;
previous = fromtop;

if (fromtop < 400) {
var factor = 400-fromtop;
$('.banner').css({'height': factor, 'opacity': factor/400});
$('.header').removeClass('fixed');
if (delta == -1 && initial) firstScroll();
else if (delta == 1) scrollAction();
}
else $('.header').addClass('fixed');
});

function firstScroll() {
initial = false;
$('html, body').animate({scrollTop: 400}, 800);
}

function scrollAction() {stopped = setTimeout(function() {goTop()}, 200)}

function goTop() {
var topoffset = $(window).scrollTop();
$('html, body').animate({scrollTop: 0}, 2*topoffset, function() {
initial = true;
});
}

Additionally, I updated the solution to include smooth scrolling across different browsers using the mousewheel.js plugin:

You can view the updated version here: http://codepen.io/anon/pen/jEoGxG?editors=001

var fromtop, initial = true, stopped;

$(window).mousewheel(function(turn, delta) {

if (!initial) $('html, body').finish();

$(window).scroll(function() {

clearTimeout(stopped);
fromtop = $(window).scrollTop();

if (fromtop <= 400) {
changeOpacity();
$('.header').removeClass('fixed');
if (delta == 1) scrollAction();
}
else $('.header').addClass('fixed');
});

if (initial) {
if (delta == -1) $('html, body').animate({scrollTop: 400}, 800, function() {
initial = false;
});
return false;
}
});

function scrollAction() {stopped = setTimeout(function() {goTop()}, 200)}

function changeOpacity() {
var factor = 400-fromtop;
$('.banner').css({'height': factor, 'opacity': factor/400});
}

function goTop() {
var topoffset = $(window).scrollTop();
$('html, body').animate({scrollTop: 0}, 2*topoffset, function() {
initial = true;
});
}

An additional line of code has been added to prevent any 'sticking' behavior after the initial scroll:

if (!initial) $('html, body').finish();

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

Using the Jquery accordion function within google maps is limited to running only one time

After successfully creating a google maps page with markers generated from XML, I encountered an issue. The plan was to display event information in a div when a marker is clicked, along with attaching an accordion to the events data. Although the first cl ...

Is there a method in Discord.JS to remove an embed from a message sent by a user?

Currently, I am developing a bot utilizing the Discord.JS API. This bot is designed to stream audio from specific YouTube links using ytdl-core. Whenever a link is typed in, an embed of the YouTube video appears. While there are methods to disable embeds o ...

Encountering an issue with Next.js React SSR using styled-jsx: unable to access the 'state' property as

I've come across a problem that I can't seem to figure out. Despite my attempts to search for a solution here, I haven't been able to help myself. I'm new to javascript and react, so please bear with me. Issue: I'm using React (1 ...

What is the proper way to ensure a flex item takes up 50% of the space when a gap is included in

It appears that I have misunderstood something about flexbox and am struggling to resolve the issue correctly. In my current setup, I have a flexbox container with four columns as its direct children. I want each .flexbox-item to take up 50% of the contai ...

Showcasing interactive column titles by employing angularjs in an html table

After preparing my data, I aim to showcase it in an HTML table. However, a complication arises each time the $http service is called as it returns a varying number of columns (n). Essentially, I wish to have the first row of the data serve as column names, ...

Leverage Summernote in conjunction with Vue components to dynamically switch their locations

I am experiencing a strange behavior with Vue components created using v-for. Each component has a textarea for summernote, and initially everything works correctly. The editor loads the text and updates it when edited. However, when the position propert ...

JavaScript-Based Header Features Similar to Excel

Currently, I am in the process of developing a function that generates a sequence of strings similar to Excel headers. For those who may not be familiar with Excel, the sequence goes like this: A,B,...,Z,AA,...,AZ,BA,...,ZZ,AAA,...,etc. Here is the code ...

"Triggering ngClick causes ngSrc to update, however, the keydown event handler does not have

Currently, I am developing a dynamic live map application that includes various navigation buttons like pan and zoom. To enable keyboard control, I have implemented a keydown event handler that triggers the same function as the button clicks. Interestingl ...

What could be causing the malfunction of client components in NextJS 13.3.0 with experimental features?

My understanding of the beta documentation suggests that I need to include "use client" at the top of my client component in order for it to be defined as such. This allows me to use it within server components, leaves, or layouts. With that in ...

Troubleshoot: OffCanvas feature in Bootstrap 5 not functioning properly within navbar on small screens

I'm having trouble implementing Bootstrap 5 OffCanvas within the Navbar on laptop/desktop screens. It seems to only work properly on mobile screens with a size of sm or lower, displaying the hamburger menu. Any suggestions on how to make it functional ...

Apply a specific style to the initial element generated by *ngFor

My current method for displaying a certain number of * characters is utilizing the code snippet below: <div *ngFor="let line of lines; let i=index">*</div> I am interested in applying a margin only to the first element. The margin value sh ...

The banner may be cropped when viewing on lower resolutions such as mobile devices or when zoomed in

I've encountered a similar issue on a popular website, www.godaddy.com. When zooming in or accessing the site from a mobile device, the right side of the banner gets cut off. However, when viewed on a full-sized computer screen, there are no issues. M ...

Change the text of a button by using an input field

How can I dynamically update button text based on input field value? <input class="paymentinput w-input" type="tel" placeholder="0" id="amount-field"> <button id="rzp-button1" class="paynowbutton w-button">Pay Now</button> I want the bu ...

What is the best way to ensure the footer is always positioned at the bottom of each page

Hey there, I'm having an issue with printing a large table on my HTML page. I want the footer to appear at the very bottom of every printed page, but so far I haven't found a perfect solution. I came across using tfoot, which prints the footer at ...

Enhancing User Experience: Real-Time Preview in Text Area using Jquery

I have a code snippet here that allows me to insert images into a textarea and display a live preview when I click on a div. However, there is a small issue with the image not appearing instantly in the textarea; instead, I have to type a character for it ...

Utilizing Various Styles within a single Google Sheets Cell

In Cell A1, I have a challenge where I need to merge 4 different arrays of names, separated by commas. Each name should have its own styling based on the array it belongs to - red for array1, green for array2, orange for array3, and gray with a strike-th ...

The offspring of a React component

How can I select a specific div in children using React without passing it as a prop? I want to transform the code snippet from: <Pane label="Tab 1"> <div>This is my tab 1 contents!</div> </Pane> to: <Pane> <div&g ...

The min-height property in CSS appears to be ineffective on Jelly Bean

In my current project developing an Android application, I have incorporated Webviews in certain activities and fragments. However, I encountered a perplexing issue when running the app on Jelly Bean (api 16 and 18) emulator - the css property min-height ...

Guide to populating a full calendar using JSON information

Implementing the FUllCALENDAR CSS template for creating a meeting calendar has been my current project. The servlet class I am using is CalendarController. However, when running it, the output appears as follows: {"events":[{"id":1,"title":"1","s ...

Exploring ways to dynamically alter templates using the link function in Angular.js

Recently, I developed a directive called widget which functions similar to ng-view, but the template name is derived from an attribute. app.directive('widget', function() { return { restrict: 'EA', scope: true, ...