// javascript document jquery.fn.float= function(settings){ if(typeof settings == "object"){ settings = jquery.extend({ //延迟 delay : 1000, //位置偏移 offset : { left : 0, right : 0, top : 0, bottom : 0 }, style : null, //样式 width:100, //宽度 height:200, //高度 position:"rm" //位置 }, settings || {}); var winw = $(window).width(); var winh = $(window).height(); //根据参数获取位置数值 function getposition($applyto,position){ var _pos = null; switch(position){ case "rm" : $applyto.data("offset","right"); $applyto.data("offsetpostion",settings.offset.right); _pos = {right:settings.offset.right,top:winh/2-$applyto.innerheight()/2}; break; case "lm" : $applyto.data("offset","left"); $applyto.data("offsetpostion",settings.offset.left); _pos = {left:settings.offset.left,top:winh/2-$applyto.innerheight()/2}; break; case "rb" : _pos = {right:settings.offset.right,top:winh - $applyto.innerheight()}; break; case "lb" : _pos = {left:settings.offset.left,top:winh - $applyto.innerheight()}; break; case "l" : _pos = {left:settings.offset.left,top:settings.offset.top}; break; case "r" : _pos = {right:settings.offset.right,top:settings.offset.top}; break; case "t" : $applyto.data("offset","top"); $applyto.data("offsetpostion",settings.offset.top); _pos = {left:settings.offset.left,top:settings.offset.top}; break; case "b" : $applyto.data("offset","bottom"); $applyto.data("offsetpostion",settings.offset.bottom); _pos = {left:settings.offset.left,top:winh - $applyto.innerheight()}; break; } return _pos; } //设置容器位置 function setposition($applyto,position,isuseanimate){ var scrolltop = $(window).scrolltop(); var scrollleft = $(window).scrollleft(); var _pos = getposition($applyto,position); _pos.top += scrolltop; isuseanimate && $applyto.stop().animate(_pos,settings.delay) || $applyto.css(_pos); } return this.each(function(){ var $this = $(this); $this.css("position","absolute"); settings.style && $this.css(settings.style); setposition($this,settings.position); $(this).data("isallowscroll",true); $(window).scroll(function(){ $this.data("isallowscroll") && setposition($this,settings.position,true); }); }) }else{ var speed = arguments.length > 1 && arguments[1] || "fast"; this.each(function(){ if(settings == "clearoffset"){ var _c = {}; if($(this).data("offset")){ _c[$(this).data("offset")] = 0; $(this).data("isallowscroll",false); $(this).stop().animate(_c,speed); } }else if(settings == "addoffset"){ var _c = {}; if($(this).data("offset") && $(this).data("offsetpostion")){ _c[$(this).data("offset")] = $(this).data("offsetpostion"); $(this).stop().animate(_c,speed); $(this).data("isallowscroll",true); } }else if(settings == "setscrolldisable"){ $(this).data("isallowscroll",false); }else if(settings == "setscrollusable"){ $(this).data("isallowscroll",true); } }) } }