Home Web Front-end JS Tutorial Emergence.js js plugin to detect element visibility

Emergence.js js plugin to detect element visibility

Jan 18, 2018 am 11:26 AM
javascript

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

  1. No need to rely on other components

  2. Supports IE8+ and all Modern browsers

  3. Only 1kb compressed

Install

Emergence.js The installation is very simple. The installation method of npm is as follows:


npm install emergence.js
Copy after login

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>
Copy after login

How to use

Add data-emergence="hidden" to any element you want to watch:


< p class = “ element ” data-emergence = “ hidden ” > </ p >
Copy after login

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 ] {
 / *可见状态* / 
}
Copy after login

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 === &#39;visible&#39;) {
  console.log(&#39;Element is visible.&#39;);
 } else if (state === &#39;reset&#39;) {
  console.log(&#39;Element is hidden with reset.&#39;);
 } else if (state === &#39;noreset&#39;) {
  console.log(&#39;Element is hidden with NO reset.&#39;);
 }
 }
});
Copy after login

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(&#39;.wrapper&#39;);
// www.xttblog.com
emergence.init({
 container: customContainer
});
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles