获取元素的scrolltop [英] Get the scrolltop of the element

查看:1139
本文介绍了获取元素的scrolltop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个javascript:
在javascript中。您可以单击a元素。我想要一个元素的scrollTop像素。当我使用console.log时。在我的例子中看到。我明白了:

I have this javascript: In the javascript. You can click on the a element. I want the scrollTop in pixels of the a element. When i used console.log. See in my example. I get this:

[
Object
scrollTop: 649
__proto__: Object
]

但问题是。我怎么能得到649?我想把649放在变量中。?

But the problem is. How can i get the 649? I want to put the 649 in a variable.?

Javascript

Javascript

$("a[href*='#']").click(function(e){
        e.preventDefault();
        var offset = $(document).find($(e.target).attr('href')).offset();
        if(!offset)
            var offset = $(document).find($(e.target).parent().attr('href')).offset();

            var test = $({ scrollTop: offset.top });
            console.log(test);  

    });


推荐答案

只需使用 offset.top 。您编写了代码以将其包装在对象中。不需要该代码。

Just use offset.top. You wrote code to wrap that in an object. That code is not needed.

这篇关于获取元素的scrolltop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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