Detailed explanation of the use of Emergence.js plug-in
This time I will bring you a detailed explanation of the use of the Emergence.js plug-in. What are the precautions when using the Emergence.js plug-in? The following is a practical case, let's take a look.
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 after compression
Installation of Emergence.js 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.
<script src="path/to/emergence.min.js"></script> <script> emergence.init(); </script>
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 changes to data-emergence="visible" . Now you can leverage CSS to animate elements:
.element [ data-emergence = hidden ] { / *隐藏状态* / } .element [ data-emergence = visible ] { / *可见状态* / }
Custom options
Emergence.js has many options to customize. 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 perform checks 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: Provides an offset (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 an element is 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(); whose functions are exactly opposite.
Emergence.js relies on the following browser API: querySelectorAll
To support IE8, please ensure standards mode.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
What are the ways to implement Ajax asynchronously in native JSOperation modal single load dataThe above is the detailed content of Detailed explanation of the use of Emergence.js plug-in. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

Numpy is a Python scientific computing library that provides a wealth of array operation functions and tools. When upgrading the Numpy version, you need to query the current version to ensure compatibility. This article will introduce the method of Numpy version query in detail and provide specific code examples. Method 1: Use Python code to query the Numpy version. You can easily query the Numpy version using Python code. The following is the implementation method and sample code: importnumpyasnpprint(np
