/*
* FileName:
* Author: Asfman
* Date:
* Contact:
http://jsframework.cn
* parameter:
* Need js:
*/
function StayPosition(speed)
{
this.objs = [];
this.speed = speed || 0.1;
this.timer = this.round = this.obj = this.end = null;
if(StayPosition.initialize !== true){
function correct(func, obj){
return function(){
func.call(obj);
}
}
StayPosition.prototype.start = function(){
this.timer = setInterval(correct(this.run, this), 33);
}
StayPosition.prototype.stop = function(){
clearInterval(this.timer);
}
StayPosition.prototype.capitalize = function(prop){return prop.replace(/^[a-z]/, function(a){return a.toUpperCase();})}
StayPosition.prototype.add = function(dom, prop){
var offset = prop ? "offset" + this.capitalize(prop) : "offsetTop";
var scroll = prop ? "scroll" + this.capitalize(prop) : "scrollTop";
prop = prop ? prop : this.offset.slice(6).toLowerCase();
this.objs.push({"dom": dom, "prop": {"size": dom[offset], "name": prop, "offset": offset, "scroll": scroll}});
}
StayPosition.prototype.run = function(){
for(var i = 0, l = this.objs.length; i < l; i++){
this.obj = this.objs[i];
this.end = (document.documentElement[this.obj.prop.scroll] || document.body[this.obj.prop.scroll]) + this.obj.prop.size;
if(this.end != this.obj.dom[this.obj.prop.offset]){
this.round = this.end - this.obj.dom[this.obj.prop.offset] > 0 ? Math.ceil : Math.floor;
this.obj.dom.style[this.obj.prop.name] = this.obj.dom[this.obj.prop.offset] + this.round((this.end - this.obj.dom[this.obj.prop.offset]) * this.speed) + "px";
}
}
}
}
StayPosition.initialize = true;
}
回复 更多评论