Skip to content

Instantly share code, notes, and snippets.

@Awea
Forked from anonymous/wrapper.js
Created September 25, 2012 13:43
Show Gist options
  • Save Awea/3781973 to your computer and use it in GitHub Desktop.
Save Awea/3781973 to your computer and use it in GitHub Desktop.
javascript wrapper
$(document).ready(function(){
// global history
var last_id = new Array;
var state = {};
last_id.push("nothing");
// Add new_id in first element of history (last_id)
function save_my_history(history_list, new_id)
{
history_list = history_list.reverse();
history_list.push(new_id);
history_list = history_list.reverse();
}
// a element call a child, this element go center=>left and the child go right=>center
$.fn.slideToNext = function( child_id, back ) {
var child = $("div.comment_" + child_id);
var father = $("div.slide-active");
father.removeClass('slide-active');
father.animate({'left': '-1000px'}, 1500);
child.animate({'left': '0px'}, 1500);
child.addClass('slide-active');
if (back == false) {
save_my_history(last_id, child_id);
}
history.pushState(state, "", "comments/" + child_id);
};
// a element call his father, this element go center=>right and the father go left=>center
$.fn.slideToPrev = function( father_id, back ) {
var child = $("div.slide-active");
// The content div is #first_slide and add comment id = 0
if (father_id == "0") {
var father = $("#first_slide");
}
else {
var father = $("div.comment_" + father_id);
}
child.removeClass('slide-active');
child.animate({'left': '1000px'}, 1500);
father.animate({'left': '0px'}, 1500);
father.addClass('slide-active');
if (back == false) {
save_my_history(last_id, father_id * -1);
}
};
// a element call a child
$(".on_child").click(function(e){
e.preventDefault();
$('#wrapper').slideToNext($(this).data("comment"), false);
});
// a element call his father
$(".on_father").click(function(e){
e.preventDefault();
$('#wrapper').slideToPrev($(this).data("comment"), false);
});
// gestion du clic sur le bouton précédent du navigateur
$(window).bind('popstate', function(event) {
console.log(event);
if (last_id[0] == "0") {
$('#wrapper').slideToNext(last_id[1], true);
}
else if (last_id[0] >= 0) {
$('#wrapper').slideToPrev($('.slide-active a.on_father').data("comment"), true);
}
else {
$('#wrapper').slideToNext(last_id[1], true);
}
last_id.shift();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment