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);//Current selected This.end = opt.end;//Callback 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 (); Slide to the first item by default 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"); Binding Events to 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<t.s.min&&c<0) { Movelastpos = T.s.min; } 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); Click on the Blank Space This.slider.click (function (e) { var mousepos = E.clientx, left = Mousepos-t.slider.offset (). Left + $ (window). ScrollLeft ()-T.MOVEWIDTH/2; if (left<t.s.min) {left = T.s.min;} if (Left>t.s.max) {left = T.s.max;} T.move.animate ({ ' Left ': Left },80,function () { T.check (); }) }) }, Check:function (t) { var T = this, X = This.move.offset (). Left, X,w,m, i = This.menu.first (), Last = This.menu.last (); if (X<first.offset (). left) { This.turn (first,t); Return } if (X>last.offset (). Left+last.outerwidth ()) { This.turn (last,t); Return } Matching menu items for (Var i=0;i<this.menu.length;i++) { m = This.menu.eq (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) {}; } } |