0

我将搜索功能与 jQueryUI 手风琴结合在一起。
搜索功能突出显示找到输入的搜索词的每个部分的标题。我试图找出一种方法,让视口在打开一个部分时滚动到第一次出现的找到的术语,从而使用户不必滚动浏览可能很长的内容。
每个突出显示的术语都包含在<span class="highlight"&gt;term&lt;/span>. onclick="jumpTo(chapter_number)"当在每个节标题中找到一个术语时,我已经到了向每个节标题添加一个处理程序的地步。我只需要有关jumpTo功能的帮助。谢谢!

这是我的小提琴:http: //jsfiddle.net/jameshuckabonetech/YsdDn/

PS小提琴很棒!第一 ...

更新完成的工作代码...

$( "#accordion" ).accordion(
{
    autoHeight: false, collapsible: true, active: false,
    activate:function(event, ui )
    {
        if ($(ui.newPanel).find('.highlight').length>0 && $("#jump-box").prop('checked') == true)
            $('html, body').animate(
            {
                scrollTop: $(ui.newPanel).find('.highlight').offset().top
            }
            , 2000);
    }
});
4

1 回答 1

2

不要使用 onclick 使用手风琴的激活事件。

$( "#accordion" ).accordion({
    autoHeight: false, collapsible: true, active: false,
    activate:function(event, ui ){
       if ($(ui.newPanel).find('.highlight').length>0)
          $('html, body').animate({
              scrollTop: $(ui.newPanel).find('.highlight').offset().top
              }, 2000);
    },
    beforeActivate:function(e,ui)
    {
        //if statement to check if you want to stop the accordion from opening
        e.preventDefault();
    }
});

使用从jQuery 滚动到元素的滚动代码

于 2013-05-02T20:00:52.447 回答