流界面之动态图标导航栏

news/2024/7/4 1:45:53 标签: function, firefox, ie, class, css, border
class="baidu_pl">
class="article_content clearfix">
class="tags" href="/tags/IE.html" title=ie>iews" class="htmledit_vclass="tags" href="/tags/IE.html" title=ie>iews">
class="postentry">

border="0" />做这个导航栏的初衷,是想在公司产品的后台界面里,模仿MAC OS X系统的dock效果,所谓dock,就是OS X桌面底部那条显眼的工具栏,我的UBUNTU桌面里也有类似的效果……

实际上我也没用过MAC,所以是凭想象做的,演示页面在此

右上角的导航栏是默认的效果,鼠标滑过时图标变大,会推挤旁边的图标。
下面第一排去掉了推挤效果。
第二排加入了左右移动的功能,图标的数量远远超出页面的宽度,只显示其中一部分,在导航栏上左右移动鼠标时,整个导航栏会向相反方向滑动,显示出隐藏的图标。
第三排把移动的操作改到了左右两侧的箭头,鼠标停留在箭头上时,导航栏就会向对应的方向滑动,鼠标离开箭头时滑动停止,当滑动到最末端的图标时,自动停止。

先实现导航栏的默认效果,HTML如下:

class="hl-surround">
    class="hl-main ln-show" title="Double click to hide line number.">
  1. class="hl-firstline"><div class="iconbarout">
  2.  
  3. <div id="iconbar0" class="iconbar" >
  4.  
  5.  <ul style="margin-left:0px;">
  6.  
  7.  <li>
  8.   <a href="#">
  9.  
  10.    <img src="images/icon7.png" style="width:68px;height:68px;" onload="alphaPNG(this);" onmouseover="largeIcon(this);" onmouseout="reIcon(this);" />
  11.  
  12.    <span>link7</span>
  13.   </a>
  14.  </li>
  15.  
  16.  
  17.  <li>
  18.   <a href="#">
  19.  
  20.    <img src="images/icon6.png" style="width:68px;height:68px;" onload="alphaPNG(this);" onmouseover="largeIcon(this);" onmouseout="reIcon(this);" />
  21.  
  22.    <span>link6</span>
  23.   </a>
  24.  </li>
  25.  
  26.  
  27.  </ul>
  28. </div>
  29.  
  30.  
  31. </div>

为了追求立体感,图标不能有背景色,所以必须用透明PNG图片,这里用<img>标签插入图片,是因为要实现图标的缩小放大效果(CSS背景里的图片只能控制位置,不能改变大小),图标的长宽要写在元素的style属性里,方便JS程序控制。

由于IE6不支持PNG的透明背景,必须用滤镜来做HACK才能达到一样的效果,而滤镜只能写在CSS的背景属性里…………为了解决这个矛盾,就要依靠JS了:

class="hl-surround">
    class="hl-main ln-show" title="Double click to hide line number.">
  1. class="hl-firstline">class="tags" href="/tags/FUNCTION.html" title=function>function alphaPNG(png)
  2. {
  3. var aVersion=navigator.appVersion.split("MSIE");
  4.  
  5. var version=parseInt(aVersion[1]);
  6. var pp=png.parentNode;
  7.  
  8. if( (version>=5.5) && (version<7) && (document.body.filters) )
  9.  
  10. {
  11.  var mout=png.onmouseout.toString();
  12.  
  13.  var mover=png.onmouseover.toString();
  14.  
  15.  alphaHTML="<span style=/"cursor:pointer;filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='"+png.src+"');display:block;width:"+png.style.width+";height:"+png.style.height+";/" οnmοuseοver=/""+mover.substring(mover.indexOf("{")+1,mover.lastIndexOf("}"))+"/" οnmοuseοut=/""+mout.substring(mout.indexOf("{")+1,mout.lastIndexOf("}"))+"/" ></span>";
  16.  
  17.  png.outerHTML=alphaHTML;
  18. }
  19.  
  20. }

