I am currently trying to unhide a DIV and then right after scroll down to an element.
I have this "index" of text where I have a list of anchor tags href'ing to different ID's down the page, but at first sight the text is hidden and will first display when you click on one of the index links. All this works fine untill I get to the scroll part.
There is nothing special about the code so far, it's just a mere if statement adding & removing a CSS class depending on the client action. But how do I get the page to automatically scroll down after the DIV displays?
I tried this solution: How to go to specific to element on page, but I couldn't seem to fit it in my own script.
if ($('#<%=paragraph.ClientID%>').hasClass("readable")) {
$('#<%=paragraph.ClientID%>').removeClass("readable"); // Readable is the class that hides the paragraphs.
// Run function to scroll down to selected element here.
}
else {
$('#<%=paragraph.ClientID%>').addClass("readable");
}
What I first tried was something like this:
(function($) {
$.fn.goTo = function() {
$('html, body').animate({
scrollTop: $(this).offset().top + 'px'
}, 'fast');
return this; // for chaining...
}
})(jQuery);
function showParagraphs(elementId) {
if ($('#<%=paragraph.ClientID%>').hasClass("readable")) {
$('#<%=paragraph.ClientID%>').removeClass("readable");
scrollToElement(elementId);
}
else {
$('#<%=paragraph.ClientID%>').addClass("readable");
}
};
function scrollToElement(elementId) {
$(elementId).goTo();
};
But the above script didn't work. And my javascript skills a limited, so I'm kind of lost.
Any hints or help would be awesome. Thank you.