I have a div on the right side of my content, using float:right;
The width of the div is fixed (265px), but the height is variable (it contains data filled up with php).
What I'm trying to achieve: if the div is NOT heigher then the browser window AND the width is completely visible (when the browser window is wide enough), the div should be position:fixed. In other cases it should be position:absolute, so the user can read everything after scrolling.
After reading some articles, including this one => Check if element is visible after scrolling, I was able to create something, but I'm not able to make it perfect.
There is always something that goes wrong. Either by scrolling fast, or resizing
function setPositionKasticket()
{
var docViewTop = $(window).scrollTop();
var docViewLeft = $(window).scrollLeft();
//var docViewTop = window.pageXOffset;
//var docViewLeft = window.pageYOffset;
var docViewBottom = docViewTop + $(window).height();
var docViewRight = docViewLeft + $(window).width();
var elemTop = $("#kasticket").offset().top;
var elemLeft = $("#kasticket").offset().left;
var elemBottom = elemTop + $("#kasticket").height();
var elemRight = elemLeft + $("#kasticket").width();
if ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) && (elemTop >= docViewTop) &&
(elemRight >= docViewLeft) && (elemLeft <= docViewRight) && (elemRight <= docViewRight) && (elemLeft >= docViewLeft))
{
$("#kasticket").css({position: 'fixed'});
}
else
{
$("#kasticket").css({position: 'absolute'});
}
}
window.onscroll=function(){
//setPositionKasticket();
//$("#kasticket").css({top: window.pageYOffset + 160});
if (($('#kasticket').css('position') == 'absolute') && (checkHeigth()))
{
$("#kasticket").animate({top: window.pageYOffset + 160},2);
}
}
window.onresize=function(){
setPositionKasticket();
}
window.onload=function(){
setPositionKasticket();
}