Scroll-triggered div translation

Within this nested structure, I have a mysterious div with an unpredictable height and another one set to 125px in height.

My goal is to transform the second div into a sticky element that remains fixed within its parent container only.

The grey box represents the container, and adjacent to it is a social media div that needs to maintain its stickiness.

https://i.sstatic.net/cYAVp.jpg

Since more content will follow after the container, utilizing 'position: fixed' won't work. My attempts using 'position: absolute' along with adjusting 'top' value or applying 'transform: translate' resulted in jittery behavior on Chrome.

Here's the code snippet I experimented with:

$offset = $(".social-media").offset().top;
$containerHeight = $(".sticky-container").height();
$bottom = $containerHeight + $(".sticky-container").offset().top;
$maxPoint = $containerHeight - $(".social-media").height();
$(window).scroll(function(){
    if($(window).scrollTop() >= $offset){
        if($(window).scrollTop() >= $bottom){
            $(".social-media").css({transform: "translate(0px,"+$maxPoint+"px)"});
        }else{
            $scroll = $(window).scrollTop() - $offset;
            $(".social-media").css({transform: "translate(0px,"+$scroll+"px)"});
        }
    }else{
        $(".social-media").css({transform: "translate(0px,0px)"});
    }
});

Answer №1

It appears that the solution in the jsbin you shared is functioning smoothly without any jittering. The root of the problem could possibly be related to repaints being triggered by various elements on your website, rather than the code you have inserted. I recommend checking out Google's repaint optimization guidelines here, as it may assist you in pinpointing and resolving the issues causing the jittering effect.

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

Encountered a challenge when attempting to substitute styles using classes in material-ui

While working on implementing a table using the TableRow component from material-ui, I encountered an issue with customizing the background color when the "selected" property is true. The default theme applies a pink background-color in such cases, but I w ...

Blending the widths of native elements with those of Bootstrap columns

Having trouble aligning spans in a Bootstrap 3 form to degrade into a stack on mobile: From [station1] to [station2] at [time] From [station1] to [station2] at [time] While this code works to an extent: <div class="row"> <div class="col ...

Rendering in Three JS involves efficiently utilizing one buffer to display the output within itself

I have been struggling with a particular issue and I really need some assistance: In my three js context, I have created a custom material and rendered it into a texture. ` /* Rendering in texture */ fbo_renderer_scene = new THREE.Scene(); fbo_r ...

Whenever I send an Ajax request in my web application, the controller in Laravel responds by providing me with a script within the request parameter

When making this ajax request: $.ajax({ url: "{{URL::to('admin/repcasetracker/getdiscount')}}", data: { serialnumber: serialnumberdata, }, success: function (data) { console.log(data); } }); Controller: public funct ...

What is the reason behind this being deemed as true?

Imagine we have this snippet of code: var attachRed = false; Why is attachRed = !attachRed equivalent to true? I'm curious because I'm working with Vue.js and trying to grasp why this particular piece of code functions as it does. <div id= ...

Encountering Routing Issues in Express.js Following Passport.js Authentication

My authentication setup with Passport.js is pretty straightforward. After the user successfully authenticates, I redirect them to /work like this. app.post('/login', passport.authenticate('local', { successRedirect: '/ ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...

How can I use Java Script to create animation of vertical black bars of a specific width moving across a white background?

Looking for a JavaScript code that can animate vertical black bars of a specific width moving over a white background. The desired outcome is similar to the video found at: https://www.youtube.com/watch?v=bdMWbfTMOMM. Thank you. ...

Preserving 'this' in jQuery function invocations

My goal is to transition from the following code: $("#btnminus").mousedown(function () { $(this).animate({width: "95%", height: "95%"}, 50); } to this revised version: function btnPressed() { $(this).animate({width: "95%", height: "95%"}, 50); } ...

What is preventing the bundling of my CSS into the application?

I'm facing an issue while setting up a new project using vue.js, scss, and webpack (with express.js on the server side and TypeScript). I copied over the configurations from a previous project where everything was working fine. According to my underst ...

How can I achieve a floating effect for a div element that is contained within another element?

I have a container located within the body of my webpage. This container has margins on the left and right, as well as a specified width. Inside this container, there is a div element that extends beyond the boundaries of the container to cover the entire ...

Issues reported with Adobe Air and Canvas functionality not functioning as expected

I am attempting to create a simple Air App using Dreamweaver CS5.5, but I am running into an issue with loading an image onto the Canvas element through a function. While I can successfully load the image onLoad, my goal is to draw the image on the canva ...

Tips for defining conditions within the style attribute in JSP

Below is the code that I attempted: <div style="width:85%;"> <input class="formbutt" value="AddNew" title="AddNew" type="button" style="{(${projectEnvironmentBean.divStyle}=='dipslay:none') ? 'display:block' :'display: ...

What's Next? Redirecting Pages in Node.js Express after Handling POST Requests

Is it possible to redirect to a different page from a post request? module.exports = function(app) { app.post('/createStation', function(request, response){ response.redirect('/'); //I'm having trouble getting ...

Is it possible to include parameters within a print() function in JavaScript?

Can anyone help me figure out how to add parameters to a print function? I only want to print a specific table, but when I try to print it, the entire page gets printed instead. Here's my code: let tableContent = document.getElementById('tablen ...

Parsley.js now supports the use of the attribute `data-parsley-errors

Currently, I am attempting to integrate the errors container (e.g., data-parsley-errors-container="#ErrorSummary") with parsley.js. Most aspects are functioning properly, but I've encountered an issue. Specifically, I am looking to solely adjust the ...

Extracting information from secured HTML

I am trying to extract data using a vb.net form from but the retrieved code contains some unnecessary white space, hiding the table I'm interested in. <th width="93">Факт. время прибытия</th> ...

Comparison of getComputedStyle() and cssText functionality between Internet Explorer and Firefox

Take a look at this example to see the issue in action. I'm attempting to access the cssText property of a <div> using window.getComputedStyle(element) (which provides a CSSStyleDeclaration object). While this works smoothly in Chrome, it' ...

Linking CSS to any page other than index.html will not be feasible

Hey everyone! I've run into a little problem that's driving me crazy, so I figured I'd reach out for some assistance. Here's the situation: In my file structure, I have a root file that includes index.html, /css/, /pages/, and /images/ ...

Optimizing Bootstrap popover responsiveness for elements with excessive length to ensure a smooth display experience

I am currently working on creating a popover that remains in position when hovered over, specifically for elements that are too large to fit on the screen. However, I am facing some issues with my current implementation: <template> <div class ...