How to create a more "correct" multi-use jQuery variable -


so here question, or issue. have written below jquery code, it's job load new page content, animate slide, slide old content out, , destroy old content.

i pretty new jquery trained designer , not developer please forgive thats not right proud of have come far.

my goal able slide content in , out in different directions, right left always. issue don't see how without creating new variables each , every direction, 8 different "clickanimation" vars, far many in mind.

var clickanimation = function() {     $(document).on('click', linkleft, function(e) {         var url = $(this).attr('href');         e.preventdefault();         $('body').append('<div id="new_wrapper"></div>');         $('#new_wrapper').load(url + ' #wrapper', function(){             $('#new_wrapper').animate({                 left: '0'             }, 550).width(windowwidth);             $('#wrapper').animate({                 left: '-100%'             }, 550, destroywrapper).width(windowwidth);         });     }); }; clickanimation(); 

you can view jquery code working on site here.

my thought want add sort of var animate function can specify direction , value. idea on i'm looking below, although incorrect.

$('#new_wrapper').animate({         directionnew, valuenew     }, 550).width(windowwidth);     $('#wrapper').animate({         directionold, valueold     }, 550, destroywrapper).width(windowwidth); clickanimation(directionnew:, 'valuenew', directionold:, 'valueold'); 

i don't need whole thing spelled out, although never hurts. looking direction on how create more correct jquery navigation/animation here , how code more correctly in future.

thanks guys , forgive me there similar on site, couldn't find it.

my suggestion define directions javascript objects example

var toleft = {   old_left:'100%',   new_left:0   old_top:0,   new_top:0 }  var toright = {   old_left:'-100%',   new_left:0,   old_top:0,   new_top:0 }  var totop = {   old_left:0,   new_left:0,   old_top:'100%',   new_top:0 }  var tobottom = {   old_left:0,   new_left:0,   old_top:'-100%',   new_top:0 } 

and give links class defines direction of upcoming content.

<a href="labors.html" class="internal tobottom">labors</a> 

after that, need make script decide way go

var clickanimation = function() {     $(document).on('click', linkleft, function(e) {         var $this = $(this)         var url = $this.attr('href');                     e.preventdefault();          var direction = null;         if($this.hasclass('toleft'))            direction = toleft;         else if($this.hasclass('toright'))            direction = toright;         else if($this.hasclass('totop'))            direction = totop;         else if($this.hasclass('tobottom'))            direction = tobottom;         //...same other classes          $new_wrapper = $('<div id="new_wrapper"></div>');         //initial position of new content         $new_wrapper.css({top:direction.old_top,left:direction.old_left});         $('body').append($new_wrapper);         $('#new_wrapper').load(url + ' #wrapper', function(){              $('#new_wrapper').animate({                  top:direction.new_top,                 left:direction.new_left              }, 550)             .width(windowwidth);              $('#wrapper').animate({                  top:'-'+direction.new_top,                 left:'-'+direction.new_left              }, 550, destroywrapper)             .width(windowwidth);          });     }); }; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -