博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js鼠标滚动网页滚动_如何在网页上滚动
阅读量:2502 次
发布时间:2019-05-11

本文共 3413 字,大约阅读时间需要 11 分钟。

js鼠标滚动网页滚动

Scrolling a page to see the content below the fold is probably the most common event happening on a page, more than clicks or keyboard events.

滚动页面以查看首屏下方的内容可能是页面上最常见的事件,而不是单击或键盘事件。

You can listen for the scroll event on the window object to get information every time the user scrolls the page:

您可以在每次用户滚动页面时侦听window对象上的scroll事件以获取信息:

window.addEventListener('scroll', event => {  // scroll event detected})

Inside the event handler you can check the current vertical scrolling position by checking the window property window.scrollY, and the horizontal scolling using window.scrollX.

在事件处理程序内部,您可以通过检查window属性window.scrollY和使用window.scrollX进行水平划线来检查当前的垂直滚动位置。

window.addEventListener('scroll', event => {  console.log(window.scrollY)  console.log(window.scrollX)})

Keep in mind that scroll event is not a one-time thing.

请记住, scroll事件不是一次性的事情。

It fires a lot of times during scrolling, not just at the end or beginning of the scrolling, so there’s a problem if you need to do any kind of operation.

它在滚动过程中会触发很多次,而不仅仅是在滚动结束或开始时,因此,如果您需要进行任何类型的操作,就会出现问题。

You shouldn’t do any computation or manipulation in the handler event handler directly, but we should use throttling instead.

您不应直接在处理程序事件处理程序中进行任何计算或操作,而应使用限制

节流 (Throttling)

The scroll event is not fired one-time per event, but rather they continuously call their event handler function during all the duration of the action.

scroll事件不会在每个事件中一次性触发,而是在动作的所有持续时间内连续调用其事件处理函数。

This is because it provide coordinates so you can track what’s happening.

这是因为它提供了坐标,因此您可以跟踪正在发生的事情。

If you perform a complex operation in the event handler, you will affect the performance and cause a sluggish experience to your site users.

如果在事件处理程序中执行复杂的操作,则会影响性能并给站点用户带来缓慢的体验。

Libraries that provide throttling like implement it in 100+ lines of code, to handle every possible use case. A simple and easy to understand implementation is this, which uses to cache the scroll event every 100ms:

像一样提供节流的库在100多行代码中实现了它,以处理所有可能的用例。 这是一个简单易懂的实现,它使用每100ms缓存滚动事件:

let cached = nullwindow.addEventListener('scroll', event => {  if (!cached) {    setTimeout(() => {      //you can access the original event at `cached`      cached = null    }, 100)  }  cached = event})

Throttling also applies to the mousemove event we saw in the mouse events lesson. Same thing - that event is fired multiple times as you move the mouse.

节流也适用于我们在鼠标事件课程中看到的mousemove事件。 同样的事情-移动鼠标会触发多次该事件。

Here’s an example on Codepen:

这是Codepen上的示例:

See the Pen by Flavio Copes () on .

见笔由弗拉维奥·科佩斯( 上) 。

如何获取元素的滚动位置 (How to get the scroll position of an element)

When you are building a user interface in the browser, you might have an element which can be scrolled, and it’s a common need to know the horizontal and vertical scrolling it currently has.

在浏览器中构建用户界面时,可能会有一个可以滚动的元素,通常需要知道它当前具有的水平和垂直滚动。

How can you do that?

你该怎么做?

Once you have the element, you can inspect its scrollLeft and scrollTop properties.

拥有元素后,就可以检查其scrollLeftscrollTop属性。

The 0, 0 position is always found in the top left corner, so any scrolling is relative to that.

总是在左上角找到0, 0位置,因此任何滚动都与之相对。

Example:

例:

const container = document.querySelector('. container')container.scrollTopcontainer.scrollLeft

Those properties are read/write, so you can also set the scroll position:

这些属性是可读写的,因此您还可以设置滚动位置:

const container = document.querySelector('. container')container.scrollTop = 1000container.scrollLeft = 1000

scrollLeft and scrollTop

翻译自:

js鼠标滚动网页滚动

转载地址:http://gqqgb.baihongyu.com/

你可能感兴趣的文章
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
图的算法专题——最短路径
查看>>
SQL批量删除与批量插入
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
C语言之一般树
查看>>
懂了很多大道理,却依旧过不好一生
查看>>
手工数据结构系列-C语言模拟队列 hdu1276
查看>>
【PyQt5 学习记录】008:改变窗口样式之二
查看>>
android EditText长按屏蔽ActionMode context菜单但保留选择工具功能
查看>>
BUAA 111 圆有点挤
查看>>
c++ 继承产生的名字冲突问题 (1)
查看>>
SQL中on条件与where条件的区别
查看>>
Ubuntu下查看软件版本及安装位置
查看>>
安装IIS
查看>>
动态加载JS(转)
查看>>
shell脚本 inotify + rsync 同步脚本
查看>>