Mark Mark - 3 months ago 10
jQuery Question

Jquery Accordion - use anchors to open & go to specific section

Since jQueryUI 1.10 "activate" method has been removed and I had some issue opening links to my accordion sections.

That's why I looked for a solution to my problem that I found here.

But I still have a remaining problem.

I would like the link to open specific tab but also scroll down to it.
I tried to add some id to paragraphs without success as you can see on http://jsfiddle.net/VZ3T5/232/

Any help would be highly appreciated.

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;
});

K K K K
Answer

Try this:

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;
});

The simple method will be to put a timer till the accordion is opened. Accordion has an option of specifying the animate value and that can also be used as a timer

Demo: http://jsfiddle.net/GCu2D/1493/