使用JQuery如何获取页面上元素上方的垂直空间? [英] Using JQuery how do I get the vertical space above an element on the page?

查看:72
本文介绍了使用JQuery如何获取页面上元素上方的垂直空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕下方大约150像素的页面上有一个元素.这在不同的浏览器中是不同的,因此我想将页面自动滚动到页面下方大约150像素的元素的顶部.如何确定该元素相对于页面顶部的位置?

I have an element on a page about 150 pixels down the screen. This is different in different browsers so I want to auto scroll the page to the top of an element that is about 150 pixels down the page. How do I determine what the location of that element relative to the top of the page?

我正在尝试此操作,但无法正常工作. offset()返回null.

I am trying this but it isn't working. offset() is returning null.

var positionOfLiveMapTop = $(mapElem).offset().top;

推荐答案

window.scrollTo(0, $(mapElem).offset().top);

确保已设置mapElem.您的问题很可能不是全局的,并且在您使用它的上下文中未定义.

Make sure that mapElem is set. Your problem is most likely that it's not global and is undefined in the context you're using it in.

这篇关于使用JQuery如何获取页面上元素上方的垂直空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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