获取页面位置和div位置

var ch=(document.documentElement.scrollTop+document.documentElement.clientHeight-box.offsetHeight);
var cw=(document.documentElement.scrollLeft+document.documentElement.clientWidth-box.offsetWidth);
var top=parseInt(box.style.top);
var left=parseInt(box.style.left);

ch=页面高度
cw=页面宽度
top=id为box的div高度
left=id为box的div宽度

var ch=(document.documentElement.scrollTop+document.documentElement.clientHeight-favoriteDiv.offsetHeight);
var cw=(document.documentElement.scrollLeft+document.documentElement.clientWidth-favoriteDiv.offsetWidth);
var top = parseInt(favoriteDiv.style.top);
var left = parseInt(favoriteDiv.style.left);
var speed=1;
if(top>=ch && left<=cw)
{
favoriteDiv.style.top = ch;
favoriteDiv.style.left = left;
}
else if(top <= ch && left >= cw)
{
favoriteDiv.style.top = top;
favoriteDiv.style.left = cw;
}
else if(top >= ch && left >= cw)
{
favoriteDiv.style.top = ch;
favoriteDiv.style.left = cw;
}

你可能感兴趣的:(JavaScript)