Scroll most recently selected object in jquery toggle to the top of the page
After toggling open several sections and I have a heading near the bottom of the window, if I click it, it opens, but the content is outside of the view. I would like the heading to scroll to the top so that the content can be seen. I've been trying scrollTo and scrollIntoView, but have not had any luck yet.
The toggle
$(document).ready(function(){
$(".toggle_container").hide();
$("h2.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
});
});
The content
<h2 class="trigger">开发者_JS百科;<a href="#">Link Name</a></h2>
<div class="toggle_container">
<div class="block">
<h3>Need a Website?</h3>
<p>Consequat te olim letalis</p>
</div>
</div>
<a href="#">Link Name</a>
causes the page to scroll to the top. This is not the correct method for creating a "null" link. Use: <a href="javascript://">Link Name</a>
. Why you're putting the click event on the H2 and not on the A is baffling as well.
Here's help with your scroll action:
$(document).ready(function(){
$(".toggle_container").hide();
$("h2.trigger").click(function(){
window.scrollTo($(this).position().left,$(this).position().top)
$(this).toggleClass("active").next().slideToggle("slow");
});
});
精彩评论