First you have to normalize what element the tip is being positioned in. I'd go for the body, and you're using the variable tip as an undeclared global, that's not the best idea as it may conflict with other global variables and/or not represent what you expect it to all of the time.
I'd try something like so, get rid of this (from elsewhere in the page's source code):
Code:
<script type='text/javascript'>
$('.tip_trigger').hover(function()
{
// retrieve the image inside this element
var elem = $(this).find('img');
// set the 'src' to the 'data-original' value
elem.attr('src', elem.data('original'));
});
</script>
And change the code from your post to:
Code:
<script type="text/javascript">
$(document).ready(function() {
//Tooltips
$(".tip_trigger").hover(function(){
// retrieve the tip image that was inside this element
var elem = $(this).data('tip');
// set the 'src' to the 'data-original' value
elem.attr('src', elem.data('original'));
}).each(function(){
var trig = $(this);
trig.data('tip', trig.find('.tip')); // create portable reference to the tip
$('body').append(trig.data('tip')); //normalize position by appending to the body
}).hover(function(){
$(this).data('tip').show(); //Show tooltip
}, function() {
$(this).data('tip').hide(); //Hide tooltip
}).mousemove(function(e) {
var mousex = e.pageX + 10; //Get X coodrinates
var mousey = e.pageY + 10; //Get Y coordinates
var tip = $(this).data('tip');
if(!tip || !tip.size || !tip.size()){return;}
var tipWidth = tip.width(); //Find width of tooltip
var tipHeight = tip.height(); //Find height of tooltip
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() + $(window).scrollLeft() - (mousex + tipWidth);
//Distance of element from the bottom of viewport
var tipVisY = $(window).height() + $(window).scrollTop() - (mousey + tipHeight);
if ( tipVisX < tipWidth ) { //If tooltip exceeds the X coordinate of viewport
mousex = e.pageX - tipWidth - 10;
} if ( tipVisY < 90 ) { //If tooltip exceeds the Y coordinate of viewport
mousey = e.pageY - tipHeight - 10;
}
//Absolute position the tooltip according to mouse position
tip.css({ top: mousey, left: mousex });
});
});
</script>
Bookmarks