这个函数已经写在图片的onload事件里了,会在IMG标签的内容加载完后触发(这个事件虽好,也不能到处乱用,根据伟大的犀牛书V5, onload只支持body, frameset, img),函数先判断浏览器类型和版本(IE7支持透明PNG),先创建一个span元素,IMG标签的所有属性、事件都原样COPY到SPAN上,而 PNG图片则放到SPAN的背景里,用滤镜处理成透明,注意滤镜属性里必须要有sizingMethod=scale。最后用outerHTML(只有 IE支持)把该IMG替换成span。

导航栏的样式:

class="hl-surround">
    class="hl-main ln-show" title="Double click to hide line number.">
  1. class="hl-firstline">.iconbarout
  2. {
  3. width:770px;
  4.  
  5. position:absolute;
  6. top:40px;right:10px;
  7.  
  8. }
  9.  
  10. .iconbar{
  11. overflow:hidden;
  12. float:left;
  13.  
  14. }
  15.  
  16. .iconbar li{
  17. float:right;
  18. padding:0px;
  19.  
  20. width:auto;
  21. text-align:center;
  22. margin:0 16px 0 0px;
  23.  
  24. }
  25. .iconbar li span{
  26. color:#333;
  27. font-weight:600;
  28.  
  29. width:auto;
  30. display:block;
  31. float:none;
  32.  
  33. margin:0px auto;
  34. }
  35. .iconbar li a img{
  36. margin:0px auto;
  37.  
  38. float:none;
  39. }
  40. .iconbar li a{
  41. text-align:center;
  42.  
  43. float:none;
  44. margin:0px auto;
  45. }
  46. .iconbar li img{
  47.  
  48. border:0px;
  49. float:left;
  50. }

这样就在IE和Firefox里实现一样的视觉效果了(这里存在一个问题,为了保证图标放大后的清晰度,初始的图标都是缩小的,在IE7和Firefox里都会看到一点锯齿,而IE6由于用了滤镜,缩小的图标边缘仍然是平滑的-_____-b)

接下来做鼠标滑过的放大缩小效果。JS的动画效果一般是利用setTimeout或setInterval的延迟功能,反复循环来实现的。为了避免这些图标在运行函数时发生冲突,我把缩放的动画效果封装到了一个类里:

class="hl-surround">
    class="hl-main ln-show" title="Double click to hide line number.">
  1. class="hl-firstline">/* zoom class by Dexter.Yy
  2. ********************************************************/
  3.  
  4. class="tags" href="/tags/FUNCTION.html" title=function>function animeZoom(ico,gWidth,gHeight,nWidth,nHeight)
  5.  
  6. {
  7. this.png=ico;
  8. this.iheight=nHeight;
  9.  
  10. this.iWidth=nWidth;
  11. this.goWidth=gWidth;
  12. this.goHeight=gHeight;
  13.  
  14. this.rico;
  15. }
  16.  
  17. animeZoom.prototype.zoomEvent=class="tags" href="/tags/FUNCTION.html" title=function>function()
  18.  
  19. {
  20. if(this.iWidth<this.goWidth)
  21. {
  22.  this.goEnlarge();
  23.  
  24. }
  25. else if(this.iWidth>this.goWidth)
  26. {
  27.  
  28.  this.goReduce();
  29. }
  30.  
  31. }
  32.  
  33. animeZoom.prototype.goEnlarge=class="tags" href="/tags/FUNCTION.html" title=function>function()
  34.  
  35. {
  36. this.png.style.width=this.goWidth+"px";
  37.  
  38. this.png.style.height=this.goWidth+"px";
  39.  
  40. }
  41.  
  42. animeZoom.prototype.goReduce=class="tags" href="/tags/FUNCTION.html" title=function>function()
  43. {
  44. var obj=this;
  45.  
  46. var h=parseInt(this.png.style.height);
  47.  
  48. var w=parseInt(this.png.style.width);
  49.  
  50. if(w>this.goWidth)
  51. {
  52.  this.png.style.width=w-2+"px";
  53.  
  54.  this.png.style.height=h-2+"px";
  55.  
  56.  this.rico=setTimeout(class="tags" href="/tags/FUNCTION.html" title=function>function(){obj.goReduce();},30);
  57.  
  58. }
  59. else
  60. {
  61.  window.clearTimeout(this.rico);
  62.  
  63. }
  64. }

