61624

Using JQuery how do I get the vertical space above an element on the page?

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?

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

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

Answer1:

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

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.

Answer2:

Are you executing your script after the DOM content has loaded?

$(document).ready( function() { var positionOfLiveMapTop = $(mapElem).offset().top; });

Answer3:

Jose, Unless you're looking for the animation associated with the scrollTo() function, this can be achieved without javascript by ulitizing the bookmark feature of anchors.

<a name="bookmark">Scroll to Here</a> <a href="#bookmark">From Here</a>

Recommend

  • Adding image to google maps marker for this code
  • prism null exception on Container.Resolve()
  • ASP.NET How to Maintain Scroll Position within a Datagrid After Postback
  • Cordova android - Keyboard covering input fields
  • Jump through search results in UIWebView with Javascript
  • Jquery Slider Timer
  • How do I add a HTML hash link without it altering the URL bar…?
  • Fancybox force scroll to top
  • JQUERY, scrollTo, after scrolling down, the page won't let me scroll up for a second… Y?
  • set Focus on Top of screen jquery
  • Using JQuery how do I get the vertical space above an element on the page?
  • Infinite Scroll Effect with javascript
  • Scroll Down [GeckoFx + Javascript]
  • Selenium python unable to scroll down
  • jQueryUI sortable and draggable target won't scroll horizontally for drop but will for sort
  • div fade-in when window is scrolled a certain distance from the top
  • How to define an array of floats in Shader properties?
  • A new chart every sheet
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Unable to send e-mail through Java
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Matrix multiplication with MKL
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?
  • Converting MP3 duration time