最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
javascript实现块拖动导航菜单效果
时间:2012-01-01 编辑:简简单单 来源:一聚教程网
HTML代码
代码如下 | 复制代码 |
代码如下 | 复制代码 |
代码如下 | 复制代码 |
var sliderMenu = function(id,opt){ opt = opt ¦¦ {}; this.box = $("#"+id); this.menu = this.box.find(".menu li"); this.slider = this.box.find(".slider"); this.move = this.slider.find(".move"); this.moveWidth = this.move.outerWidth(); this.S = { min:0, max:this.slider.width() - this.moveWidth } this.on = this.menu.eq(0);//当前选中项 this.end = opt.end;//回调 this.bind(); } sliderMenu.prototype = { bind : function(){ var T = this; this.menu.click(function(){ T.turn($(this)); }) this.move.click(function(e){ stopBubble(e); }).hover(function(){ $(this).addClass("current"); },function(){ $(this).removeClass("current"); }); this.Move(); //默认滑动到第一项 window.setTimeout(function(){ T.turn(T.on); },500) }, Move : function(e){ var T = this, D = $(document), moveNowPos = null, moveLastPos = null, mouseLastPos = null, mouseNowPos = null, c, E = { state : false, timer : null, down : function(e){ E.state = true; e = e ¦¦ window.event; moveNowPos = parseInt(T.move.css("left")); mouseLastPos = e.clientX; T.slider.addClass("on_move"); //绑定事件到document D.unbind("mousemove").unbind("mouseup").bind("mousemove",E.move).bind("mouseup",E.up); if(e.preventDefault){ e.preventDefault(); }else{ e.returnValue = false; } }, move : function(e){ if(!E.state){return;} e = e ¦¦ window.event; mouseNowPos = e.clientX; c = mouseNowPos - mouseLastPos; moveLastPos = moveNowPos+c; if(moveLastPos } if(moveLastPos>T.S.max&&c>0){ c = T.S.max - moveNowPos; moveLastPos = moveNowPos+c; } T.move.css({ "left":moveLastPos }) if(E.timer) { window.clearTimeout(E.timer); } E.timer = window.setTimeout(function(){ T.check(false); },5) if(e.preventDefault){ e.preventDefault(); }else{ e.returnValue = false; } }, up : function(){ if(!E.state){return;} E.state = false; T.slider.removeClass("on_move"); D.unbind("mousemove").unbind("mouseup"); T.turn(T.on); } } this.move.bind("mousedown",E.down); //点击空白处 this.slider.click(function(e){ var mousePos = e.clientX, left = mousePos - T.slider.offset().left + $(window).scrollLeft() - T.moveWidth/2; if(left T.move.animate({ "left":left },80,function(){ T.check(); }) }) }, check : function(t){ var T = this, X = this.move.offset().left, x,w,m, first = this.menu.first(), last = this.menu.last(); if(X return; } if(X>last.offset().left+last.outerWidth()){ this.turn(last,t); return; } //匹配菜单项 for(var i=0;i if(m.hasClass("current")){continue;} w = m.outerWidth(); x = m.offset().left; if(X>=x&&X<=(x+w)){ this.turn(m,t); break; } } }, turn : function(j,t){ var x = j.offset().left, w = j.outerWidth(); j.addClass("current").siblings().removeClass("current"); if(t!==false){ this.move.animate({ "left":x+w/2-this.slider.offset().left-this.moveWidth/2 },200) } this.on = j; try{this.end();}catch(e){}; } } |
效果如下图
-
上一个: 仿hao123邮箱登录效果
-
下一个: linux中通过FTP下载指定的文件方法
相关文章
- 原生javascript延时导航菜单条效果 08-20
- Javascript 超级导航菜单效果代码 06-25
- javascript 树形导航菜单特效代码 05-21
- Javascript中可折叠式导航菜单效果 12-27
- JavaScript脚本制作二级导航菜单 04-13
- javasctipt 漂亮的感应鼠标导航菜单 04-13