You can use the width of the document to check how wide the html document is and adjust the left position accordingly. Say:
//set the left position var left = $(this).offset().left + 10; if(left + 200 > $(document).width()){ left = $(document).width() - 200; }
I used 200 here because you set your tooltip to be 200 pixels wide. Something similar can be done with height.
There is also window width, but I always confuse the difference, so you should check which one gives the best results.
An example of the bottom of the page:
//set the height, top position var height = $(this).height(); var top = $(this).offset().top; if(top + 200 > $(window).height()){ top = $(window).height() - 200 - height; }
Again, using 200, as you set your tooltip to a height of 200 pixels.
source share