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

Find a Text and scroll into it with javascript - Stack Overflow

programmeradmin1浏览0评论

I have a div that contains hundreds of lines. I want to create a function that allows me to find a text and scroll into it:

function findAndScroll(text)

So, I enter the wanted text in an input text, I click on the "Go" button that will trigger the "findAndScroll" function then I get scrolled to that text.

Before coding, is there an existant jQuery plugin or a javascript library that can do this?

Thank you,

Regards.

I have a div that contains hundreds of lines. I want to create a function that allows me to find a text and scroll into it:

function findAndScroll(text)

So, I enter the wanted text in an input text, I click on the "Go" button that will trigger the "findAndScroll" function then I get scrolled to that text.

Before coding, is there an existant jQuery plugin or a javascript library that can do this?

Thank you,

Regards.

Share Improve this question asked Aug 25, 2010 at 15:14 ZakariaZakaria 15.1k22 gold badges86 silver badges125 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You can use a function like this to find the text and highlight it. You can then scroll to the highlighted element like this:

var offset = $("#id_of_highlighted_element").offset().top;
window.scrollTo(0,offset);

or you can simply go the id like this

window.location = "#id_of_highlighted_element";

However window.scrollTo is more flexible because you can set the element to wherever you want on the page.

发布评论

评论列表(0)

  1. 暂无评论