Giter Club home page Giter Club logo

Comments (3)

silvestreh avatar silvestreh commented on July 30, 2024

It's pretty straight forward. In all snippets os is the OnScreen instance:

const os = new OnScreen();

os.on('enter', '.selector', element => console.log(element));
os.destroy();

from onscreen.

TELUS-Alexander avatar TELUS-Alexander commented on July 30, 2024

I was referring to the following scenario:

  1. You assign the on enter for a set of elements, ".selector"
  2. When a specific element from the set has entered the viewport, perform the "doIt" function
  3. Remove the "enter" event for the element from the specific element from the set, only for the element that has finished loading, not the entire ".selector"

from onscreen.

silvestreh avatar silvestreh commented on July 30, 2024

The implementation of that scenario is up to the developer. I would suggest having some kind of flag that you check while running the "doIt" function…

const os = new OnScreen();

os.on('enter', '.selector', (element) => {
    if (!element.entered) {
        // Write whatever you want to do with the `element`

        // Set the flag so this code block won't run next time the `element` enters the viewport
        element.entered = true;
    }
});

In the above snippet element is an instance of DOMElement. I added a new property to the element object to track if it entered the viewport before. If you don't want to add properties to element you could add a new attribute instead:

os.on('enter', '.selector', (element) => {
    if (!element.getAttribute('entered')) {
        // Write whatever you want to do with the `element`

        // Set the flag so this code block won't run next time the `element` enters the viewport
        element.setAttribute('entered', true);
    }
});

In your scenario, .destroy() isn't needed at all. What .destroy() does is removing the event listener from the window (or HTMLElement if you're using a different container) object. Once you remove the event listener you can add again it using .attach().

from onscreen.

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.