Jquery手风琴 - 使用锚来打开&转到特定部分

时间:2016-08-29 16:05:09

标签: jquery anchor accordion

由于jQueryUI 1.10“激活”方法已被删除,我有一些问题打开了我的手风琴部分的链接。

这就是为什么我找到了我发现here的问题的解决方案。

但我仍然有一个问题。

我想要打开特定标签的链接,但也向下滚动。 我试图在段落中添加一些ID而没有成功,正如您在http://jsfiddle.net/VZ3T5/232/

上看到的那样

任何帮助都将受到高度赞赏。

HTML:

    <a class="opener" data-panel="0" href="#question1">Open & go to Question 1</a>

<a class="opener" data-panel="1" href="#question2">Open & go to Question 2</a>

<a class="opener" data-panel="2" href="#question3">Open & go to Question 3</a>

<a class="opener" data-panel="3" href="#question4">Open & go to Question 4</a>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="accordion">

<h3>Question 1</h3>

    <div>
        <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo
            ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
    </div>

<h3>Question 2</h3>

    <div>
        <p id="question2">Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
            purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
            velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit
            faucibus urna.</p>
    </div>

<h3>Question 3</h3>

    <div>
        <p id="question3">Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
            Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
            ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia
            ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.</p>
        <ul>
            <li>List item one</li>
            <li>List item two</li>
            <li>List item three</li>
        </ul>
    </div>

<h3>Question 4</h3>

    <div>
        <p id="question4">Cras dictum. Pellentesque habitant morbi tristique senectus et netus et
            malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus
            orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel
            est.</p>
        <p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
            Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
            inceptos himenaeos.</p>
    </div>
    </div>

jQuery的:

 var $accordion = $("#accordion");

$accordion.accordion({
      collapsible: true, active: false
    });

$(".opener").on("click", function () {
    var $this = $(this),
        toOpen = $this.data("panel");

    $accordion.accordion("option", "active", toOpen);

    return false;
});

1 个答案:

答案 0 :(得分:1)

试试这个:

var $accordion = $("#accordion");

$accordion.accordion({
    collapsible: true,
    active: false
});

$(".opener").on("click", function() {
    var $this = $(this),
        toOpen = $this.data("panel");
    $accordion.accordion("option", "active", toOpen);
    var timer = $accordion.accordion("option", "animate");//get animate value
    setTimeout(function() {
        $("html,body").animate({
            scrollTop: $($("#accordion>div").get(toOpen)).offset().top
        }, 300);
    }, timer); //Use it in the timer.
    return false;
});

简单的方法是放一个计时器,直到手风琴打开。 Accordion可以选择指定animate值,也可以用作计时器

演示:http://jsfiddle.net/GCu2D/1493/