//对象悬浮在页面的某个位置(公共调用方法)
//悬浮对象
//对象在页面的位置(可为null)(格式{ifandtop:bool,ifandleft:bool,top:count,bottom:count,left:count,right:count}),第一二个参数必填:top是否加上页面被卷去的高;left是否加上页面被卷去的左
//外加方法(可为null)
//位置移动后需执行的方法(可为null)
function ObjectPositionToWindow(obj, parameter, fun, alfterfun) {
$(window).resize(function () {
ObjectPositionFunction(obj, parameter, alfterfun);
});
$(window).scroll(function () {
ObjectPositionFunction(obj, parameter, alfterfun);
});
if (fun != null) fun();
}
//对象悬浮在页面的有个位置(实现方法)
function ObjectPositionFunction(obj, parameter, alfterfun) {
obj.stop();
var windowTop = parameter.ifandtop != null && parameter.ifandtop ? $(document).scrollTop() : 0;
var windowLeft = parameter.ifandleft != null && parameter.ifandleft ? $(document).scrollLeft() : 0;
var windowseeHeight = $(window).height();
var windowseeWidth = $(window).width();
var toTop = parameter.top;
var toBottom = parameter.bottom;
var toLeft = parameter.left;
var toRight = parameter.right;
var objtop = 0, objleft = 0;
if (toTop == null && toBottom == null && toLeft == null && toRight == null) {
objtop = windowTop + windowseeHeight - obj.height();
objleft = windowLeft;
}
if ((toTop != null && toBottom == null) || (toTop != null && toBottom != null)) {
if ((toTop + obj.height()) > windowseeHeight) {
objtop = windowTop + windowseeHeight - obj.height();
}
else if ((toTop + obj.height()) < obj.height()) {
objtop = windowTop;
}
else {
objtop = windowTop + toTop;
}
}
if (toTop == null && toBottom != null) {
if (toBottom < obj.height()) {
objtop = windowTop + windowseeHeight - obj.height();
}
else if (toBottom > windowseeHeight) {
objtop = windowTop;
}
else {
objtop = windowTop + windowseeHeight - toBottom;
}
}
if ((toLeft != null && toRight == null) || (toLeft != null && toRight != null)) {
if ((toLeft + obj.width()) > windowseeWidth) {
objleft = windowLeft + windowseeWidth - obj.width();
}
else if ((toLeft + obj.width()) < obj.width()) {
objleft = windowLeft;
}
else {
objleft = windowLeft + toLeft;
}
}
if (toLeft == null && toRight != null) {
if (toRight < obj.width()) {
objleft = windowLeft + windowseeWidth - obj.width();
}
else if (toRight > windowseeWidth) {
objleft = windowLeft;
}
else {
objleft = windowLeft + windowseeWidth - toRight;
}
}
ObjectPositionAnimate(obj, { top: objtop, left: objleft }, alfterfun);
}
//对象悬浮在页面的有个位置(移动动画)
function ObjectPositionAnimate(obj, parameter, alfterfun) {
obj.animate(parameter, 500, "swing", function () { if (alfterfun != null) alfterfun(); });
}
jquery的悬浮层方法
原创
©著作权归作者所有:来自51CTO博客作者527087796的原创作品,谢绝转载,否则将追究法律责任
下一篇:C#反射
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章