html - marginLeft as a Javascript variable -


i'm trying animate 'div' in mobile version of website.

$(function(){     $( "#iconreorder" ).bind( "tap", taphandler );          function taphandler( event ){             $( "#iconreorder" ).animate({                 marginleft: "15px"                  }, 500 );         } }); 

when 'div' wil change position i'd able tap once again on , let return default position.

i tried create 'if' statement...but can't understand why doesn't work. i'd assume 'marginleft' of 'div' variable javascript code in order move right if 'marginleft=10px' , move left (default position) if "new" 'marginleft not equal 10px anymore. i've tried following code:

var x = ("#iconreorder").style.marginleft;            if (x = "10px") {         $(function(){             $( "#iconreorder" ).bind( "tap", taphandler );              function taphandler( event ){                 $( "#iconreorder" ).animate({                     marginleft: "15px"                 }, 500 );             }         });     } else {         $(function(){             $( "#iconreorder" ).bind( "tap", taphandler );              function taphandler( event ){                 $( "#iconreorder" ).animate({                     marginleft: "10px"                 }, 1000 );             }         });     } 

can me? hope said understandable. thank you

so if understood correctly, should do:

var x = $("#iconreorder");        x.on('tap', function () {     var = '10px', time = 1000;     if (x.css('margin-left') === '10px') {        = '15px';        time = 500;     }     x.animate({         marginleft:     }, time);  }); 

or in action. replaced tap event click see i'm doing. anyway should work better now.

there might slimmer way make work, can't think of @ moment (maybe toggle method).


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo