javascript - Auto scroll to span element - Stack Overflow

I have anelement that contains (surprise!) an article.There is a list of tags at the top of the page

I have an element that contains (surprise!) an article.

There is a list of tags at the top of the page that are found in the article. When a user clicks a tag it any matching words in the article are highlighted.

What I am having trouble with is then automatically scrolling to the highlighted word.

Is there any way to do this with javascript/jQuery?

The following is my code to find the word and highlight it:

$(".article-tags a.toggle").live("click", function () {
        var $this = $(this);
        var $p = $this.closest("p");
        if ($p.find("span.highlight").length == 0) {
            $("#viewer .article-body").highlight($this.text());
            $this.highlight($this.text());
            document.getElementById("viewer").scrollTop = $p.find("span.highlight").offsetTop;
        }
        else {
            $("#viewer .article-body").removeHighlight();
            $p.removeHighlight();
        }
        return false;
    });

Thanks.

I have an element that contains (surprise!) an article.

There is a list of tags at the top of the page that are found in the article. When a user clicks a tag it any matching words in the article are highlighted.

What I am having trouble with is then automatically scrolling to the highlighted word.

Is there any way to do this with javascript/jQuery?

The following is my code to find the word and highlight it:

$(".article-tags a.toggle").live("click", function () {
        var $this = $(this);
        var $p = $this.closest("p");
        if ($p.find("span.highlight").length == 0) {
            $("#viewer .article-body").highlight($this.text());
            $this.highlight($this.text());
            document.getElementById("viewer").scrollTop = $p.find("span.highlight").offsetTop;
        }
        else {
            $("#viewer .article-body").removeHighlight();
            $p.removeHighlight();
        }
        return false;
    });

Thanks.

Share Improve this question asked Nov 23, 2010 at 16:11 electricsheepelectricsheep 5,2249 gold badges38 silver badges41 bronze badges 1
  • Does it work in the first place? I see that this scrolls, so may I assume that it doesn't work? – Blender Commented Nov 23, 2010 at 16:15
Add a ment  | 

2 Answers 2

Reset to default 5

Several ways to acplish that.

  • jQuerys .animate() with scrollTop set to elements.offset().top
  • $(window).scrollTop(element.offset().top);
  • element.scrollIntoView();

.scrollIntoView() is a native method which you can call on a DOM node directly.

$('span.highlight').first().prepend('<a class="highlighted" name="hightlighted" />');
window.location.hash = '#highlighted';

That should do it. You're adding an anchor just before the element, then scrolling to it. You might want to put in a $('a.highlighted').remove() before this to clear any previous additions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745234902a4617845.html

相关推荐

  • javascript - Auto scroll to span element - Stack Overflow

    I have anelement that contains (surprise!) an article.There is a list of tags at the top of the page

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信