function slideToPage($page){
var $wrapper = $('.wrapper');
var width = $wrapper.outerWidth();
var height = $wrapper.outerWidth();
// Set size of all children and stop previous animations
$wrapper.children().css({width: width, height: height}).stop();
// Find the current page (positioned at 0)
var $currentpage = $wrapper.find('.currentpage');
if ($page.is($currentpage)){
console.log("Stay on current page");
return;
}
console.log("$currentpage.index() " + $currentpage.index());
console.log("$page.index() " + $page.index());
// Is the new page index before or after (left or right scroll)
var slideRight = $page.index() > $currentpage.index();
var delta = slideRight ? 1 : -1;
// Position offscreen (to left or right of the screen)
$page.css({left: delta * width});
// Animate new panel to positon 0
$page.animate({left: 0}, 1000).show();
// Animation old panels offscreen (left or right) then hide
$currentpage.animate({left: -delta * width}, 1000, function(){
$currentpage.hide();
});
$currentpage.removeClass('currentpage');
$page.addClass('currentpage');
// hide all other pages (just to be sure)
$wrapper.children().not($currentpage).not($page).hide();
}
// Use wrapped DOM ready event to provide local $ for jQuery
jQuery(function ($) {
$('.scrollitem').on('click', function () {
var $link = $(this);
var target = $link.attr('href').substr(1);
// If the div has any content it is "loaded" (if you know the page has child elements use children().length as it will be faster)
var $page = $('#' + target);
var $target = $page.find('.page-container');
if ($target.html().length) {
console.log('loaded');
slideToPage($page);
} else {
// Dummy test code - REMOVE THIS
$target.html("I AM LOADED:" + target +...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.