Javascript滚动到div id [已关闭]

前端之家收集整理的这篇文章主要介绍了Javascript滚动到div id [已关闭]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在google上搜索了我的问题的解决方案,我不明白为什么我写的代码对所有的工作,但对我来说不.

我写过:

  1. <head>
  2. <Meta charset="utf-8" />
  3. <title></title>
  4. <script type="text/javascript">
  5. function scrollTo() {
  6. $('html,body').animate({ scrollTop: $('#div_id').offset().top },'slow');
  7. return false;
  8. }
  9. </script>
  10. <style type="text/css">
  11. .uno {
  12. height: 1000px;
  13. background: #808080;
  14. }
  15. .due {
  16. margin-top: 300px;
  17. height: 500px;
  18. background: #ff00ff;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="uno" onclick="scrollTo()">
  24. Clicca
  25. </div>
  26. <div class="due" id="div_id"></div>
  27. </body>

解决方法

  1. <html>
  2. <head>
  3. <Meta charset="utf-8" />
  4. <title></title>
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  6. <script type="text/javascript">
  7. function scrollTo() {
  8. $('html,'slow');
  9. return false;
  10. }
  11. </script>
  12. <style type="text/css">
  13. .uno {
  14. height: 1000px;
  15. background: #808080;
  16. }
  17. .due {
  18. margin-top: 300px;
  19. height: 500px;
  20. background: #ff00ff;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="uno" onclick="scrollTo()">
  26. Clicca
  27. </div>
  28. <div class="due" id="div_id"></div>
  29. </body>
  30. </html>

尝试这个:

猜你在找的JavaScript相关文章