2015-06-08 63 views
1

我正在为我公司的FAQ/Helpcenter页面工作。我们试图完成的最后一件事是“顶级问题”部分,用户只需点击问题即可打开问题所在页面的链接,并且手风琴向正确部分打开以显示答案。如何使用基于外部链接打开手风琴来加载页面

$(document).ready(function() { 
function close_accordion_section() { 
    $('.accordion .accordion-section-title').removeClass('active') 
    .find('img').attr('src', 'http://www.scrubsandbeyond.com/app_themes/scrubsandbeyond/graphics/right.png'); 
    $('.accordion .accordion-section-content').slideUp(300).removeClass('open'); 
} 

$('.accordion-section-title').click(function(e) { 
    // Grab current anchor value 
    var currentAttrValue = jQuery(this).attr('href'); 

    if($(this).is('.active')) { 
     close_accordion_section(); 
    }else { 
     close_accordion_section(); 

     $(this).find('img').attr('src', 'http://www.scrubsandbeyond.com/app_themes/scrubsandbeyond/graphics/down.png'); 
     // Add active class to section title 
     $(this).addClass('active'); 
     // Open up the hidden content panel 
     $('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
    } 

    e.preventDefault(); 
}); 


}); 

这是jQuery的用于手风琴,和完整的工作代码是在这里http://jsfiddle.net/gvolkerding/ancu6fgu/3/ 一个例子是,如果我们做了顶部的一个问题“我如何注册接收促销电子邮件?” ,那么该页面将需要加载手风琴第4部分。我们有8个单独的页面,其中包含问题,因此理想情况下,我只需将查询之后的链接(或其他任何可以想到的方式)指向正确的页面/问题即可。我非常感谢所提供的任何帮助,谢谢大家。

回答

1

在你使用的ID(例如手风琴3)来识别小提琴,显示和隐藏手风琴部分。该ID还可以用作任何链接到常见问题解答页面的锚点。将下面的代码在document.ready函数的末尾:

// current location has anchor 
if(location.hash) { 
    // find accordion href ending with that anchor 
    // and trigger a click 
    $(".accordion-section-title[href$="+location.hash+"]").trigger('click'); 
} 

和链接的网页会被somethink像/path/to/faq.html#accordion-3。 Where accordion-3是您想要打开的锚/元素ID。请注意,该页面也会滚动到具有相应ID的元素位置(accordion-3)。为了避免这种情况,您必须在触发点击后滚动到顶部,否则您将使用GET参数而不是位置哈希。

更新:包括页面滚动的问题

由于下面的评论,在这里包括解决滚动到活动问题的一个版本。

if(location.hash) { 
    // the selector 
    var currentTarget = $(".accordion-section-title[href$="+location.hash+"]"), 
     offset = {top: 0, left: 0}; 
    // in case we have a hit... 
    if(currentTarget.length) { 
     // trigger the click 
     currentTarget.trigger('click'); 

     // get current offset (relative to document, contains left and top) 
     // since the selector is the question (not the answer with the id) 
     // this will show the question right on top 
     offset = currentTarget.offset(); 

     // just in case you'll want to add some extra space do it like this: 
     // offset.top -= 10; 

     // and let the page scroll to this position 
     $('html, body').animate({ 
      scrollTop: offset.top, 
      scrollLeft: offset.left 
     }); 
    }  

} 

更新后的fiddle is here

+0

除了你所说的关于滚动到这个问题上的内容之外,这个工作完美无缺。我们可以让它向下滚动到这个问题,但可能回滚也许...... 65px。就这样问题的标题是可见的? – idontwantnoscrubs

+0

@idontwantnoscrubs我已经更新了答案和相关的小提琴。 –

+0

真棒谢谢你,我不知道翻译中是否有错误或发生了什么,但是当我使用小提琴中的代码时它根本不起作用,但是当我复制上面的代码时它完美地工作!非常感谢! – idontwantnoscrubs

0

您可以这样做的一种方法是将问题索引作为查询参数传递。然后,你会在你的代码获取参数值,说,qIndex然后添加以下内容:

//get question index passed via query parameter 
var qIndex = ..... 

$('.accordion-section-title').click(function(e) { 
    ... 
}) 
.eq(qIndex).trigger('click'); //<<<------------ 

DEMO

+0

我应该提到我对jQuery非常陌生,而且我的这段代码已经从互联网和其他一个stackoverflow问题中拼凑出来了。所以,如果我听起来很愚蠢,不过我不知道该怎么做。 – idontwantnoscrubs

+0

我很乐意带你通过....但你必须要更具体。 – PeterKA

+0

好吧,只是为了这个第一个例子。说“我如何注册促销电子邮件?”是我们最重要的问题之一。该链接将显示在我们的顶级问题页面上,但答案将在手风琴页面上显示。所以对于这个例子,它需要加载打开“#accordion-4”的页面页面。该问题所在页面的链接是http://www.scrubsandbeyond.com/help-1.aspx。所以我会假设这个链接看起来像http://www.scrubsandbeyond.com/help-1.aspx#accordion- – idontwantnoscrubs

相关问题