使用时要传5个参数:目标对象、期望达到的宽度、期望达到的高度、原始宽度、原始高度,像这样:

class="hl-surround">
    class="hl-main ln-show" title="Double click to hide line number.">
  1. class="hl-firstline">class="tags" href="/tags/FUNCTION.html" title=function>function largeIcon(png)
  2. {
  3. var lIco=new animeZoom(png,91,91,68,68);
  4.  
  5. lIco.zoomEvent();
  6. }
  7.  
  8. class="tags" href="/tags/FUNCTION.html" title=function>function reIcon(png)
  9.  
  10. {
  11. var rIco=new animeZoom(png,68,68,91,91);
  12.  
  13. rIco.zoomEvent();
  14. }

OK完工……如果是在FLASH里做这种推挤效果,可能还要写一大堆AS,但这里有CSS的浮动属性帮忙,就很省事……

 

http://www.niftyadmin.cn/n/1412694.html

相关文章

DELPHI 注册表注册授权代码

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Registry ; //Registry ;注册表的引用 type TForm1 class(TForm) Button1: TButton; Button2: TButton; procedure Button2Click(Sender: TObject…

git 使用教程,常用命令

### git常用命令~~~git pull --rebase从服务器获得更新git status查看本地修改git add .git add xxx添加本地的修改git rm xxx删除一个本地修改git commit提交本地修改&#xff0c;但没有上传到服务器&#xff0c;只是本地的记录&#xff0c;git commit --amend&#xff0c;修…

JAVA IO - 从文件中读取数据的6种方法

JAVA IO - 从文件中读取数据的6种方法 // 1.Scanner Test void testReadFile1() throws IOException {//文件内容&#xff1a;Hello World|Hello ZimugString fileName "D:\data\test\newFile4.txt";try (Scanner sc new Scanner(new FileReader(fileName))) {whi…

Nginx+Tomcat(多实例)实现动静分离和负载均衡(四层、七层)

目录 一、Tomcat 多实例部署 二、反向代理的两种类型 三、NginxTomcat实现负载均衡和动静分离&#xff08;七层代理&#xff09; 1.动静分离和负载均衡原理 2.实现方法 3.部署实例 &#xff08;1&#xff09;部署Nginx负载均衡服务器 &#xff08;2&#xff09;配置Tom…

H3C IP地址动态获取过程

转载于:https://www.cnblogs.com/fanweisheng/p/11156612.html

JAVA IO - 删除文件或文件夹的7种方法

JAVA IO - 删除文件或文件夹的7种方法 //一、删除文件或文件夹的四种基础方法 //File类的delete() //File类的deleteOnExit() //Files.delete(Path path) //Files.deleteIfExists(Path path); //false只能告诉你失败了 &#xff0c;但是没有给出任何失败的原因 Test void test…

工作 5 年观察:快速在职场崛起,拼这 10 个认知

转自&#xff1a;http://app.myzaker.com/news/article.php?pk5cce592432ce40dc68000044 工作 5 年观察&#xff1a;快速在职场崛起&#xff0c;拼这 10 个认知05-05初入职场时&#xff0c;很多人差距并不大&#xff0c;但三四年后差距就很明显了&#xff0c;五到十年甚至有天…

Android库分析工具(崩溃反编译)

[时间&#xff1a;2016-07] [状态&#xff1a;Open] [关键词&#xff1a;android, 动态库&#xff0c;静态库, 编译&#xff0c;crash&#xff0c;addr2line] 本文主要整理Android编译系统中可用的库分析工作&#xff0c;可作为后续代码崩溃分析的参考。 动态库&#xff08;*.s…