Clicking on anchor takes me to the bottom instead of restoring the previous position
I am kind of really stuck with this problem. Any help will gr开发者_Go百科eat.
I am clicking on a link which expand the content and when i am cliking on a hide button, instead of taking me to the Expand link, it takes me to the bottom.I have already tried such options like onclick="fun() return false" and href=javascrpit:void(0), but not could help.
PLease refer http://jsfiddle.net/BdsyJ/ this link and click on "How do I maximize battery life" and at the bottom you will get a hide button which should take the control back to the Click it rather than placing the page at the bottom.
Thank you guys.
I changed your ReverseDisplay()
method to this and it works nicely:
function ReverseDisplay(d) {
$("#" + d).toggle();
$('html, body').animate({
scrollTop: $("#" + d).prev().offset().top
}, 100);
}
here's a working example:
http://jsfiddle.net/hunter/BdsyJ/5/
In case you were wondering; YES your HTML is invalid. <li>
elements should not have <div>
siblings.
You're at the bottom of the page because you have hidden so much content. Two things I would update in your code:
- cache the element look up so you only do it once and and
- scroll the page to the top after you close it using
scrollTo(0,0)
or something more complex if you need to scroll back to the exact element you toggled.
Code:
function ReverseDisplay(d) {
var el = document.getElementById(d);
el.style.display = (el.style.display == "none")?"block":"none";
}
精彩评论