What is the best way to prevent content from jumping around on a webpage?

After implementing a sticky header on scroll, I encountered an issue. When scrolling down by 1000px, the 'sticky' class is added to the header causing a slight jump in the content. This may be due to the header no longer visually existing in that location within the markup.

I've tried adding a specific height to the header and wrapping a div around the container to keep the 'sub-header' in place, but unfortunately, these attempts have been unsuccessful. How can I resolve this issue?

View live code example: http://codepen.io/anon/pen/qbaQvL?editors=110

Answer №1

To activate the sticky feature, make sure to also adjust the margin-top of the about element to match the height of the sticky section.

Here's an example of how to do this:

if (windowPos >= content) {
  $('#sub-header').addClass('sticky');
  $('#about').addClass('sticky-active');

} else {
  $('#sub-header').removeClass();
  $('#about').removeClass();
}
#about.sticky-active {
  margin-top: 96px;
}

I have also created a functioning version on Codepen. You can view it here.

Answer №2

To achieve a sticky header, simply apply padding to the body element:

JavaScript logic:

if (scrollPos >= headerHeight) {
  $('#header').addClass('sticky');
  $('body').addClass('scrolled');
} else {
  $('#header').removeClass();
  $('body').removeClass('scrolled');
}

CSS styling:

body.scrolled {
  padding-top: 60px;
}

Answer №3

To create a fixed header while scrolling, you can use absolute positioning:

...
#sub-header {
    position: absolute;
  width: 100%;
}
#sub-header, #sub-header.sticky {
    background: #FFF;
    border-bottom: 1px solid #f0f0f0; 
    z-index: 2000;
}
...

Additionally, remember to add padding to your sections for proper spacing:

section {
  padding-top: 96px;
}

Check out this link for a live example: http://codepen.io/anon/pen/PZGxMZ

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

How to set an offset for an anchor point in JQuery on a div

I'm having trouble getting my link to navigate to a specific anchor point on another webpage. The page I'm linking to has a fixed menu with CSS positioning that covers the anchor point, so I need to offset the positioning of where the 'top&a ...

substitute bullet point list item with new element

Assuming I have a list: <ul class="ul-1"> <li class="li-1">xx -1</li> <li class="li-2">xx -2</li> <li class="li-3">xx -3</li> </ul> Then I store it as: var list = $('.ul-1').html(); In ...

Successfully resolving the API without encountering any response errors, even after sending a response

Once the data is successfully saved in the database and the image upload is completed, I am attempting to send res.json. However, I keep encountering the error message API resolved without sending a response for /api/auth/registeration, this may result in ...

How can require.js be effectively used in conjunction with web workers?

As of now, my current project involves the task of transitioning an existing web application to require.js. Most aspects of the process are proceeding smoothly, except for the functionality that utilizes web-workers. One such example is a worker defined in ...

Implementing a PHP back button to navigate through previous pages

Currently, I am in the process of coding a project. In this project, there are a total of 10 pages, each equipped with back and forward buttons. However, when I attempt to navigate using either javascript:history.go(-1) or $url = htmlspecialchars($_SERVER ...

Using jQuery to create transitions when a class is changed and adding a delay to the transition

If you want to check out the section I created on CodePen, feel free to visit it by clicking here. I've noticed that my JavaScript code has a lot of repetitive elements and I'm looking to optimize it for better performance. Any advice on how I c ...

Positioning an absolute div inside a relative div within a v-for loop while using VueJS and Element UI

I am experimenting with rendering a list of <el-card> items using a transition-group. Each card has a front and back side that flip when a button is clicked. To achieve a smooth flip transition, I need to apply the style: position: absolute; top: 0; ...

Node.js socket.io emit function not functioning properly

In my current configuration, I have the following setup: app.set('port', process.env.PORT || 3000); var httpserver = http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' ...

Dynamic Loading of CSS Styles

My style-sheet is saved in this unique location: /opt/lampp/htdocs/project/core/styles/Style.css To load the CSS file using the full directory, considering that the files utilizing it are situated here: /opt/lampp/htdocs/project/client/ The ultimate ob ...

Is your jQuery TextEditor not functioning properly?

Having some trouble integrating the jQuery TextEditor plugin into my CodeIgniter project. I'm puzzled as to why it's not functioning properly. Just a heads up, my project also utilizes Bootstrap. Can anyone shed some light on why the plugin is fa ...

Labels arranged in a fixed width format

Is there a way to ensure that the labels for the inputs are all the same width, creating a more cohesive table-like layout? Imagine it as two columns: one for the labels and one for the inputs. Here is the code I have so far: th, td, table{border: 2px ...

Table featuring identical submit buttons in each row: initial submit button is nonfunctional (potential issue with multiple identical IDs)

My table displays product files, with the option to add notes. If a note is added, it shows in a row. If not, a text area field with a submit button appears instead. Everything seems to be working well, except for the first row without a note. After addin ...

"Spinning Effect in CSS with a Pulsating Twist

I am attempting to create a unique CSS animation that involves rotating an image and giving it a pulsing effect similar to the animation seen on Shazam's button. Here is the code that I have so far. While the image is successfully rotating, I am runn ...

Having issues with my jQuery getJSON request. It's returning an empty response even though

I have been struggling to find a solution to this particular issue with no luck... Here is the jQuery getJSON request that I am working on: $.getJSON("http://localhost:8080/context/json/removeEntity.html", { contentId : 1, entIndex : entityIndex ...

Is there a way to execute this process twice without duplicating the code and manually adjusting the variables?

I'm looking to modify this code so that it can be used for multiple calendars simultaneously. For example, I want something similar to the following: Here is what I currently have: This is my JavaScript code: var Calendar = { start: function () ...

What is the best way to ensure that the checkbox is not affected when you click on the area?

If the user interacts with the checkbox, I don't want the handleClick function to execute. Is there a way to exclude it or prevent the click event from triggering? <div ... onClick={this.handleClick}> <div> some content here < ...

Retrieve the element's name with jQuery

In my form, there is a select element with the name field_p_payment[value] and I am trying to reset this select box. I attempted to do this by using the code below: document.getElementsByName("field_p_payment[value]").selectedIndex='0' Unfortun ...

Locate a specific value within an array of objects and display it

Data : const areas = [ { "ID":"4131", "RID":"f1438b", "Name":"City" }, { "ID":"6266", "RID":& ...

Making a jQuery AJAX request for JSON from a separate domain

My goal is to send an AJAX request to , expecting JSON data in return (not sure if JSONP) However, when I click on the button to make the AJAX request, I encounter this issue: http://prntscr.com/8xswr1 (Google Chrome Console) Upon double-clicking ' ...

Alter the border hue of the checkbox and radio button

Is there a way to modify the border color of checkboxes and radio buttons (both circle and rectangle) using CSS? I've attempted several methods with no success. Can someone provide guidance on this? Any advice would be greatly appreciated. Thank you ...