Creating an animated sidebar that moves at a different speed than the rest of the

I have a layout with a large sidebar and small content, where the "Fixed part" refers to sticky positioning.

I'm attempting to achieve this using scrollTop, but the sidebar is causing some issues like this:

The code should only be executed when the height of the main content is less than the sidebar content height.

This is the code from my latest attempt:

function sidebarParallax(expertStatisticsHeight) {
        var sidebar = $('aside.site-aside');
        var main = $('main.site-content');
        var footer = $('footer');
        var c = 1;

        sidebar.css({
            position: 'absolute',
            right: 0,
            width: $(document).width() - main.width(),
        });

        if ((expertStatisticsHeight + $banner.height()) > main.height()) {
            var speed = c - (main.height() / sidebar.height());

            sidebar.css('transform', 'translateY(' + -speed + 'px)');
        } else {
            sidebar.removeAttr('style');
        }
    }

How can I ensure that when I scroll to the bottom, the bottom of the sidebar aligns with the bottom of the content?

Thank you in advance!

Answer №1

Finally cracked the code!

let sidePanel = $('aside.site-aside');
let mainContent = $('main.site-content');
let banners = $('.section-banner');
let bannerCount = banners.length;
let isMainSmallerThanSidePanel = initialize();

$(window).resize(checkDimensions);

$(document).ajaxComplete(checkDimensions);

$(window).scroll(function() {
    if (isMainSmallerThanSidePanel) {
        return moveSidePanel();
    }
});

function checkDimensions() {
    isMainSmallerThanSidePanel = initialize();
}

function moveSidePanel() {
    let scrolled = window.pageYOffset || document.documentElement.scrollTop;
    let scrollDifference = mainDistance - scrolled;
    let percentage = scrollDifference / mainDistance;
    sidePanel.css('top', -distance + (distance * percentage));
}

function initialize() {
    sidePanel.css({
        position: 'absolute',
        right: 0,
        width: $(document).width() - mainContent.width()
    });

    let isMainShorter = sidePanel.height() > mainContent.height();

    if (isMainShorter) {
        distance = sidePanel.height() - mainContent.height();
        mainDistance = $('main.site-content').height() - document.documentElement.clientHeight;
        moveSidePanel();
    } else {
        sidePanel.removeAttr('style');
    }

    return isMainShorter;
}

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

Leveraging ng-click and ng-hide/show directives within ng-repeat in Angular

I have a simple Single Page Website built with Angular. I utilized ng-repeat to generate content on the main page, where each item is an image. I am looking to create an alternate view for each image to display more details. I have implemented this feature ...

Difficulty encountered while integrating chart.js with Django using npm

Encountering an issue while using Chart.js in my Django project - when utilizing the NPM package, it fails to work. However, switching to the CDN resolves the problem seamlessly. Chart.js version 3.9.1 Below is a snippet from my project's index.html ...

Increasing the values of jQuery variables

I've written this code to assign different ids to variables, as demonstrated below: var numgrid = jQuery('.panel-grid').length; for (var i = 0; i < numgrid; i++) { var currentId = "#pg-"+id+"-"+i; if (jQuery(currentId)[0]) ...

Displaying entries of input data

I have an application that includes a modal window with filters, but I am looking to add another type of filter. However, I am struggling with implementing it in React and would appreciate any help with the code or recommended links. Essentially, I want t ...

CSS animations for loading page content

Currently, I am incorporating animations into my project using HTML5 and CSS3, and the progress has been smooth. I have been able to achieve effects such as: #someDivId { position: absolute; background:rgba(255,0,0,0.75); transition: all 0.7s ...

Tips for choosing nested elements with basic CSS selectors like nth-of-type or nth-child for Selenium

Is there a way to select the paragraph tag for B (or C) by utilizing nth-child or nth-of-type in Selenium WebDriver? <tr> <td> <p class="myClass">A</p> </td> </tr> <tr> <td> <p ...

store events in a MySQL database using a servlet callback and display them on a full calendar

Hey there! I recently started using the full-calendar jQuery plugin and successfully integrated it into a JSP page. The events on the calendar are loaded from a MySQL database by calling a servlet that generates a JSON array of retrieved elements. Now, I& ...

Updating a query parameter with jQuery

Looking for a way to modify the value of a query parameter in a URL using jQuery. There are two example URLs: www.domain.com/?id=10&name=xxx and www.domain.com/?name=xxx&id=10 I want to update the id parameter to something like www.domain.com/ ...

Revolutionary Notification System - Harnessing the Power of Cutting-Edge notification

My system is working well, but I'm facing an issue with the GET method. It keeps using the same notification_id from the while loop for every search, resulting in repetitive requests. Here's an example of what's happening: jquery....486299 ...

How can I access the id_lang variable in React JS from outside its scope?

How do I access the 'id_lang' variable outside of the render function in order to pass it down for checking? const Navbar = () => { const getID = async (id) => { let id_lang = id; console.log(id_lang); } ret ...

Aligning a table at the center of another table

Frustration has been brewing within me all week as I grapple with the task of sending out an important emailer soon. The challenge lies in aligning these product images next to their descriptions at the center, a feat that seems impossible to achieve withi ...

The Npm generate script is failing to create the necessary routes

I've integrated vue-router into my nuxt project, but I encountered an issue when running npm run generate - it generates everything except for my pages. I suspect the problem lies with the router implementation as I didn't face any issues before ...

Struggling to display a chart using angular-chart

I am facing an issue with rendering my chart. I have followed the instructions provided on the GitHub page of angular-chart.js. I have created a plunker to showcase my problem: http://plnkr.co/edit/x7XJhxxvYMzWr3u7lBcJ?p=preview Although I can access and ...

What about a Material UI-inspired homepage design?

Looking at the demo image on the main page for Material UI has me really impressed. I am interested in using a similar theme with MUI for my web application. Can you advise me on how I can achieve this look, or do I have to start from scratch? https://i.s ...

highlight the selected option in the ng-repeat list of items

Looking for some assistance with a coding problem I'm having. I keep running into an error when trying to make a selected item in an ng-repeat list highlight. The CSS style is only applied on the second click, not the first. Additionally, I need to en ...

How to format numbers with commas in AngularJS to make them easier to read

One of my variables looks like this: $scope.numbers = 1234567. When I apply the filter {{numbers| number : 0}}, the result is 1,234,567. Is it possible to get a result like 12,34,567 instead? Thank you in advance. ...

Achieving data synchronization and sequencing with AngularJS promises at a 1:1 ratio

Concern I am facing challenges with AngularJS promise synchronization and sequencing as my app expands across multiple controllers and services. In this scenario, I have an articles controller named ArticleController along with a related service named Art ...

Steps for removing an item from an array using lodash

I have an array containing objects and I am looking to remove specific items based on certain conditions. Can someone guide me on how to achieve this using the lodash map function? Example: [{a: 1}, {a: 0}, {a: 9}, {a: -1}, {a: 'string'}, {a: 5 ...

Learn how to instruct ajax to fetch the designated information and retrieve corresponding data from the database based on the selected criteria

Looking for some help with my 2 select boxes. The first box allows users to choose a brand, while the second box should display products from that brand fetched from the database. Unfortunately, I'm not familiar with AJAX and the script provided by a ...

Is each individual character displayed on the web page twice when using a controlled component?

Currently, I am diving into the extensive React documentation. A particular section caught my attention - controlled components. It delves into how form elements, such as an <input>, manage their own internal state. The recommendation is to utilize c ...