React snap scrolling feature is not working as intended

I've been attempting to implement the snap scroll feature in my react app, but I'm facing issues with it. The regular CSS method doesn't seem to work well in Chrome, as discussed in this Scroll Snap Skips Section on Smaller Screen - Chrome thread.

As an alternative, I decided to use the package available at https://www.npmjs.com/package/react-use-scroll-snap. However, even after integrating it, the snap scroll feature does not function correctly (no snapping occurs while scrolling).

Expected behavior: As I scroll down, the view should automatically snap to the "FIRST" div, then to the "SECOND" div, and so on.

I'm unsure of what I might be missing or doing wrong here. Any insights would be greatly appreciated.

Thank you.

App.jsx

import useScrollSnap from 'react-use-scroll-snap';
import React, { useRef } from "react";
import Topbar from "./components/topbar/Topbar";
import "./app.scss";

function App() {
  const scrollRef = useRef(null);
  useScrollSnap({ ref: scrollRef, duration: 100, delay: 50 });

  return (
    <div className="App">
      <Topbar></Topbar>
      <div className="sections" ref={scrollRef}>
        <div>FIRST
        </div>
        <div>SECOND
        </div>
        <div>THIRD
        </div>
        <div>FOURTH
        </div>
      </div>
    </div>
  );
}

export default App;

app.scss

.App{
    height: 100vh;

    .sections{
        width: 100%;
        height: calc(100vh - 70px);
        background-color: lightblue;
        position: relative;
        top: 70px;
        scroll-behavior: smooth;

        > *{
            width: 100vw;
            height: calc(100vh - 70px);
        }
    }
}

Answer №1

After searching extensively, I finally discovered the perfect fix for my issue. Utilizing the package https://www.npmjs.com/package/scroll-snap yielded the exact results I had been seeking.

Interestingly enough, it functions flawlessly on Chrome but encounters difficulties on Firefox; this discrepancy is puzzling as the documentation claims compatibility with all modern browsers.

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

What is the reason for AngularJS's inclusion of a colon at the end of a data object in an $http post

While attempting to utilize angular js $http for sending a post request to elasticSearch, I encounter an "Unexpected token : " Error. Here is a snippet of my code: var request= $http({ method: "post", url: path, accept:"*/*", headers:{"Co ...

The view template in react-native is failing to update after modifying the state in React

I am facing an issue where I am updating the state using the onPress event, the state gets updated but the view is not changing accordingly. <View style={styles.dropDownMenu}> { dataLoaded ? ...

Utilizing Custom Validators in Angular to Enhance Accessibility

I'm struggling to access my service to perform validator checks, but all I'm getting is a console filled with errors. I believe it's just a syntax issue that's tripping me up. Validator: import { DataService } from './services/da ...

Having trouble resizing divisions using three.js?

Three.js is an incredible library that offers thorough documentation and functions perfectly. I am currently attempting to display a plane with trackball control inside a div that resizes according to the window or browser size. However, I am encountering ...

Locating the elusive sequence number within a document

Greetings, I am currently trying to locate a missing number within an xml file but seem to be encountering some challenges. Any suggestions or ideas would be greatly appreciated. Example The file contains an <a> tag with various ids such as page-1, ...

Add elements to a ul element using JavaScript and make the changes permanent

Managing a dashboard website with multiple div elements can be quite tedious, especially when daily updates are required. Manually editing the HTML code is inefficient and time-consuming. Each div contains a ul element where new li items need to be added ...

tips for sending the database value to the next page with ajax

Struggling to send the database value retrieved through ajax to the next page. Tried several methods but none seem to work. Could anyone provide guidance on how to achieve this? php Successfully connected to the database $sql = "SELECT * FROM `billin ...

Sending text content using AJAX

Recently, I've delved into the world of AJAX and JavaScript while working on a project in JSP. My goal is to submit text entered in a textarea using a JavaScript button. The first submit button worked fine initially, but upon posting, I ended up with ...

Error encountered when attempting to reference multiple Select MenuItems in Material UI

Recently, I've been encountering a perplexing error when attempting to open a multiple Select in React+Next.js using Material UI: Error: Argument appears to not be a ReactComponent. Keys: retry This issue seems to be related to a ref. It occurs with ...

Guide to vertically aligning text within a row using Bootstrap 5

I am currently struggling to achieve vertical alignment of text in the center of a row. Despite my efforts, I have not been successful in achieving the desired vertical alignment. What steps can be taken to ensure that the text is vertically centered with ...

Next.js app encounters a BSON error when using TypeORM

Currently, I am in the process of integrating TypeORM into my Next.js application. Despite utilizing the mysql2 driver and configuring 5 data sources, I am encountering a persistent BSON error: ./node_modules/typeorm/browser/driver/mongodb/bson.typings.js ...

Display the toggle button for expanding/collapsing text only when the length of the string exceeds 50 characters

Is there a way to only show a "read more/less" button in a table if the content exceeds 50 characters? The table contains a mix of content, some short announcements with about 10 characters and others with over 100. <div class="col"> <span ng-c ...

Implementing JavaScript Code in TypeScript

Every JavaScript code should also be valid in TypeScript, but when attempting to run the following code snippet below, an error is triggered. Could someone convert this JavaScript code into TypeScript? Error: 20:9 - TS2531 Error: Object is possibly 'z ...

Updating data scope in AngularJS using $http request not functioning properly

I am facing an issue where the $scope.user_free_status is not updating when I set a user free, but works perfectly fine when I unset the parameter. It's strange that I need to reload the page in one case and not the other. The data fetched is stored i ...

"React RSuite: How to reset the value of a select box

Having some trouble with resetting the value of an rsuite SelectBox from outside. It seems setting it to null, undefined, or an empty string doesn't work as expected. After digging into the source code, it appears that this behavior is intentional and ...

When incorporating babel-plugin-styled-components, Nextjs may encounter a mismatch error with classnames

After transferring the content of _document.js from the following styled components example and implementing the babel plugin mentioned in the styled components documentation, I am still encountering an error. _document.js import Document from 'next/ ...

Creating an array upon clicking and dynamically changing its class using AngularJS

Currently, I am developing a scheduling application where there are 2 individuals enrolled for 11 different dates. The functionality I am working on involves allowing the user to click on a month, which will then be added to an array and highlighted. If t ...

Accessing the current window dimensions using CSS

I'm experimenting with a way to set the background of the entire body element. <html> <head><head/> <body> <div class="wrapper"></div> </body> </html> How can I determine the height and width of the cu ...

Error: Null value does not have a property 'tagName' to read

I am having trouble dynamically removing a CSS file from the head tag. I keep receiving this error message: Error: Unable to access property 'tagName' of null Here is my code: Link to CodeSandbox https://i.sstatic.net/Jk3Gt.png export default f ...

Redux fails to retrieve data due to undefined Action Payload

I've decided to transition a React application to incorporate Redux. After setting up the action to trigger on a click event, I noticed that when I include console.log in the reducer's action, I'm getting an undefined value. Since I am fetc ...