Comments (3)
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.
I was referring to the following scenario:
- You assign the on enter for a set of elements, ".selector"
- When a specific element from the set has entered the viewport, perform the "doIt" function
- 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.
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)
- Bower support? HOT 12
- `on(event, selector, callback)` should support multi callbacks HOT 1
- First time leave event was not triggered on flex items HOT 5
- check IOS 6 Safari compatibility HOT 1
- Simplify `enter` / `leave` processing with callback: possible to pass the event type into the callback? HOT 3
- off() Method throws an error if trackedElements only has 'enter' or 'leave' HOT 6
- Debounce option is not used in debounced-scroll HOT 1
- Integration with React.js HOT 2
- a question
- Input: Improving scrolling performance HOT 1
- Events fire on page load, not on scroll HOT 2
- It doesn't seem that the check function works when the container is not window HOT 1
- the on.leave event seems not to be working HOT 1
- What about supporting Intersection Observer? HOT 2
- Ionic 3 Can't get this to work HOT 1
- Destroy does not remove MutationObserver HOT 1
- Typescript support HOT 4
- Option to not use debounced scrolling HOT 8
- Enter event firing for all elements matching selector even if outside of container HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onscreen.