最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting anchor's offset position - Stack Overflow

programmeradmin2浏览0评论

I am attempting to scroll the window to a location on my page based on a user's selection. I am getting some strange results. Can anyone suggest a good/better way to go about this?

Here is what I'm working with:

 var rel = $(this).attr('rel');
 var citation = rel.split("-")[0];
 window.scrollTo(0, $('[name = ' + citation + ' ]').offset().top);
 alert($('[name = ' + citation + ' ]').offset().top);

The last alert gives me a number that seems wrong and the scrolling is not working. The following code is executed when the user clicks on a link from within the document. I am capturing that element's rel attribute value and (after a little string manipulation) using it to find the position of the corresponding anchor. The destination element's name attribute should match the rel attribute of the clicked link. See what I mean?

Thanks!

I am attempting to scroll the window to a location on my page based on a user's selection. I am getting some strange results. Can anyone suggest a good/better way to go about this?

Here is what I'm working with:

 var rel = $(this).attr('rel');
 var citation = rel.split("-")[0];
 window.scrollTo(0, $('[name = ' + citation + ' ]').offset().top);
 alert($('[name = ' + citation + ' ]').offset().top);

The last alert gives me a number that seems wrong and the scrolling is not working. The following code is executed when the user clicks on a link from within the document. I am capturing that element's rel attribute value and (after a little string manipulation) using it to find the position of the corresponding anchor. The destination element's name attribute should match the rel attribute of the clicked link. See what I mean?

Thanks!

Share Improve this question edited Feb 8, 2010 at 20:11 Dan McGrath 42.1k11 gold badges103 silver badges130 bronze badges asked Jul 23, 2009 at 17:20 NickNick 19.7k53 gold badges190 silver badges313 bronze badges 1
  • 3 if(anchorPos != ON_DECK) { return ON_OCEAN_BOTTOM; } – Kelly S. French Commented Jul 23, 2009 at 17:26
Add a ment  | 

5 Answers 5

Reset to default 3

This is another easy oldschool way to scroll to a html element:

// scrolls to the element with id='citation' 

var node = document.getElementById('citation');    
node.scrollIntoView();

You should be using scrollTop instead of offset since your goal is attempting to scroll the window.

This code ought to work:

var rel = $(this).attr('rel');
var citation = rel.split("-")[0];
window.scrollTo(0, $('[name = ' + citation + ' ]').scrollTop());
alert($('[name = ' + citation + ' ]').scrollTop());

I would add, though, that your name selector there isn't guaranteed to be unique, in which case you'd get strange effects. IDs are meant to be unique on a page, name doesn't have to be. Just a tip.

Had similar issues but the jQuery's scrollTo plugin saved my life.

I was able to get around this by not using offset() but by rather using jQuery's position() function.

I am just getting the returned object's "top" property. I have to use the element's text as an index value because these elements did not have unique IDs.

var citationIndex = parseInt($(this).text() - 1);
var elementOffset = $('.HwCitationsContent li:eq(' + citationIndex + ')').position().top;
发布评论

评论列表(0)

  1. 暂无评论