带滑块背景的导航

前端入坑纪 09

昨天过生日,零点的今天又是好兄弟过生日,预祝他年年有今日,岁岁有今朝。顺便深夜更新福利来一个,嘿嘿嘿~

一等大事:项目链接

这次换了个源码网址,原来那个runjs网站不知怎么的,一直新建不了,尴尬啊~

亮骚的导航截图
HTML 结构
<div class="wrp">
  <nav>
    <span id="arcr"></span>
    <a class="active" href="javascript:;">导航1</a>
    <a href="javascript:;">导航2</a>
    <a href="javascript:;">导航3</a>
    <a href="javascript:;">导航4</a>
    <a href="javascript:;">导航5</a>
    <a href="javascript:;">导航6</a>
    <a href="javascript:;">导航7</a>
    <a href="javascript:;">导航8</a>
  </nav>
</div>

简单的nav套上a标签,那个span就是滑块

CSS 结构
        html,
        body {
          margin: 0;
          padding: 0
        }
        
        a {
          text-decoration: none;
          color: #333;
        }
        
        .wrp {
          background-color: #fefefe
        }
        
        nav {
          position: relative;
          height: 46px;
          line-height: 46px;
          text-align: center;
          background: #f1eeee;
        }
        
        nav a {
          position: relative;
          display: block;
          float: left;
          width: 12.5%;
          font-size: 13px;
          box-sizing: border-box;
        }
        
        nav a:hover,
        nav a.active {
          color: #f7f9c6;
          transition: all .15s ease-out;
        }
        
        nav a::after {
          position: absolute;
          right: 0;
          display: block;
          width: 0;
          height: 26px;
          top: 10px;
          content: "";
        }
        
        nav a:last-child::after {
          display: none
        }
        
        #arcr {
          display: block;
          width: 12.5%;
          height: 46px;
          position: absolute;
          left: 0;
          top: 0;
          background-color: #ff5400;
          z-index: 0;
          transition: all .3s ease-out .2s;
        }
        
        nav::after {
          content: "";
          display: block;
          visibility: hidden;
          clear: both;
        }

滑块是要绝对定位的,相对于它的父级nav,而所有的a都是浮动的,最后nav:after是用来清除浮动的。

JavaScript 结构
var obtn = document.getElementsByTagName('a'),
  arc = document.getElementById('arcr'),
  winW = window.innerWidth,
  obtnLens = obtn.length,
  indx = 0;
for (var i = 0; i < obtnLens; i++) {
// 给每个a 增加对应的index,以便后面获取
  obtn[i].setAttribute("index", i);
// 给每个a 添加鼠标滑入事件
  obtn[i].onmouseenter = function(e) {
// 获取当前滑入的a的index
    var idx = e.target.getAttribute("index");
// 去除有active的a标签
    document.getElementsByClassName('active')[0].className = "";
// 使用translateX来移动滑块到对应index的a标签上
    arc.style.transform ="translateX("+ idx * winW * 0.125 + "px)";
// 对应的a标签 加上active
    obtn[idx].className = "active"
  }
}

用mouseenter事件是为了更好的演示,小伙伴可以根据实际需求采取更贴切的事件。加油哦,小伙伴们,每天都要进步一点点!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 菜单、按钮及导航 一、下拉菜单 小伙伴们注意,在Bootstrap框架中的下拉菜单组件是一个独立的组件,根...
    凛0_0阅读 5,071评论 0 66
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,911评论 22 665
  • XMind是一款非常实用的思维导图软件,现在的一些实操软件基本都支持快捷键,像ps之类的,大家会发现会快捷键和不会...
    wv橙子阅读 1,496评论 0 1
  • 猫大爷原本是一只流浪猫,三年前与我家的猫小妹恋爱了,那时候猫大爷很年轻,猫小妹很萌很漂亮。猫大爷的落魄贵族气质...
    张何七阅读 503评论 0 3