Get the scrolltop of the element

2019-06-03 06:12发布

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
]

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

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

    });

2条回答
来,给爷笑一个
2楼-- · 2019-06-03 06:18

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

查看更多
Juvenile、少年°
3楼-- · 2019-06-03 06:38

In jquery will be like this

$('#yourElement').offset().top;
查看更多
登录 后发表回答