Blogger Information
Blog 40
fans 0
comment 1
visits 39956
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jq返回顶部多种实现方法
Dong.
Original
750 people have browsed it

基础版:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>返回顶部</title>
  6. <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
  7. <style type="text/css">
  8. .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
  9. .content { height: 2000px; border: 1px solid red; }
  10. #goToTop { position: fixed; bottom: 20px; right: 10%; }
  11. #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #FF9966; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="header"> 我是头部</div>
  17. <div class="content">我是主内容,高度是2000px</div>
  18. <div class="footer">我是在最底部</div>
  19. <div id="goToTop"><a href="javascript:;">点我回到页面顶部</a></div>
  20. </div>
  21. <script>
  22. // 原始版
  23. $(function(){
  24. $('#goToTop a').click(function(){
  25. $('html , body').animate({scrollTop: 0},'slow');
  26. });
  27. });
  28. </script>
  29. </body>
  30. </html>

改进版:默认不显示,滚动到一定距离显示

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>返回顶部</title>
  6. <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
  7. <style type="text/css">
  8. .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
  9. .content { height: 2000px; border: 1px solid red; }
  10. #goToTop { position: fixed; bottom: 20px; right: 10%; }
  11. #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #ff0; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="header"> 我是头部</div>
  17. <div class="content">我是主内容,高度是2000px</div>
  18. <div class="footer">我是在最底部</div>
  19. <div id="goToTop"><a href="javascript:;">点我返回顶部</a></div>
  20. </div>
  21. <script>
  22. // 改进版
  23. $(function(){
  24. $('#goToTop').hide(); //隐藏go to top按钮
  25. $(window).scroll(function(){
  26. // console.log($(this).scrollTop());
  27. //当window的scrolltop距离大于1时,go to
  28. if($(this).scrollTop() > 100){
  29. $('#goToTop').fadeIn();
  30. }else{
  31. $('#goToTop').fadeOut();
  32. }
  33. });
  34. $('#goToTop a').click(function(){
  35. $('html ,body').animate({scrollTop: 0}, 300);
  36. return false;
  37. });
  38. });
  39. </script>
  40. </body>
  41. </html>

加强版:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>返回顶部</title>
  6. <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
  7. <style type="text/css">
  8. .container { width:980px; margin:0 auto; height:auto; min-height:100%; position:relative; }
  9. .content { height: 2000px; border: 1px solid red; }
  10. #goToTop {position: absolute; right: -130px; z-index: 9000; }
  11. #goToTop a { background: none repeat scroll 0 0 #336699; border: 1px solid #CCCCCC; border-radius: 3px; -webkit-border-radius: 3px; color: #ff0; font-size: 14px; padding: 5px; text-decoration: none; text-shadow: 0 1px 0 #999; -webkit-text-shadow: 0 1px 0 #999; }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="header"> 我是头部</div>
  17. <div class="content">我是主内容,高度是2000px</div>
  18. <div class="footer">我是在最底部</div>
  19. </div>
  20. <script>
  21. // 加强版(兼容性基本完好)
  22. $(function(){
  23. //goToTop距浏览器顶端的距离,这个距离可以根据你的需求修改
  24. var topDistance = 500;
  25. //距离浏览器顶端多少距离开始显示goToTop按钮,这个距离也可以修改,但不能超过浏览器默认高度,为了兼容不同分辨率的浏览器,我建议在这里设置值为1;
  26. var showDistance = 1;
  27. //定义一个变量,方便后面加入在html元素标签中插入这个goToTop按钮的标签
  28. var goToTopButton = $('<div id="goToTop"><a href="javascript:;">点我回到页面顶部</a></div>');
  29. var thisTop = $(window).scrollTop() + topDistance;
  30. //在container的div里插入我们前面定义好的html标签元素
  31. $('.container').append(goToTopButton);
  32. //设置goToTop按钮top的css属性和属性值
  33. $('#goToTop').css('top' ,thisTop);
  34. if($(window).scrollTop() < showDistance){
  35. $('#goToTop').hide();
  36. }
  37. // 给窗口绑定一个滚动事件,当窗口滚动条滚动时执行
  38. $(window).scroll(function(){
  39. // console.log($(this).scrollTop());
  40. thisTop = $(this).scrollTop() + topDistance; //获取当前window向上滚动的距离
  41. $('#goToTop').css('top', thisTop); //修改goToTop按钮的top距离
  42. console.log(thisTop);
  43. if($(this).scrollTop() > showDistance){
  44. $('#goToTop').fadeIn();
  45. }else{
  46. $('#goToTop').fadeOut();
  47. }
  48. });
  49. // 给按钮绑定一个click事件,点击按钮时,返回顶部
  50. $('#goToTop a').click(function(){
  51. $('html ,body').animate({scrollTop: 0}, 300);
  52. return false;
  53. });
  54. });
  55. </script>
  56. </body>
  57. </html>

资料来源:https://www.cnblogs.com/tianlongouba/p/9838205.html

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post