


JavaScript listening event content analysis for monitoring page scrolling
本篇文章给大家带来的内容是关于javascript监听事件之监听页面滚动的内容解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
监听事件
代码
function pageChange () { // ... 页面滚动时,需要做的事情 } window.addEventListener("scroll" , pageChange, false);
知识点
1、使用 window.addEventListener 和 document.addEventListener 来处理页面上的事件,区别仅仅在于:不同事件模型上,处理的顺序不一样。
捕获,window 先于 document
冒泡,document 先于 window
2、参数
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false)
1) type: String 事件的类型
2) listener: Function 侦听到事件后处理事件的函数
3) useCapture: Boolean(default = false)
这里牵扯到“事件流”的概念。
侦听器在侦听时有三个阶段:捕获阶段、目标阶段和冒泡阶段。
顺序 为:捕获阶段(根节点到子节点检查是否调用了监听函数)→
冒泡阶段(目标本身到根节点)。
此处的参数确定侦听器是运行于捕获阶段、 目标阶段还是冒泡阶段。
如果将 useCapture 设置为 true,则侦听器只在捕获阶段处理事件,而不在目标或冒泡阶段处理事件。 如果useCapture 为 false,则侦听器只在目标或冒泡阶段处理事件。
要在所有三个阶段都侦听事件,请调用两次 addEventListener,一次将 useCapture 设置为 true,第二次再将useCapture 设置为 false。
4) priority: int (default = 0)
事件侦听器的优先级。
优先级由一个带符号的 32 位整数指定。
数字越大,优先级越高。
优先级为 n 的所有侦听器会在优先级为 n -1 的侦听器之前得到处理。 如果两个或更多个侦听器共享相同的优先级,则按照它们的添加顺序进行处理。
默认优先级为 0。
5) useWeakReference:Boolean (default = false)
确定对侦听器的引用是强引用,还是弱引用。
强引用(默认值)可防止您的侦听器被当作垃圾回收。 弱引用则没有此作用。
获取页面滚动高度
代码
function getScrollTop() { return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; }
浏览器兼容性
谷歌 | 火狐 | IE | 360 | Microsoft Edge | |
---|---|---|---|---|---|
window.pageYOffset | Yes | Yes | Yes | Yes | Yes |
document.documentElement.scrollTop | Yes | Yes | Yes | Yes | No |
document.body.scrollTop | No | No | No | No | Yes |
其中,pageYOffset 属性返回文档在窗口左上角垂直方向滚动的像素
让页面滚动至指定位置
代码
/* 滚动动画 s: 当前页面滚动高度 sTop: 指定位置滚动高度 */ function tabAnimation(s, sTop) { var type = s < sTop ? true : false; // true 页面上滑 var timmer = requestAnimationFrame(function fn() { if (type) { s+=50 } else { s-=50 } if((type && s > sTop) || (!type && s < sTop)) { // $el.scrollTop = sTop; window.scrollTo(0, sTop); } else { // $el.scrollTop = s; window.scrollTo(0, s); timmer = requestAnimationFrame(fn); } }); }
说明
window.requestAnimationFrame() 方法告诉浏览器您希望执行动画并请求浏览器在下一次重绘之前调用指定的函数来更新动画。该方法使用一个回调函数作为参数,这个回调函数会在浏览器重绘之前调用。
当你需要更新屏幕画面时就可以调用此方法。在浏览器下次重绘前执行回调函数。回调的次数通常是每秒60次,但大多数浏览器通常匹配 W3C 所建议的刷新频率。
在大多数浏览器里,当运行在后台标签页或者隐藏的

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











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 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

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 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 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

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

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 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
