I have single .html file
I have <ul>
menu and <li>
's have data-id attributes. I have several content items, which has same id's as menu <li>
's data-id
's. By default most of content items are display:none;
first page content is display:block;
<ul>
<li class="menuLink" id="onepage" data-id="one"></li>
<li class="menuLink" id="twopage" data-id="two"></li>
<li class="menuLink" id="threepage" data-id="three"></li>
<li class="menuLink" id="fourpage" data-id="four"></li>
</ul>
<div class="content">
<div id="one" class="page">...</div>
<div id="two" class="page">...</div>
<div id="three" class="page">...</div>
<div id="four" class="page">...</div>
</div>
This script should appear and disappear content items by clicking on menu items. This works very well.
$('li.menuLink').click(function () {
var elem = $(this);
if (elem.hasClass('active')) {
} else {
$('li.active').removeClass('active');
elem.addClass('active');
$('#' + elem.attr('data-id')).prependTo($('.content'));
var existing = $('.page:visible');
(existing) ? existing.fadeOut(1) : '';
$('#' + elem.attr('data-id')).delay(10).fadeIn(400);
if (($('.body-wrapper').width()) <= 700) {
$('html,body').animate({scrollTop: $('#' + elem.attr('data-id')).offset().top}, 500);
}
}
});
If .body-wrapper
width is less then 700 (mobile device), this script should also scroll down to content, but this function isn't working at all. Once you refresh page and click FIRST menu element, it works once. after that... nothing.
if (($('.body-wrapper').width()) <= 700) {
$('html,body').animate({scrollTop: $('#' + elem.attr('data-id')).offset().top}, 500);
}