jQuery置顶导航

一时失言乱红尘 2022-02-02 19:21 353阅读 0赞

Html的代码

  1. <body>
  2. <div class="logo_bar">顶部 logo</div>
  3. <div class="menu">置顶菜单</div>
  4. <div class="menu_pos"></div>
  5. <div class="down_con">
  6. <p style="color:red">英莱特网站主内容</p>
  7. <p>网站主内容</p>
  8. <p>网站主内容</p>
  9. <p>网站主内容</p>
  10. <p>网站主内容</p>
  11. </div>
  12. <a href="javascript:;" class="totop"></a>
  13. </body>

Css的代码

  1. <style type="text/css">
  2. body {
  3. margin: 0px;
  4. }
  5. .logo_bar {
  6. width: 960px;
  7. height: 200px;
  8. background: url(img/bg3.jpg) no-repeat center;
  9. background-size: 100%, 100%;
  10. margin: 0 auto;
  11. }
  12. .menu,
  13. .menu_pos {
  14. width: 960px;
  15. height: 50px;
  16. margin: 0 auto;
  17. background-color: gold;
  18. text-align: center;
  19. line-height: 50px;
  20. }
  21. .menu_pos {
  22. display: none;
  23. }
  24. .down_con {
  25. width: 960px;
  26. height: 1800px;
  27. margin: 0 auto;
  28. }
  29. .down_con p {
  30. margin-top: 100px;
  31. text-align: center;
  32. }
  33. .totop {
  34. width: 50px;
  35. height: 50px;
  36. background: url(img/top.jpg) center center no-repeat #000;
  37. background-size: 100%, 100%;
  38. border-radius: 50%;
  39. position: fixed;
  40. right: 50px;
  41. bottom: 50px;
  42. display: none;
  43. }
  44. </style>

jQuery的代码

  1. <script src="../js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
  2. <script type="text/javascript">
  3. $(function(){
  4. $(window).scroll(function(){
  5. var nowTop=$(document).scrollTop();
  6. if(nowTop>200){
  7. $('.menu').css({
  8. position:'fixed',
  9. left:'50%',
  10. top:0,
  11. marginLeft:-480
  12. });
  13. $('.menu_pos').show();
  14. }
  15. else{
  16. $('.menu').css({
  17. position:'static',
  18. marginLeft:'auto'
  19. });
  20. $('.menu_pos').hide();
  21. }
  22. if(nowTop>400){
  23. $('.totop').fadeIn();
  24. }
  25. else{
  26. $('.totop').fadeOut();
  27. }
  28. })
  29. $('.totop').click(function(){
  30. $('html,body').animate({'scrollTop':0});
  31. })
  32. })
  33. </script>

发表评论

表情:
评论列表 (有 0 条评论,353人围观)

还没有评论,来说两句吧...

相关阅读

    相关 博文

    欢迎来到我的博客 > 执灯把酒看见 归来仍是少年 简介 原产地:浙江衢州 生产日期:2004.10 生产编号:CYW 常用ID:Martian148