This article mainly introduces the js plug-in for detecting the visibility of elements in Emergence.js. It introduces the installation and use of Emergence.js in detail. It has a certain degree of participation. If you are interested, you can learn more. I hope it can help everyone. .
Emergence.js is a lightweight, high-performance JS plugin for detecting and manipulating elements in the browser.
This plugin is designed to allow manipulation of elements based on their visibility in the browser. It gives developers the freedom to use their own CSS or JS to view animations or state changes. It leverages HTML5 data attributes instead of classes to simplify development. Emergence.js is one of the lightest and most compatible plugins of its kind.
Features of emergence.js
No need to rely on other components
Supports IE8+ and all Modern browsers
Only 1kb compressed
Install
Emergence.js The installation is very simple. The installation method of npm is as follows:
npm install emergence.js
In addition, we can also download it directly and introduce Emergence.js in the html file.
How to use
Add data-emergence="hidden" to any element you want to watch:
< p class = “ element ” data-emergence = “ hidden ” > p >
When the element becomes visible within the viewport, the attribute will change to data-emergence="visible". Now you can utilize CSS to animate elements:
.element [ data-emergence = hidden ] { / *隐藏状态* / } .element [ data-emergence = visible ] { / *可见状态* / }
Customization options
Emergence.js has many options Can be customized. The following are the default values:
emergence.init({ container: window, reset: true, handheld: true, throttle: 250, elemCushion: 0.15, offsetTop: 0, offsetRight: 0, offsetBottom: 0, offsetLeft: 0, callback: function(element, state) { if (state === 'visible') { console.log('Element is visible.'); } else if (state === 'reset') { console.log('Element is hidden with reset.'); } else if (state === 'noreset') { console.log('Element is hidden with NO reset.'); } } });
Option description
container: Container, by default, the visibility of the element Will be determined by the window's viewport dimensions and X/Y scroll position (set to window). However, it can be changed to a custom container. For example:
var customContainer = document.querySelector('.wrapper'); // www.xttblog.com emergence.init({ container: customContainer });
Throttle: is a way to prevent performance issues related to scrolling and resizing events. Throttle will create a small timeout and steadily check the element's visibility every certain amount of milliseconds during the event. The default is 250.
reset: Determines whether the data attribute state is reset after display. Reset it to false if you want the element to maintain its displayed state even after leaving the viewport. The default is true.
handheld: Emergence will check for most handheld device models such as phones and tablets. When set to false, the plugin will not run on these devices. The default is true.
elemCushion: Element coordinates will determine how many elements need to be "visible" within the viewport. A value of 0.5 will equal 50% of the elements that need to be visible. The default is 0.15.
offsetTop, offsetRight, offsetBottom, offsetLeft: Provide offsets (in pixels) on any side of the viewport. This is useful if you have a fixed component (such as a header) that you can offset by the same value for the header's height. A value of 100 applied to offsetTop will mean that an element will only be considered visible if it is greater than 100 pixels from the top of the viewport. All default values are 0.
callback: Used to provide callbacks to determine when elements are visible, hidden and reset. Possible states are visible, reset and noreset.
In addition, Emergence.js also provides two advanced methods emergence.engage(); and emergence.disengage();. Their functions are exactly opposite.
Emergence.js relies on the following browser API: querySelectorAll
In order to support IE8, please ensure standards mode.
Related recommendations:
jquery.validata.js plug-in collection, everything you want is here
Detailed explanation of Vue. js plug-in development
jQuery combined with jQuery.cookie.js plug-in to implement skin changing function example
The above is the detailed content of Emergence.js js plugin to detect element visibility. For more information, please follow other related articles on the PHP Chinese website!