Recreating scroll, image/text animation, and fade-ins using Next.js and TypeScript

I'm currently in the process of replicating a demo template found at using scroll effects and mouse location tracking within nextjs/typescript.

While troubleshooting, I encountered an issue with horizontal scrolling that can be resolved by adding overflow:hidden to the section tags - however, this breaks any sticky content interactions.

Unfortunately, I haven't come across any examples on how to accurately recreate these actions, and I'm unsure about the process of manipulating styles/dom to achieve the effect correctly.

I've started developing a function to manipulate these elements, but I'm uncertain about how to bind it and animate it effectively based on mouse events.

Upon loading, there is a title animation where the text enlarges, shrinks, and then returns to its original size https://i.sstatic.net/6Hndg4yB.png

The side rings are rotating https://i.sstatic.net/rE1dV3hk.png https://i.sstatic.net/cw8J8qPg.png

// Hero Section
const Hero = () => {
  React.useEffect(() => {
    var ellipseLeft = document.querySelector(".ellipse-left");
    var ellipseRight = document.querySelector(".ellipse-right");

    var rotateZ = 23;

    setInterval(() => {
      rotateZ++;

      ellipseRight.style.transform =
        "translate3d(0px, 0px, 0px) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(" +
        rotateZ +
        "deg) skew(0deg, 0deg)";
      ellipseRight.style["transform-style"] = "preserve-3d";
      ellipseRight.style["will-change"] = "transform";

      ellipseLeft.style.transform =
        "translate3d(0px, 0px, 0px) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(" +
        rotateZ +
        "deg) skew(0deg, 0deg)";
      ellipseLeft.style["transform-style"] = "preserve-3d";
      ellipseLeft.style["will-change"] = "transform";
    }, 50);
  }, []);

During scrolling, the phone unlocks and text moves using parallax https://i.sstatic.net/QvxkmOnZ.png

Check out the project on: https://codesandbox.io/p/sandbox/fervent-fast-forked-wcsmh4

As elements are revealed for the first time, they fade in smoothly https://i.sstatic.net/F55YDgVo.png

Additionally, there is a contrast transition from light to dark within this section which changes based on scroll direction and speed https://i.sstatic.net/BH5NWT2z.png

Answer №1

To achieve scroll effects, image/text animation, and fade-ins in a Next.js project with TypeScript, consider using libraries like framer-motion for animations and react-intersection-observer for scroll-based animations.

Here is a simple example illustrating how you can organize your components to create similar effects:

jsx

import { useEffect, useRef } from 'react';
import { motion, useAnimation } from 'framer-motion';
import { useInView } from 'react-intersection-observer';

const HeroSection = () => {
    const controls = useAnimation();
    const [ref, inView] = useInView();
    const titleRef = useRef(null);

    useEffect(() => {
        if (inView) {
            controls.start('visible');
        }
    }, [controls, inView]);

    useEffect(() => {
        if (titleRef.current) {
            const titleTimeline = controls
                .sequence()
                .add({ scale: 1.2, transition: { duration: 0.5 } })
                .add({ scale: 1, transition: { duration: 0.5 } });

            titleTimeline.start();
        }
    }, [controls]);

    return (
        <section className="hero" ref={ref}>
            <motion.div
                className="title"
                ref={titleRef}
                initial={{ scale: 0 }}
                animate={controls}
            >
                Your Title Here
            </motion.div>
            {/* Additional content and animations */}
        </section>
    );
};

export default HeroSection;

Explanation:

We utilize react-intersection-observer to detect when the component enters the view. Once the component is in view, we trigger the animation sequence defined by framer-motion controls. For the title animation, we use a useRef hook to target the title element and animate it upon entering the view. You can incorporate more animations and effects using framer-motion controls and hooks. Ensure to install framer-motion and react-intersection-observer:

bash

npm install framer-motion
npm install react-intersection-observer

Remember to import these dependencies at the beginning of your file:

jsx

import { useEffect, useRef } from 'react';
import { motion, useAnimation } from 'framer-motion';
import { useInView } from 'react-intersection-observer';

This code offers a foundational framework for incorporating scroll-based animations and fade-ins in a Next.js TypeScript project. Customize and expand upon this structure to suit your specific animation requirements.

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

Does manually changing the "dirty" setting of a field affect the CSS classes in AngularJS?

I am facing an issue with my AngularJS form. Even after setting the fields to dirty on form submission, the CSS property does not change as expected. The ng-pristine class remains on the elements instead of changing to ng-dirty. Although error messages ar ...

The Jquery datepicker can display the day prior to the selected date

I am utilizing jQuery for a date filter function, but when I send the date to the server it appears different from what is in the model. For example, if I select from October 27, 2014 to October 27, 2014. var fromDate = new Date($scope.date.from); //Mon O ...

updating page in angular2 based on location.hash

I'm facing an issue with my application where I need to reload the page, but it seems to be stuck in a loop and keeps reloading repeatedly. ngOnInit() { location.reload(); } I attempted another approach: ngOnInit() { console.log("inited") ...

Vue.js is having trouble locating images

I am currently using Vue-CLI with the latest version of Vue (3.9.3), but I am facing an issue where Vue cannot locate my images. Below are some screenshots for reference. Why are the images not showing up? First image (Structure) Second image (template) ...

Is there a way to capture a word from a webpage with just a double click as an input?

I am interested in creating a web application that can take input from the user by double-clicking on a word within the webpage. I have seen this feature utilized in the E2B dictionary Google Chrome extension and would like to implement something similar. ...

Strange behavior observed in the Datepicker, possibly linked to the blur event

I'm facing a challenge with the Datepicker feature. When I blur, the calendar disappears if the Datepicker was active, but the focus remains on the input field. As a result, I am unable to trigger the Datepicker again by clicking on the input field. T ...

Creating a countdown timer for an ASP.NET web application using C#

As I work on developing a timed online test in C# ASP.NET, I encountered an issue with the countdown timer functionality. Currently, the timer is based on the local machine time, but if there is a database disconnection or system hang, it needs to resume f ...

How can I use query to swap out elements within a div when clicked?

I have a project with two separate div classes named demo-heart and demo-coal. The goal is to implement functionality that allows the user to click on the fa-heart icon and have it switch to fa-coal, and vice versa when clicking on fa-coal. <div class ...

Clicking on a Vuetify v-btn with the :href attribute set to download will open the XML file

I'm having trouble getting the v-btn to download an XML file instead of opening it in the browser. <v-btn :disabled="!exportUrl" block x-large height="100" color="primary" :href="exportUrl" download> ...

Selecting an entire row in a Kendo grid

Is there a way to configure kendoGrid to highlight the entire row when clicked on? I have tried setting: scrollable: { virtual: true } and disabled paging, but it doesn't seem to work as intended. You can view an example here: Kendo UI Dojo When ...

Unable to assign the 'id' property within Datatable

Hello there, I am currently using a datatable and fetching data via AJAX. For each data entry, I add a row to my datatable. However, I keep encountering an error that reads: "Uncaught TypeError: Cannot set property 'id' of null in line code " ...

Managing authentication tokens in next.js

I'm currently working on a next.js app that requires authorization for certain functionalities. In the past with CRA, I would store the token in a config.js file and easily import, use, and update it throughout my application. Here is an example of ho ...

Looking for a JavaScript function that will enable the acceptance of commas and spaces

How can I modify this integer validation function to allow for commas and spaces to be entered during the keydown event? function intValidate(event) { if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || even ...

Is there a way to carry out tests on keydown events within Jasmine by specifying the keyCode within an Angular2+

I am working on a project where I need to trigger keydown events on an <input> field. Tools Used Karma v1.7.1 as the test runner Tests running on Chrome browser Using Angular 5 framework Any insights on how I can achieve this? ...

Transforming NIF to OBJ within the Blender 249.2 software results in an object that is not visible

I am currently utilizing Three.js for rendering Fallout 3 assets in WebGL. Check out the JavaScript code for a similar object rendering here. Most objects are loading fine along with their normals, except for the brahmin... While the texture and normals a ...

Node.js with ejs supports the inclusion of partials, but sometimes struggles to locate the variable that has been defined in the partial file

This is the code that should be included in the main ejs file: <% const IDP_URL = "http://idp.com:8082"; const SP_ID = "testing"; const SP_SECRET = "XRRpYIoMtaJC8hFLfUN7Bw=="; const TOKEN_VERIFY_FAIL_URL ="/exsignon/sso/token_verify_fail.ejs"; const L ...

ReactHtmlParser function is returning an object that is classified as [object Object]

Utilizing ReactHtmlParser to return a string as an HTML tag. JavaScript_Lessons_Objects.js function JavaScriptLessonObject() { const one = "Robby "; return ( [ { title: [<div><p classNam ...

Upon transitioning from Angular 5 to Angular 6, a noticeable issue arises: The existing document lacks a required doctype

I recently updated my project from Angular 5 to Angular 6. Post-upgrade, everything compiles without errors. However, when I try to access the website, all I see is a blank screen. Upon inspecting the console, I came across the following error message: Th ...

The comparison between AJAX and JSON passing and PHP generating HTML versus returning it

Currently, my code looks like this: <li onclick = " function CBAppData( callerObj, data ) { var string = ''; for( a in data ) { debug.push( data[ ...

How can I find the last element that was selected using XPath in the browser console

Need help with XPath: $x(("//div[@class='ag-header-row']))[1] I'm working with an array of divs, but I want to select the last one. The [1] is necessary due to multiple rows with this class. I’ve heard about using [last()], but unsure w ...