圆角背景导航栏

圆角背景导航栏

    • 说明
    • html框架
    • css
    • js
    • 源代码

说明

在这里插入图片描述
实现类似图中 圆角导航效果 当鼠标放在某个导航栏的时候是有选中的效果,并且随着鼠标的移动框会跟着移动,当鼠标点击的时候会有滑动效果

html框架

利用li 实现列表,选项里包含a标签,为了实现动画效果添加两个li

  1. <div id="top">
  2. <ul class="nav">
  3. <li class="slide1"></li>
  4. <li class="slide2"></li>
  5. <li><a class="active" href="#">导航一 </a></li>
  6. <li><a href="#">导航二</a></li>
  7. <li><a href="#">导航三</a></li>
  8. <li><a href="#">导航四</a></li>
  9. </ul>
  10. </div>

css

  • 实现列表水平显示采用flex布局
  • 去掉列表前面的 “.” 利用 list-style-type: none;
  • 实现圆角 border-radius 设置元素的外边框圆角
  • 实现阴影:box-shadow: h-shadow v-shadow blur spread color inset;
  • 将列表放入中央,doby采用flex布局,justify-content 设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。
  • 去掉a标签文本的划线: text-decoration: none;
  • 将。.slides1 和a标签居中显示用 display:inline-block – 显示为内联块元素,表现为同行显示并可修改宽高内外边距等属性
  • 用z-index表示元素的堆叠顺序

    body {

    1. display: flex;
    2. justify-content: center;

    }

    top .nav{

    1. list-style-type: none;
    2. position: relative;
    3. display: flex;
    4. border: none;
    5. border-radius: 10em;
    6. box-shadow:20px 40px 50px #E6E6E6;
    7. padding: 10px;

    }

  1. #top .nav li a{
  2. position: relative;
  3. padding: 0.6em 2em;
  4. font-size: 18px;
  5. border: none;
  6. color: #000;
  7. display:inline-block;
  8. text-decoration: none;
  9. z-index:3;
  10. }
  • 实现动画

    top .slide1, #top .slide2 {

    position: absolute;
    display: inline-block;
    height: 3em;
    border-radius: 10em;
    transition: all 0.4s cubic-bezier(0.23, 1, 0.32, 1.05);
    }

    top .nav .slide1 {

    background-color: yellowgreen;
    z-index: 2;
    }

    top .nav .slide2 {

    opacity: 0;
    background: #fff;
    border: 1px solid #8ab9ff;
    z-index: 1;
    }

    .squeeze {
    transition: all 1.5s;
    transform: scale(0.9);
    }

    ,::before,*::after {
    box-sizing: border-box;
    }

js

  1. $("#top a").on("click",function(){
  2. var position = $(this).parent().position();
  3. var width = $(this).parent().width();
  4. $("#top .slide1").css({
  5. opacity:1,
  6. left:+position.left,
  7. width:width
  8. })
  9. });
  10. $("#top a").on("mouseover",function(){
  11. var position=$(this).parent().position();
  12. var width = $(this).parent().width();
  13. $("#top .slide2").css({
  14. opacity:1,
  15. left:+position.left,
  16. width:width
  17. }).addClass("squeeze");
  18. });
  19. $("#top a").on("mouseout",function(){
  20. $("#top .slide2").css({ opacity:0}).removeClass("squeeze");
  21. })
  22. var currentWidth=$("#top .nav").find(".active").parent("li").width();
  23. var current =$(".nav .active").position();
  24. $("#top .slide1").css({ left:+current.left,width:currentWidth});

源代码

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  6. <title>圆角背景导航栏</title>
  7. </head>
  8. <style> body { display: flex; justify-content: center; } #top .nav{ list-style-type: none; position: relative; display: flex; border: none; border-radius: 10em; box-shadow:20px 40px 50px #E6E6E6; padding: 10px; } #top .nav li a{ position: relative; padding: 0.6em 2em; font-size: 18px; border: none; color: #000; display:inline-block; text-decoration: none; z-index:3; } #top .slide1, #top .slide2{ position: absolute; display: inline-block; height: 3em; border-radius: 10em; transition: all 0.4s cubic-bezier(0.23,1,0.32,1.05); } #top .nav .slide1{ background-color: #FFFF00; z-index: 2; } #top .nav .slide2{ opacity: 0; border: 1px solid #0B6121; z-index: 1; } *,*::before,*::after{ box-sizing: border-box; } .squeeze{ transition: all 1.5s; -webkit-transform: scale(0.9); transform: scale(0.9); } </style>
  9. <body>
  10. <div id="top">
  11. <ul class="nav">
  12. <li class="slide1"></li>
  13. <li class="slide2"></li>
  14. <li><a class="active" href="#">导航一 </a></li>
  15. <li><a href="#">导航二</a></li>
  16. <li><a href="#">导航三</a></li>
  17. <li><a href="#">导航四</a></li>
  18. </ul>
  19. </div>
  20. </body>
  21. <script src="js/jquery.min.js"></script>
  22. <script > $("#top a").on("click",function(){ var position = $(this).parent().position(); var width = $(this).parent().width(); $("#top .slide1").css({ opacity:1, left:+position.left, width:width }) }); $("#top a").on("mouseover",function(){ var position=$(this).parent().position(); var width = $(this).parent().width(); $("#top .slide2").css({ opacity:1, left:+position.left, width:width }).addClass("squeeze"); }); $("#top a").on("mouseout",function(){ $("#top .slide2").css({ opacity:0}).removeClass("squeeze"); }) var currentWidth=$("#top .nav").find(".active").parent("li").width(); var current =$(".nav .active").position(); $("#top .slide1").css({ left:+current.left,width:currentWidth}); </script>
  23. </html>

发表评论

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

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

相关阅读

    相关 导航

    想必大家在百度上见过许多导航栏的代码,下面我来为大家安利三种比较实用的导航栏(仅靠html/css来实现): 1 下拉菜单栏: <!DOCTYPE html>