Skip to content

Instantly share code, notes, and snippets.

@Megic
Created October 29, 2012 02:21
Show Gist options
  • Save Megic/3971062 to your computer and use it in GitHub Desktop.
Save Megic/3971062 to your computer and use it in GitHub Desktop.
Jquery:float div
function Float(elementId,postion,styleCss){
this.id = elementId;
this.div = $("#"+this.id);
this.scrollTop = 0;
this.screenHeight = window.screen.height-window.screenTop;
this.topPx = rePosition(postion,this);
div.css({"position":"absolute","top":topPx+"px"});
if(arguments.length > 2)
$.each(styleCss,function(i,n){
div.css(i,n);
});
var scrollObj = $.browser.msie ? document.documentElement : document.body;
var Expression = null;
window.onscroll = function(){
this.scrollTop = scrollObj.scrollTop;
this.topPx = rePosition(postion,this);
div.stop(true).animate({"top":this.topPx},500);
}
}
function rePosition(postion,obj){
switch(postion){
case "TOP":
obj.topPx = obj.scrollTop;
break;
case "CENTER":
obj.topPx = (obj.scrollTop + ( obj.screenHeight / 2));
break;
case "BOTTOM":
obj.topPx = obj.screenHeight + obj.scrollTop - div.height()-60;
}
return obj.topPx;
}
//var div = Float("test","CENTER",{"color":"red"}); //使用
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment