Tips for utilizing window.scrollTo in order to scroll inside an element:

I'm experiencing an issue where I have a vertical scrollbar for the entire page, and within that, there's an element (let's call it 'content') with a max-height and overflow-y scroll.

The 'content' element contains child elements without specific HTML IDs (due to using an external library). I've obtained the offsetTop of the child element I want to scroll to, but instead of the element scrolling, the window itself scrolls to that offset top.

A demonstration of the issue can be found here: JSFiddle

var selector = document.getElementsByClassName("inner")
var offset = selector[0].childNodes[3]['offsetTop'];
  window.scrollTo(0, offset);
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
  max-height:500px;
  overflow-y: auto;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
  text-align: center;
}

.inner {
  max-height: 700px;
  overflow-y: scroll
}
<div id="app">
  <div class='inner'>
     <p>
     // Lorem Ipsum placeholder text
     </p> 
     ... (repeated Lorem Ipsum paragraphs)

     <p>
      harsimer kaur (repeated many times)
     </p>

     <p>
     // Lorem Ipsum placeholder text
     </p> 
  </div>
</div>

Answer №1

If you want to scroll within a div, consider using

selector[0].childNodes[3].scrollIntoView();
as a potential solution.

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

Baconjs exclusively retrieves the final debounce value

Below is a code snippet that showcases my current implementation: let watcher; const streamWatcher = bacon.fromBinder(sink => { watcher = chokidar.watch(root, { ignored: /(^|[\/\\])\../ }); watcher.on('all&a ...

I'm having trouble locating the container with only one child element in JavaScript

I am currently working on a project where I need to locate a specific container using JavaScript. More specifically, I am looking to target the container that houses the required address. My aim is to assign a class to the container of the requested addre ...

Top method for adding animation to the undeclared feature on a component

During a recent keynote, I heard that in the future versions of React, it's recommended to hide a component or element using the hidden property. However, I'm curious about how to incorporate a transition effect when toggling the visibility of an ...

Converting the given data into an object in JavaScript: a step-by-step guide

"[{'id': 3, 'Name': 'ABC', 'price': [955, 1032, 998, 941, 915, 952, 899]}, {'id': 4, 'Name': 'XYZ', 'id': [1016, 1015, 1014, 915, 1023, 1012, 998, 907, 952, 945, 1013, 105 ...

Code Not Executing

I have come across an issue with two select tags that are supposed to show or hide divs based on the dropdown selection. Strangely, I am able to get them to work independently, but when trying to make them work together, they fail. Any ideas as to why this ...

When using the makeStyles hook in a function component with React, Material-UI, and Typescript, an Invalid Hook Call error may

I'm feeling lost and frustrated with my current coding issue. Despite following the guidelines in the (javascript) documentation, I keep encountering the dreaded Invalid Hook Call error. My setup includes TypeScript 4.1.2, React 17.0.1, and React Typ ...

Sorting arrays can yield varying results depending on the browser being used

The variations in output between Chrome 70.0, Chrome 69.0, and Firefox 63.0 for the same code are puzzling. var arr = [1,2,43,1233,5546,33,6,11]; arr.sort(() => -1); //[11, 6, 33, 5546, 1233, 43, 2, 1] arr.sort(() => 1); //[1, 2, 43, 1233, 5546, 33, ...

Exploring NodeJS and ExpressJS: Unveiling the significance of routes/index.js

Can you explain the function of this particular file? Does this file handle all the GET and POST requests in a project? If so, wouldn't it become excessively long and complex for larger projects? I encountered an issue when trying to call a POST re ...

Ways to indicate in Typescript that a value, if it exists, is not undefined

Is there a way to represent the following logic in TypeScript? type LanguageName = "javascript" | "typescript" | "java" | "csharp" type LanguageToWasmMap = { [key in LanguageName]: Exclude<LanguageName, key> ...

Encountering issues with generating image files using createObjectURL after transitioning to NextJS 13 from version 10

I'm currently working on a website with the following functionality: Client side: making an API call to retrieve an image from a URL Server side: fetching the image data by URL and returning it as an arrayBuffer Client side: extracting the arrayBuffe ...

Transition from FadeOut to loading content and displaying it

Is there a way to simply display the content after loading without using fadeIn? $(function() { $('.hovers').click(function(event) { var target = $(this).attr('href'); window.location.hash = target; $.ajax({ url: ...

Vue's getComponentName method now properly returns a function instead of returning 'undefined'

After tirelessly working on upgrading my project from Vue 2.6 to 2.7, I've managed to resolve most of the issues. However, there is a persistent problem with certain files that have cased props, triggering Vue to generate an error message known as a & ...

How can I create a single modal in Ionic 3 that occupies the full screen width and height?

Having trouble resizing my Ionic modal to full screen for just one modal, as Ionic's default settings are overriding any custom changes. I've attempted the following code snippet: @media only screen and (orientation: landscape) { ion-modal { ...

The backspace key is unresponsive following the use of a jQuery class

Using a specific class for an input field: Here is the code for the input field: <?php echo $this->Form->input('duration', array('class'=>'input-large text-right number-field','value'=>'0&apo ...

How can you adjust the input size in Chrome with italic font styling?

Currently, I am troubleshooting an issue with the styling of my input fields. Specifically, I want to change the font style to italic when the input is modified. This functionality is performing correctly in Firefox. However, in Chrome, when I add font-sty ...

The horizontal overflow in the HTML code was unsuccessful

I stumbled upon an interesting issue where I applied a div with specific styling: overflow-x: scroll However, the outcome was not as expected. Instead of overflowing, the content simply started on a new line. Here is the source code for reference: & ...

Angular provides a convenient way to call an API on key press. Let's delve

How can I trigger an API call in Angular when a user clicks on a textbox during a keypress event? I am encountering an error with the debounce method that says Cannot read property 'valueChanges' of undefined app.component.ts ngOnInit() { t ...

Unable to function in simplistic html/php code, 'Require' fails to operate as intended

I am facing an issue while attempting to import a header.php file into my index.php file. For some reason, it is not working as expected. header.php: <!DOCTYPE html> <html> <head></head> <body> <header> & ...

Maxlength and Minlength attributes are not considered when using input type=“number”

Why is the maxlength attribute not functioning properly for this input element? <input type="number" maxlength="5" maxlength="10" /> ...

Steps for serving audio files in a Spring Boot application

Can someone advise on the best location to place my audio folder in order to reference it as src="audio/audio_name" within an HTML audio tag? I attempted placing it in the resources folder, but when using src="http://localhost:9191/resource ...