如何使用jQuery测量用户滚动了多远? [英] How can you use jQuery measure how far down the user has scrolled?

查看:120
本文介绍了如何使用jQuery测量用户滚动了多远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户向下滚动超过一定数量的像素后更改元素的样式,然后在用户向上滚动后将其更改。我已经使用jQuery,所以我想使用jQuery如果可能的话。任何人都可以提供一个例子,当用户滚动超过200像素,然后删除类名称一旦用户已回滚到小于200像素,你添加一个类名到div?

I need to change the style of an element after the user has scrolled down beyond a certain number of pixels, and then change it back once the user has scrolled back up. I'm using jQuery already so I'd like to use jQuery if possible. Can anyone provide an example where you add a a classname to a div once the user has scrolled beyond 200 pixels and then remove the classname once the user has scrolled back up to less than 200 pixels?

推荐答案

查看 scrollTop scrollLeft 活动/滚动

示例:

$('div#something').scroll(function () {
    if ($(this).scrollTop() > 200) {
        $(this).addClass('foo');
    } else {
        $(this).removeClass('foo');
    }
});

这篇关于如何使用jQuery测量用户滚动了多远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