[前端] js实现导航栏上下动画效果

2143 0
Honkers 2022-10-21 16:00:39 | 显示全部楼层 |阅读模式
本文实例为大家分享了js实现导航栏上下动画的具体代码,供大家参考,具体内容如下
obj.currentStyle[name] getComputedStyle(obj,false)[name],一个是支持IE 一个支持FE
完美运动js插件,能根据传进来的值,进行匹配,从而得到了理想的运动效果,也就是运行逻辑。
实现上下动画动画效果就是控制元素height的值,通过不断的修改该值,就能呈现所谓的动画效果,这里就需要用到定时器
定时器有两种
重复不断的定时器 setInterval(fn,time);
延时只在设定时间后只出现一次的定时器 setTimeout(fn,time)
在遍历传进的每一个值,需要使用一个boolean来控制是否完成解析,解析成功则返回true,结束定时器,返回false,知道返回true位置
  1. function getStyle(obj, attr)
  2. {
  3. if(obj.currentStyle)
  4. {
  5.   return obj.currentStyle[attr];
  6. }
  7. else
  8. {
  9.   return getComputedStyle(obj, false)[attr];
  10. }
  11. }
  12. function startMove(obj,json,fnEnd){
  13. if(obj.timer){
  14.   clearInterval(obj.timer);
  15. };
  16. obj.timer=setInterval(function(){
  17.   var bStop = true; //假设全部找到
  18.   for(var attr in json){
  19.    var icurr = 0;
  20.    if(attr=='opacity'){//匹配key
  21.     icurr=Math.round(parseFloat(getStyle(obj,attr))*100); //获取元素的属性值
  22.    }else{
  23.     icurr=parseInt(getStyle(obj,attr));
  24.    };
  25.    var sPeed = (json[attr]-icurr)/8;
  26.    sPeed=sPeed>0?Math.ceil(sPeed):Math.floor(sPeed);
  27.    if(attr=='opacity'){
  28.     obj.style.filter="alpha(opacity:"+(icurr+sPeed)+")";
  29.     obj.style.opacity=(icurr+sPeed)/100;
  30.    }else{
  31.     obj.style[attr]=icurr+sPeed+'px';
  32.    }
  33.    ificurr!=json[attr]){
  34.     bStop=false;
  35.    }
  36.   }
  37.   if(bStop){
  38.    clearInterval(obj.timer);
  39.    if(fnEnd){
  40.     fnEnd();
  41.    }
  42.   }
  43. },30);
  44. }
  45. // alert('dffe');
复制代码
html布局
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4.         <meta charset="UTF-8">
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.         <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.         <link rel="stylesheet" type="text/css" href="css/common.css" />
  8.         <script src="js/move.js"></script>
  9.         <script src="js/common.js"></script>
  10.         <title></title>
  11. </head>
  12. <body>
  13. <div id="box">
  14.         <div><a href="javascript:;" class="a01"></a><em>关于</em><span></span></div>
  15.         <div><a href="javascript:;" class="a02"></a><em>招聘</em><span></span></div>
  16.         <div><a href="javascript:;" class="a04"></a><em>产品</em><span></span></div>
  17.         <div><a href="javascript:;" class="a03"></a><em>公司</em><span></span></div>
  18. </div>       
  19. </body>
  20. </html>
复制代码
css文件
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. a{
  6. text-decoration: none;
  7. }
  8. body{
  9. background-color: #c1c1c1;
  10. }
  11. #box{
  12. width: 880px;
  13. margin: 100px auto;
  14. overflow: hidden;
  15. }
  16. #box div{
  17. width: 200px;
  18. height: 100px;
  19. float: left;
  20. overflow: hidden;
  21. position: relative;
  22. }
  23. #box div a{
  24. position: absolute;
  25. left: 0;
  26. top: 0;
  27. width: 200px;
  28. height: 100px;
  29. display: block;
  30. /* background-color: red; */
  31. background: url(../images/1.jpg) no-repeat;
  32. }
  33. #box div span{
  34. display: block;
  35. width: 200px;
  36. height: 100px;
  37. position: absolute;
  38. background: url(../images/window.png) left top repeat-x;
  39. }
  40. #box div em{
  41. display: block;
  42. width: 100%;
  43. height: 100%;
  44. background-color: #999;
  45. position: absolute;
  46. text-align: center;
  47. line-height: 100px;
  48. font-family: Verdana;
  49. font-style: normal;
  50. font-size: 30px;
  51. color: white;
  52. text-shadow: 2px 1px 4px black;
  53. top: 0;
  54. }
  55. #box div a.a01{
  56.   /* background: url(../images/1.jpg) 0 5px no-repeat; */
  57. background-position: 0 5px;
  58. }
  59. #box div a.a02{
  60.   /* background: url(../images/1.jpg) 0 5px no-repeat; */
  61. background-position: -200px 5px;
  62. }
  63. #box div a.a03{
  64.   /* background: url(../images/1.jpg) 0 5px no-repeat; */
  65. background-position: -400px 5px;
  66. }
  67. #box div a.a04{
  68.   /* background: url(../images/1.jpg) 0 5px no-repeat; */
  69. background-position: -600px 5px;
  70. }
复制代码
window.οnlοad=fn
  1. window.onload=function(){
  2. var oDiv = document.getElementById('box');
  3. var aDiv = oDiv.getElementsByTagName('div');
  4. var aEm = oDiv.getElementsByTagName('em');
  5.   var aEm = oDiv.getElementsByTagName('em');
  6. for(var i=0;i<aDiv.length;i++)
  7.   {
  8.    aDiv[i].index = i;
  9.    aDiv[i].onmouseover = function()
  10.    {
  11.     startMove(aEm[this.index],{top:-aEm[this.index].offsetHeight})
  12.    }
  13.    aDiv[i].onmouseout = function()
  14.    {
  15.     startMove(aEm[this.index],{top:0})
  16.    }
  17.   }
  18. }
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行