Create a smooth scrolling effect for a div with a fixed position

Having trouble scrolling a fixed position div on the page?

Are you using this jquery script:

$('html, body').animate({
    scrollTop: $(".example").offset().top
}, 750);

I had to set the body overflow property to hidden for another issue.

The problem: The above code doesn't work within a fixed position div. I need to scroll to an element inside a fixed positioned div.

<div class="overlay"> 
    <div class="example"></div>
</div>

.overlay {
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   bottom: 0;
}

I can't scroll to the example element inside the overlay div due to its fixed position.

Using the code below:

$('html, body').animate({
   scrollTop: $(".example").offset().top
}, 750);

It doesn't work within the fixed position overlay div.

Answer №1

The main issue lies in the fact that your scroll function is being called on html, body instead of the fixed element. To resolve this, you must set the overflow property to auto and activate the scroll animation on the actual element.

Here's how you can do it:

$(document)
.on('click', '#btnScr1', function(e) {
//IMPORTANT: call on the fixed element itself
$('#example').animate({ scrollTop: $(".exp").offset().top }, 750);
})
.on('click', '#btnScr2, #btnScr4', function(e) {
//IMPORTANT: call on the fixed element itself
$('#example').animate({ scrollTop: 0 }, 750);
})
.on('click', '#btnScr3', function(e) {
//IMPORTANT: call on the fixed element itself
$('#example').animate({ scrollTop: $('#example').prop("scrollHeight") }, 750);
})
#example { bottom: 0; left: 0; overflow: auto; position: fixed; right: 0; top: 0; }
.exp { text-align: center; }
p b i { background: yellow; color: red; padding: .5em 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="example">

...your content here...

</div>

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

Is it possible to pass an HTML element's id attribute to a function in JavaScript?

In the following code snippet, I am looking to send the id=content to the function mr and then display the result in the passed id=result. While this functionality is currently limited to this HTML file, I aim to extend it to other HTML pages by adding the ...

Differentiate between chrome and chromium with the help of Javascript programming

Can we differentiate between Google Chrome and the open-source Chromium browser using JavaScript? It appears that the navigator.userAgent property is the same in both browsers. ...

Particles.js fails to persist as I scroll down the HTML page

After creating a website page, I incorporated Particles.js for the background. However, when I scroll down, the particles seem to be confined to the initial corner of the screen and do not continue downward. Here are my current particle settings: #particl ...

Creating an Observable Collection in Angular using AngularFire2 Firestore

I am currently using material 2 and attempting to develop data tables with pagination and sorting. In order to achieve this, I require my collections to be observable. However, I believe that I might be incorrectly populating or initializing the arrays in ...

To activate a function, simply click anywhere on the body: instruction

One of my latest projects involved creating a directive that allows users to click on a word and edit it in a text box. Once edited, clicking anywhere on the body should return the word back to its updated form. html <div markdown>bineesh</div& ...

This React.Js application hosted on Heroku is optimized for use on Chrome Desktop browsers

Just finished completing a React project that retrieves News Articles and presents them. Recently launched it on Heroku, but I'm encountering an issue where only Chrome on Desktop seems to be able to run it. Both Safari and Firefox are showing the sa ...

Mastering the art of linking asynchronous callbacks based on conditions

I have a node.js express project where I need to create a switch-to-user feature for admin users. The admin should be able to enter either a username or user-id in a box. Below is the code snippet that handles this functionality. The issue arises when th ...

Toggling a div updates the content of a different div

I am looking to make the content within <div class="tcw-content"> dynamically change when a different div is clicked. I have little experience with Ajax, so I'm wondering if there are alternative ways to accomplish this using JS/CSS. If anyone h ...

React Component is not functioning with the timer running

I am currently developing a basic timer app using React. Here is the code snippet I have so far: import React from "react" const timer = (props) => { let time = 25; let processStatus = props.timerProcessStatus; // set to true if(processSta ...

What is the best way to align a text element to the right and a PNG element to the left within a

I am currently working on setting up a navigation bar with a PNG link in the top left corner, and a text element "menu" in the top right corner. Despite my efforts, I have not been successful in achieving this layout using "float: right;" The code snippet ...

Incorporate jQuery's reload function inside another function with the syntax $(function

I have a JavaScript application that loads when the page is loaded. var lang = 'en'; i18n.setLng(lang, function(err, t) { /* loading done */console.log('loaded ' + lang) }); $(function() { Mirador({ "id": "vi ...

Dim the shading of a portion of the picture

If you have an image that covers an entire element through CSS like this #myDiv {background-image: url("../images/background.jpg "); background-repeat: no-repeat; background-size: cover; background-position: center;} Now, what if you want to add a gray ov ...

Is your event listener failing to activate?

I have been working on a project where I gather array elements from user input and try to display them in an HTML table. Unfortunately, the event listener seems to not be triggering, and I can't figure out why. Here is the relevant HTML code: ...

Different strategies for displaying a singular pie chart on multiple pages

I have implemented a pie chart on an HTML page and now I want to display this chart on multiple other HTML pages. Below is the JavaScript code for creating the pie chart: function piechart() { var chart; var legend; ...

Using JavaScript, extract individual objects from a JSON object

Recently, I've been tasked with displaying items from a JSON-file in a well-organized manner. However, the format of the JSON file is unfamiliar to me. The code snippet provided below pertains to this task: function readFile(file) { var rawFile = ...

Tips for creating a unique design for a form that includes a non-binary mask, inspired by bitmasks

Currently, I am in the process of creating an app using Reactjs and Material-UI. The main requirement is for users to input a mask of specific length containing various letters such as I, V, and C. In search of a suitable design pattern or solution for thi ...

Learn the method to conceal rows within a table simply by toggling a button

I need a function that will hide the rows below a row with a header and button, and only reveal them when another row with a header and button is clicked. When one of the +/- buttons is clicked, it should hide or expand all the rows with data content. http ...

Utilizing the CSS Top and Left attributes along with Jquery animations

There are two divisions on a page, namely Left_Div and Right_Div. The images in the Right_Div are positioned using CSS properties such as Top and Left. The Left_Div acts as a left panel, and upon clicking a button, both Left_Div and Right_Div animate to sl ...

CSS - Adding a Distinctive "Line" to a Navigation Bar

I am attempting to design a unique navbar layout with two "lines". The first line should consist of several links, while the second line would include an HTML select field with a submit button. The following code will generate the desired navbar visual ou ...

Revamping the design of a menu bar in WordPress

I designed a navigation bar and now I'm looking to customize the CSS for it in WordPress. Specifically, I want to be able to reference the nav bars by name from PHP to CSS. It seems like there are just two dots representing variables. Here is the co ...