Tips for initiating a scrollable (overflow-y: scroll) div in the middle rather than the top while scrolling

I am working on a code where I have a scrollable div with overflow-y: scroll. However, the default scrolling starts at the top of my div when I want it to start in the middle. Can anyone suggest a solution for this issue?

export function Portfolio({ children }) {
  const portfolioRef = React.useRef(null)
  return (
    <div ref={portfolioRef} className={styles.component}>
      { children }
    </div>
  )
}

.component {
  overflow-y: scroll;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  height: 30rem;
  margin: auto;

  & > * {
    margin-top: var(--size-xxs);
    margin-bottom: var(--size-xss);
  }
}

Answer №1

When referring to the element, you have the ability to utilize the functions scrollTop and scrollLeft and set the desired height for the scrollbar to appear.

//An example of how it can be implemented
useEffect(()=>{
  this.ref.portfolioRef.scrollLeft = 200 //x scroll position
  this.ref.portfolioRef.scrollTop = 200  //y scroll position
 },[])

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

Organizing a drop down list in alphabetical order with Angular.js is not supported

I have encountered an issue with ordering the drop down list alphabetically using Angular.js. Below is the code I am using: <div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon ndrftextwidth text-right" style="width:180p ...

Storing Unique Characters in the Database

Currently, I am utilizing a combination of HTML and PHP for my website, but I have encountered an issue when saving content in a textarea. Whenever I incorporate a different font with special characters like ' and ", it saves as � (a black dia ...

Keeping an eye out for modifications within the ng-repeat when updating mongoose from a separate controller

Recently, I encountered a very specific issue that I need help with resolving. Here's the scenario: I have a department controller that loads a collection from MongoDB and displays it in a table using ng-repeat. Each document in the collection has an ...

Infinite scrolling with Jquery: Loading dynamic content seamlessly from external websites

Hey there! I recently started using this cool jQuery plugin called jquery-endless-scroll. Here's a snippet of my code: $(function() { $('#list').endlessScroll({ pagesToKeep: 10, fireOnce: false, insertBefore: "#list div:first ...

I am struggling to get the images aligned properly on my HTML page. Can someone please advise on how to fix this issue?

On my HTML page, I have 2 div fields. Within each field, there are 5 images listed sideways. The issue is that the images in the div below are not lining up properly side by side. Given my limited frontend knowledge, how can I align them correctly? Here&a ...

Error: accessing localStorage returns undefined value

I'm struggling with fetching values from localStorage. My goal is to save inputs and selections from a form and access them on a different page after hitting submit. Here is an example of the form structure : <form action="thank-you.html" role="fo ...

Unable to retrieve the unprocessed value (including periods) from an HTML number input

Whenever I press a key on the <input type="number" id="n" /> and then type a ., it appears in the input but does not show up in $('#n').val(). For instance, if I type 123., the result from $('#n').val() is just 123. Is there a w ...

How to eliminate underlines in lists using CSS

Currently, I am utilizing Wordpress to develop my website (www.codedgames.com) and the theme I have chosen adds an underline to one of its lists but not the others. I am looking to eliminate the underlines from the "Recent blog posts" widget in the foote ...

Create a custom route variable in Node.js with Express framework

Is there a way to achieve this particular task using express.js? In my express.js application, I have set up a route like so: app.get('/hello', (req, res) => { // Code goes here }); This setup is functional, but I am curious if it is poss ...

Using AngularJS to toggle between two select dropdowns

I have two drop-down lists containing JSON data. <select class="form control" ng-model="fruitsName" ng-options="r.id as r.name for r in fruits"> <option value="">--Select---</option></select> $scope.fruits = [{'id': &apo ...

I am experiencing an issue with setting the border to none using .navbar-toggler

I'm currently utilizing bootstrap 5, and I've encountered an issue with setting the border and outline to none in my CSS file. Specifically, when I apply these properties to the nav-toggler class, it seems to have no effect. .menu-bar .navbar- ...

Tips on creating a dropdown menu with a fontawesome icon

I'm brand new to coding and struggling to create a drop-down menu similar to the one used in fonawsome fa fa-ellipsis-v While I'm attempting to code it here, I'm not sure why the class is being overridden. *{ margin: 0; padding : ...

Issues with CSS transitions/animations not functioning properly across different browsers

Take a look at this FIDDLE Link. I'm facing an issue where the animation works smoothly in Firefox, but it's not functioning as expected in Chrome. Kindly open the fiddle in both browsers (Chrome and Firefox), hover over the image, and notice t ...

What could be the reason my function is not showing any data?

Using AngularJS in web development $scope.fetchData = function(dateTime) { console.log(dateTime + " fetch"); $http({ method: 'GET', url: 'https://api.example.com/appointments', params: { date ...

Creating a spacious text box for an enhanced Ajax search feature

I'm currently working on an AJAX application that allows users to input the name of a movie, and then loads results from the database through jquery using a PHP API. However, I'm facing a challenge in implementing a text box with the following re ...

The DIV is only partially concealed by the box shadow

I'm having an issue with the Box Shadow css style I've applied. It seems to only cover a small portion at the top of the mainContent DIV, leaving the rest without any shadow. Can anyone help me figure out what I might be doing wrong? .mainConten ...

What is the proper way to implement Typescript custom props with styled-components?

My development project involves a Button component and a DayButton that extends the base Button. I also have a component that creates instances of DayButtons. //button.tsx export const StyledButton = styled.button` ... `; export interface ButtonProps { ...

Tips for creating code that will allow users to update their status on a web application, similar to Twitter

I am interested in developing a web application that can receive status updates (text) from an Android device. My plan is to use Ajax and HTML for this project. I'm looking for guidance on how to dynamically update the status on the web page without r ...

Clicking outside the navigation container will not cause it to disappear completely

When the width of my navigation bar reaches a maximum of 560px, a hamburger menu appears for mobile devices. I want to implement a functionality where clicking on a navigation item (e.g., About) will close the nav-container instead of making it disappear c ...

What is the best way to generate a JavaScript variable using information from SQLite?

I found the examples provided in the JavaScript edition of the Missing Manual series to be extremely helpful! OG.Q. I have explored various options but have not been able to find a solution to my problem yet. This is what I am trying to achieve: Save u ...