<style>
*{margin:0;padding:0;}
#box{height:50px;float:left;position:relative;background:#f90;}
#bar{width:64px;height:100%;position:absolute;left:0;top:0;background:#333;}
#bar .l{width:0;height:100%;background:#333;border-radius:50%;position:absolute;top:0;left:0;}
#bar .r{width:0;height:100%;background:#333;border-radius:50%;position:absolute;top:0;right:0;}
#ul{height:100%;font-size:14px;letter-spacing:1px;font-family:"微软雅黑";list-style:none;position:relative;}
#ul li{height:100%;line-height:50px;float:left;}
#ul a{height:100%;text-decoration:none;color:#fff;padding:0 20px;display:block;}
</style>
<div id="box">
<div id="bar">
<div class="l"></div>
<div class="r"></div>
</div>
<ul id="ul">
<li><a href="#1">祝</a></li>
<li><a href="#2">大家</a></li>
<li><a href="#3">在2018</a></li>
<li><a href="#4">能顺利</a></li>
<li><a href="#5">掌握</a></li>
<li><a href="#6">javaScript</a></li>
</ul>
</div>
<script>
var oBar=document.getElementById("bar"),
oRight=oBar.getElementsByClassName("r")[0],
oLeft=oBar.getElementsByClassName("l")[0],
oUl=document.getElementById("ul"),
aLi=oUl.getElementsByTagName("li"),
iNow=1;//表示当前哪项高亮
oBar.style.left=aLi[iNow].offsetLeft+"px";
oBar.style.width=aLi[iNow].offsetWidth+"px";
for(var i=0;i<aLi.length;++i){
aLi[i].index=i;
aLi[i].onmouseover=function(){
lesgo(this);
};
}
oUl.onmouseout=function(){
lesgo(aLi[iNow]);
};
function lesgo(oLi){//效果主要函数
var t=Math.abs(oLi.offsetLeft-oBar.offsetLeft)*0.7;
move(oBar,{left:oLi.offsetLeft,width:oLi.offsetWidth},t,"easeOut",function(){
move(oRight,{width:0,right:0},80,"linear",function(){
oRight.style.background="#f90";
move(oRight,{width:20,right:-10},80,"linear",function(){
move(oRight,{width:0,right:0},50,"linear");
});
});
move(oLeft,{width:0,left:0},80,"linear",function(){
oLeft.style.background="#f90";
move(oLeft,{width:20,left:-10},80,"linear",function(){
move(oLeft,{width:0,left:0},50,"linear");
});
});
});
if(oLi.offsetLeft > oBar.offsetLeft){
console.log(oLi.offsetLeft);
console.log(oBar.offsetLeft);
setColor('#333','#f90'); }else if(oLi.offsetLeft < oBar.offsetLeft){
setColor('#f90','#333');
}
function setColor(color1,color2){
oRight.style.background = color1;
oLeft.style.background = color2;
move(oRight,{width:40,right:-20},150);
move(oLeft,{width:40,left:-20},150);
}
} //下面都是运动库相关move(元素,属性json,时间,运动类型,运动结束函数)
function move(t, n, e, r, a) {
clearInterval(t.iTimer);
var u = e || 1e3,
i = {};
for (var o in n) i[o] = {}, "opacity" == o ? (i[o].b = Math.round(100 * css(t, o)), i[o].c = 100 * n[o] - i[o].b) : (i[o].b = parseInt(css(t, o)), i[o].c = n[o] - i[o].b);
var r = r || "linear",
c = (new Date).getTime();
t.iTimer = setInterval(function() {
var e = (new Date).getTime() - c;
e >= u && (e = u);
for (var o in n) {
var f = Tween[r](e, i[o].b, i[o].c, u);
"opacity" == o ? (t.style[o] = f / 100, t.style.filter = "alpha(opacity=" + f + ")") : t.style[o] = f + "px"
}
e == u && (clearInterval(t.iTimer), a && a.call(t))
}, 14) }
function css(t, n) {
return t.currentStyle ? t.currentStyle[n] : getComputedStyle(t, !1)[n]
}
var Tween = {
linear: function(t, n, e, r) {
return e * t / r + n
},
easeIn: function(t, n, e, r) {
return e * (t /= r) * t + n
},
easeOut: function(t, n, e, r) {
return -e * (t /= r) * (t - 2) + n
},
easeBoth: function(t, n, e, r) {
return (t /= r / 2) < 1 ? e / 2 * t * t + n : -e / 2 * (--t * (t - 2) - 1) + n
},
easeInStrong: function(t, n, e, r) {
return e * (t /= r) * t * t * t + n
},
easeOutStrong: function(t, n, e, r) {
return -e * ((t = t / r - 1) * t * t * t - 1) + n
},
easeBothStrong: function(t, n, e, r) {
return (t /= r / 2) < 1 ? e / 2 * t * t * t * t + n : -e / 2 * ((t -= 2) * t * t * t - 2) + n
},
elasticIn: function(t, n, e, r, a, u) {
if (0 === t) return n;
if (1 == (t /= r)) return n + e;
if (u || (u = .3 * r), !a || a < Math.abs(e)) {
a = e;
var i = u / 4
} else var i = u / (2 * Math.PI) * Math.asin(e / a);
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin(2 * (t * r - i) * Math.PI / u)) + n
},
elasticOut: function(t, n, e, r, a, u) {
if (0 === t) return n;
if (1 == (t /= r)) return n + e;
if (u || (u = .3 * r), !a || a < Math.abs(e)) {
a = e;
var i = u / 4
} else var i = u / (2 * Math.PI) * Math.asin(e / a);
return a * Math.pow(2, -10 * t) * Math.sin(2 * (t * r - i) * Math.PI / u) + e + n
},
elasticBoth: function(t, n, e, r, a, u) {
if (0 === t) return n;
if (2 == (t /= r / 2)) return n + e;
if (u || (u = .3 * r * 1.5), !a || a < Math.abs(e)) {
a = e;
var i = u / 4
} else var i = u / (2 * Math.PI) * Math.asin(e / a);
return 1 > t ? -.5 * a * Math.pow(2, 10 * (t -= 1)) * Math.sin(2 * (t * r - i) * Math.PI / u) + n : a * Math.pow(2, -10 * (t -= 1)) * Math.sin(2 * (t * r - i) * Math.PI / u) * .5 + e + n
},
backIn: function(t, n, e, r, a) {
return "undefined" == typeof a && (a = 1.70158), e * (t /= r) * t * ((a + 1) * t - a) + n
},
backOut: function(t, n, e, r, a) {
return "undefined" == typeof a && (a = 2.0158), e * ((t = t / r - 1) * t * ((a + 1) * t + a) + 1) + n
},
backBoth: function(t, n, e, r, a) {
return "undefined" == typeof a && (a = 1.70158), (t /= r / 2) < 1 ? e / 2 * t * t * (((a *= 1.525) + 1) * t - a) + n : e / 2 * ((t -= 2) * t * (((a *= 1.525) + 1) * t + a) + 2) + n
},
bounceIn: function(t, n, e, r) {
return e - Tween.bounceOut(r - t, 0, e, r) + n
},
bounceOut: function(t, n, e, r) {
return (t /= r) < 1 / 2.75 ? 7.5625 * e * t * t + n : 2 / 2.75 > t ? e * (7.5625 * (t -= 1.5 / 2.75) * t + .75) + n : 2.5 / 2.75 > t ? e * (7.5625 * (t -= 2.25 / 2.75) * t + .9375) + n : e * (7.5625 * (t -= 2.625 / 2.75) * t + .984375) + n
},
bounceBoth: function(t, n, e, r) {
return r / 2 > t ? .5 * Tween.bounceIn(2 * t, 0, e, r) + n : .5 * Tween.bounceOut(2 * t - r, 0, e, r) + .5 * e + n
}
};
</script>

