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

Removing a CSS Class Using Tampermonkey: A Step-by-Step Guide

I'm completely new to CSS and javascript, so please bear with me. My goal is to remove the class disable-stream from each of the div elements located under the div with the class "stream-notifications". Below is an image for reference: Even though I ...

Create a Flot graph using data from a database

Here is a snippet of jQuery code that I am currently working with: jQuery(document).ready(function () { var formData = "f=27/07/2015&t=01/08/2015"; jQuery.ajax ({ url: 'http://www.yourhealthfoodstore.co.uk/test.p ...

JavaScript code to locate the most pertinent strings within an array based on a specific substring

I'm working with a large array containing names of people, such as: let names = [ "John Brown", "Tristan Black", "Carl Jobbs", "Aidan Burrows", "Taylor Joe" ]; When given an input, I want to return the top 5 most relevant results ...

Replace async/await with Promise

I want to convert the async/await code snippet below: const mongoose = require('mongoose') const supertest = require('supertest') const app = require('../app') const api = supertest(app) test("total number of blogs" ...

Once a value is selected from the datalist input autocomplete, it loses focus

My issue involves using an input with a dynamic datalist for auto completion. To achieve this, I've implemented the following approach: A key function in my process is as follows: $(function () { $("#to").on("input", function (e) { ...

Identifying the user's location within the application and dynamically loading various Angular scripts

Currently, I am working on a large-scale web application using Laravel and Angular. In this project, I have integrated various angular modules that come with their own controllers, directives, and views. One challenge I am facing is the need to load diffe ...

Use TypeScript to cast the retrieved object from the local storage

const [savedHistory, setSavedHistory] = useState(localStorage.getItem('history') || {}); I'm facing an issue in TypeScript where it doesn't recognize the type of 'history' once I fetch it from localStorage. How can I reassign ...

Stop the automatic hiding of the sticky header when reaching the bottom of the page

I have implemented the position: sticky property for my website's header. However, I am facing an issue where the header becomes hidden when I scroll halfway down the page (bottom on mobile devices). Can anyone suggest a solution to fix this problem? ...

Connecting Angularfire2 with Firestore for advanced querying

Glad you stopped by! Currently, I have two Firestore Collections set up in my Angularfire2 application. One consists of "Clients", while the other contains "Jobs". Each client can have multiple jobs assigned to them, and vice versa. I've been workin ...

The checkbox is not showing the check mark accurately

I am currently working on a React form where I am trying to retrieve the status of a checkbox element from local storage using the useEffect hook. const [checkbox, setCheckbox] = useState(false); useEffect(() => { setCheckbox(localStorage.getItem(&ap ...

Steps to design a unique input radio button with embedded attributes

In my current project, I am utilizing react styled components for styling. One issue that I have encountered is with the text placement within a box and the need to style it differently when checked. What have I attempted so far? I created an outer div a ...

Google Chrome Content Script: Unable to Trigger Dynamically Added Button

In my project, the custom Google Chrome Extension content script is designed to receive a message from popup.html through the Google Chrome Message Passing API once a user logs in using popup.html. This content script is responsible for dynamically adding ...

Issues with the functionality of the asynchronous socket.io JavaScript callback are being experienced

I am facing an issue with my JavaScript code that involves querying data from a database using Node.js and Socket.io. Currently, I have implemented setTimeout() functions to make it work, but I want to switch to using callbacks for better reliability. Howe ...

Investigating the issue of drag and drop functionality not working with the combination of rspec, capy

I am currently using the following tools and technologies: RSpec Capybara Selenium jQuery UI-Sortable Ruby Rails The following code snippet is being used: expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "0") expect(find_by_id(" ...

What is the method of duplicating an array using the array.push() function while ensuring no duplicate key values are

In the process of developing a food cart feature, I encountered an issue with my Array type cart and object products. Whenever I add a new product with a different value for a similar key, it ends up overwriting the existing values for all products in the ...

What is the reason that property spreading is effective within Grid components but not in FormControl components?

Explore the sandbox environment here: https://codesandbox.io/s/agitated-ardinghelli-fnoj15?file=/src/temp4.tsx:0-1206. import { FormControl, FormControlProps, Grid, GridProps } from "@mui/material"; interface ICustomControlProps { gridProps?: ...

Access a designated tab within a CSS-designed tab system

Can anyone offer some assistance? I currently have a tab structure created using CSS. The first tab is set as the default when the page loads. I am looking to create a link from another page that will take users directly to the page with the tabs and ope ...

Exploring the Application of CSS Styles in Selenium

I need help with a webpage that contains a configurable field. The values of this field can be set through a configuration panel, and it can be marked as required by checking a checkbox which applies a specific style to the field label. My question is, how ...

Create a single declaration in which you can assign values to multiple const variables

As a newcomer to react/JS, I have a question that may seem basic: I need multiple variables that are determined by a system variable. These variables should remain constant for each instance. Currently, my approach is functional but it feels incorrect to ...

Errors encountered: Navigation guard causing infinite redirection due to unhandled runtime issue

My Vue3 router is set up with the following routes: export const routes: Array<RouteRecordRaw> = [ { path: "/", name: "Browse Questions", component: HomeView, meta: { access: "canAdmin", }, ...