Giter Club home page Giter Club logo

Comments (7)

pixelmanya avatar pixelmanya commented on June 21, 2024 1

I had no problems using it like inView.is(element) with Hooks.

from in-view.

theNewWave avatar theNewWave commented on June 21, 2024 1

Thanks.
but I can’t imagine how to write code with hooks.
Would you show me sample code here?

from in-view.

pixelmanya avatar pixelmanya commented on June 21, 2024

Sure, @theNewWave !

Imagine you have two sections which take the entire viewport's height (100vh) and we want to display the current section's name in a box which is on the screen right now. I've put together a quick demo over at codesandbox.io

Code:

import React, { useRef, useState, useEffect } from "react";
import inView from "in-view";

inView.threshold(0.5);

function App() {
  const sectionA = useRef(null);
  const sectionB = useRef(null);
  const sections = [sectionA, sectionB];

  const [visibleSection, setVisibleSection] = useState(false);

  useEffect(() => {
    const onScroll = () => {
      sections.forEach(section =>
        inView.is(section.current) ? setVisibleSection(section) : null
      );
    };

    onScroll();

    document.addEventListener("scroll", onScroll);

    return () => document.removeEventListener("scroll", onScroll);
  });

  return (
    <div className="App">
      {visibleSection && (
        <div className="CurrentSection">{visibleSection.current.innerHTML}</div>
      )}
      <div ref={sectionA} className="Section">
        Section A
      </div>
      <div ref={sectionB} className="Section">
        Section B
      </div>
    </div>
  );
}

I hope this helps!

from in-view.

theNewWave avatar theNewWave commented on June 21, 2024

Thank you for kindly telling me the code!!

from in-view.

pixelmanya avatar pixelmanya commented on June 21, 2024

You're welcome!

from in-view.

paulblqr avatar paulblqr commented on June 21, 2024

I have trouble using it with React Router. It doesn't seem to work when I navigate through a route, then going back on the previous one. I use it to add class and trigger animations, but it doesn't work. Anyone ?

from in-view.

freshmilkdev avatar freshmilkdev commented on June 21, 2024

@theNewWave

  const sections = ['one', 'two', 'three', 'four', 'five'];
  const [sectionInViewport, setSectionInViewport] = useState(null);
  useEffect(() => {
    sections.map(section => {
      //each section is rendered with `#${id}` from list above, i.e. <div id='one'>...</>
      inView(`#${section}`)
        .on('enter', () => {
          console.log(section);
          setSectionInViewport(section);
        })
      /*.on('exit', el => {
        //optional block
        setSectionInViewport(null);
      });*/
    });
  }, []);

from in-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.