开发者

Scroll to text link

I would like to make a link that finds some text and scrolls to that point. I can't add span or div tags.

I have found this and idea开发者_Python百科lly I would like to turn it into a link and add animation. Thanks

$(window).scrollTop($("*:contains('Are you a Lib Dem or Tory'):last").offset().top);

Original stackoverflow question


It seems to me that this is the sort of thing a standard <a> tag does already without JavaScript, if you can add an <a name="jumppoint"> tag around or at the start of that text and then another <a href="#jumppoint"> tag where you want your visible link. You could insert such an <a> dynamically with jQuery.

But if you're determined to use that code you can add a link as follows:

<a href="javascript:void(0);" onclick="$(window).scrollTop($('*:contains(\'Are you a Lib Dem or Tory\'):last').offset().top);">Your link text here</a>

Of course that's kind of ugly so you'd probably be better off creating a function:

<a href="javascript:void(0);" onclick="findLink(); return false;">Your link text here</a>

function findLink() {
    $(window).scrollTop($("*:contains('Are you a Lib Dem or Tory'):last").offset().top);
}

If you want some animation add it in the findLink() function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