更多前端效果:https://xue-y.github.io/fore-end/

js 弹性导航的更多相关文章

  1. js实现滑动的弹性导航

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Js弹性漂浮广告代码

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  3. js实现导航固定定位

                                                                                   js实现导航固定定位 <!DOCTY ...

  4. js二级导航下拉菜单

    <!DOCTYPE html> <html> <head> <title>导航列表</title> <meta http-equiv= ...

  5. js二级导航

    js写二级导航要点 1.ul li 2.js获取元素 3.setInterval(function(),time); 代码如下 <style type="text/css"& ...

  6. 用js实现导航菜单点击切换选中时高亮状态

    随着用户点击导航或菜单上不同的页面,出现此选项高亮显示或变为一个新的样式是经常用到的.实现它所用的原理就是通过js中的location.href得到当前页面的地址,然后在与导航上的链接地址匹对,相同的 ...

  7. jquery.nav.js定位导航滚动插件

    jQuery.nav.js插件代码: /* * jQuery One Page Nav Plugin * http://github.com/davist11/jQuery-One-Page-Nav ...

  8. js动态控制导航栏样式

    导航栏一般做为母版页,为了使增加用户体验,往往在用户进入某个页面给予导航栏相应的样式,这里可以用js动态添加 <div class="box_left fl"> < ...

  9. JS面向对象方法(一): 使用原生JS 实现导航栏下多级分类弹出效果

    利用二级菜单的onmouseover/out事件 重新构建一级菜单 ".hover" 样式类 代码如下: CSS部分: 在原来的目标:hover样式中 增加 .hover状态 li ...

随机推荐

  1. [Selenium]Grid模式下运行时打印出当前Case在哪台node机器上运行

    当Case在本地运行成功,在Grid模式下运行失败时,我们需要在Grid模式下进行调试,同时登录远程的node去查看运行的情况. Hub是随机将case分配到某台node上运行的,怎样知道当前的cas ...

  2. UVa 11419 SAM I AM (最小覆盖数)

    题意:给定一个 n * m 的矩阵,有一些格子有目标,每次可以消灭一行或者一列,问你最少要几次才能完成. 析:把 行看成 X,把列看成是 Y,每个目标都连一条线,那么就是一个二分图的最小覆盖数,这个答 ...

  3. UVa 247 Calling Circles (DFS+Floyd)

    题意:如果两个人互通电话,那么他们就在一个电话圈里,现在给定 n 个人,并且给定 m 个通话记录,让你输出所有的电话圈. 析:刚开始没想到是Floyd算法,后来才知道是这个算法,利用这个算法进行连通性 ...

  4. 异步IO原理及相应函数

    何为异步IO? (1)几乎可以认为:异步IO就是操作系统用软件实现的一套中断响应系统.(2)异步IO的工作方法是:我们当前进程注册一个异步IO事件(使用signal注册一个信号 SIGIO的处理函数) ...

  5. msfvenom木马生成+免杀+壳(实测并不能免杀)

    msfvenom 选项: -p, --payload 有效载荷使用.指定一个有效的自定义载荷 --payload-options 列出有效载荷的标准选项 -l, --list [type] 列出一个模 ...

  6. shell脚本生成xml文件

    今天把这段时间学习完shell后完成工作上的一个小案件整理了一下,分享给大家! 说来也巧了,作为一个刚刚毕业半年的菜鸟,进入公司后,听公司的大牛推荐学习linux--”鸟哥的私房菜“,基本上是从去年8 ...

  7. SDWebImage从缓存中获取图片

      if ([[SDImageCache sharedImageCache] imageFromKey:sort.imageUrl]) {         [cell.photoImageView s ...

  8. 20155308 2016-2017-2 《Java程序设计》第8周学习总结

    20155308 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO(New IO)-from JDK1.4 NIO2 -fr ...

  9. Android-工作总结-LX-2018-08-20-setHint

    问题的因素: 调试了一下午,我一直是以为是数据没有传递过来,然后一直在数据获取环节检查在检查,数据跟踪在跟踪,最后发现数据获取是OK

  10. Android-自定义开关(升级版)

    效果图: 定义一个类,取名为MySwitch.java,此类去继承View,为何是继承View而不是去继承ViewGroup呢,是因为自定义开关没有子控件,之需要操作自身绘制即可 package cu ...