JS实现鼠标移入导航背景 移动动效

    <style>
        *{
            margin:0;
            padding: 0 ;
            border:0;
            list-style: none;
        }
        body{
            background-color: #ffa8a8;

        }
        #nav{
            width:1300px;
            height:63px;
            line-height: 63px;
            background: #ffffff;
            background-size:contain;
            position: relative;
            top:100px;
            border-radius:20px;
            margin: 0 auto;

        }
        #nav ul{
            position:relative;
        }
        #nav ul li{
            float: left;
            line-height: 63px;
            height:63px;
            width: 88px;
            text-align: center;
            cursor: pointer;
        }
        #t_mall{
            position: absolute;
            width:88px;
            height:63px;
            background: url("./backGrOne.jpg") no-repeat center;
            background-size:cover;
        }
        .active{ 
            font-size: 18px;
            font-weight: bold;
        }
    </style>

 

<nav id="nav">
        <span id="t_mall"> </span>
        <ul></ul>
</nav>
<script>
    window.onload=function () {
        var oldTag=0;
        let ulT=document.querySelector('ul')
        let arrD=['一个人','两个人','三个人','四个人','五个人','六个人','七个人','八个人','九个人']
        var oFrag=document.createDocumentFragment();
        for(var i=0;i<arrD.length;i++){
            var liT=document.createElement('li')
            liT.setAttribute('t',i)
            liT.innerText=arrD[i]
            oFrag.appendChild(liT)
        }
        
        ulT.appendChild(oFrag)
        var commonAll=document.querySelectorAll('li'); 
        commonAll[0].className='active';
        //获取需要的标签
        var nav=$("nav")
        var t_mall=nav.children[0];
        var ul=nav.children[1];
        var allLis=ul.children;
        //记录鼠标点击的位置
        var beginX=0;
        //遍历标签执行操作
        for(var i=0;i<allLis.length;i++){
            var li=allLis[i];
            //监听鼠标进入
            li.onmouseover=function () {
                console.log('进入');
                //获取当前li左边的距离
                end=this.offsetLeft;
            }
            //监听鼠标按下事件
            li.onmousedown=function (e) {
                if(oldTag>-1){
                    commonAll[oldTag].className=null;
                } 
                beginX=this.offsetLeft;
                setTimeout(()=>{
                    e.target.className='active'
                })
                oldTag=e.target.attributes[0].nodeValue;
            }
            //监听鼠标离开事件
            li.onmouseout=function () {
                console.log('离开');
                end=beginX;
            }
        } 
        //缓动动画
        var begin=0,end=0;
        setInterval(function () {
            begin=begin+(end - begin)*0.1;
            t_mall.style.left=begin+"px";

        },10);
        function $(id) {
            return typeof id==='string'?document.getElementById(id):null;
        }
    }
</script>

 

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值