简体   繁体   中英

Animate scrollTop not working

I have a function when on click it a div is being shown and that works fine. I have also tried to add animate scrollTop to the function where I would like to add div is shown & scroll to it. But unfortunately that's not working. I am not getting any errors in the console.

Here my code below:-

  $(document).ready(function () { $('#video-edit-button').click(function() { $('#video-edit-form').show(); $('html,body').animate({ scrollTop: $("#video-edit-form").offset().top }); }); }); 
  <button class="btn btn-default button-edit" id="video-edit-button">Edit info</button> <div class="panel panel-default video-edit-form" id="video-edit-form"> .... </div> 

That's what you're trying to implement? https://plnkr.co/edit/0jVELqSpoUr4xxcarE0S?p=preview

Here is the working snippet:

 $(document).ready(function () { $('#video-edit-button').click(function() { $('#video-edit-form').fadeIn(1000); $("html, body").animate({ scrollTop: $('#video-edit-form').offset().top }, 1000); }); }); 
 .fakeContent { margin: 10px; width: 300px; height: 300px; } .video-edit-form { display: none; width: 100vw; background-color: pink; height: 300px; } 
 <head> <link rel="stylesheet" href="style.css"> <script data-require="jquery" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> <script src="script.js"></script> </head> <body> <div class="fakeContent", style="background-color: black;"> </div> <div class="panel panel-default video-edit-form" id="video-edit-form"> Video Edit Form Here </div> <div class="fakeContent" style="background-color: green;"> </div> <div class="fakeContent" style="background-color: red;"> </div> <div class="fakeContent" style="background-color: blue;"> </div> <div class="fakeContent" style="background-color: yellow;"> </div> <button class="btn btn-default button-edit" id="video-edit-button">Edit info</button> </body> 

Cheers

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