简体   繁体   中英

Not Able to Reset Div to be in Center After Changing its Position

Can you please take a look at this demo and let me know why I am not able to re-set the .container to be center on

$("#back").on("click", function(){
      $(".container").animate({
              'margin-left': 'auto',
              'margin-right': 'auto',
              'margin':'0 auto'
        }, 550);
});

 $("#move").on("click", function() { $(".container").animate({ 'margin-left': '2.1%' }, 550); }); $("#back").on("click", function() { $(".container").animate({ 'margin-left': 'auto', 'margin-right': 'auto', 'margin': '0 auto' }, 550); }); 
 .container { height: 100px; background: khaki; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <button type="button" class="btn btn-primary" id="move">Move Left</button> <button type="button" class="btn btn-secondary" id="back">Get Back</button> <div class="container"></div> 

You can make the JavaScript remember the margin-left value, when the document gets ready to be spawned!

 var margin; $(document).ready(function() { $('.container').css('margin-left', 'auto'); // first, align it in the center margin = $('.container').css('margin-left'); // and then, store the value // console.log(margin); }); $("#move").on("click", function() { $(".container").animate({ 'margin-left': '2.1%' }, 550); }); $("#back").on("click", function() { $(".container").animate({ 'margin-left': margin, // use it anywhere ;) 'margin-right': 'auto', 'margin': '0 auto' }, 550); }); 
 .container { height: 100px; background: khaki; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <button type="button" class="btn btn-primary" id="move">Move Left</button> <button type="button" class="btn btn-secondary" id="back">Get Back</button> <div class="container"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM