On my page, I have a number of long paragraphs with 'more' links to toggle additional text.
When clicking on the 'more' toggle, the page should move up by 100px to show the new text without the user having to scroll.
The code I'm using for this is:
$("html, body").animate({ scrollTop: "100px" });
At this moment, this works only on the first link 'Home1' - when clicking "more" on home1 it moves up 100px
But on other links (Home2/Home3), the page moves back to 'Home1' when clicking on more.
What am I doing wrong?
$(document).ready(function() {
var ellipsestext = "...",
moretext = "More",
lesstext = "Less",
showChar;
$('.more').each(function() {
var content = $(this).html();
showChar = content.search(/<!\-\-\s*break\s*\-\->/);
if (content.length > showChar && showChar != -1) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var html = c + '<span class="moreellipses">' + ellipsestext + ' </span><span class="morecontent"><span>' + h + '</span> <a href="" class="morelink">' + moretext + '</a></span>';
$(this).html(html);
}
});
$(".morelink").click(function() {
if ($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
// Example : Scroll the window at 100px from the top
$("html, body").animate({ scrollTop: "100px" });
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});
});
<li><a class="first" href="#f">Home1</a></li>
<li><a class="second" href="#second">Home2</a></li>
<li><a class="third" href="#third">Home3</a></li>
Here's a jsfiddle with text.
EDIT: updated text and added jsfiddle as agreed through comments. Clarifies "homepage" vs "Home1" section on the page.