jquery.isonscreen.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Copyright (c) 2010
  2. * @author Laurence Wheway
  3. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  4. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  5. *
  6. * @version 1.2.0
  7. */
  8. (function($) {
  9. jQuery.extend({
  10. isOnScreen: function(box, container) {
  11. //ensure numbers come in as intgers (not strings) and remove 'px' is it's there
  12. for(var i in box){box[i] = parseFloat(box[i])};
  13. for(var i in container){container[i] = parseFloat(container[i])};
  14. if(!container){
  15. container = {
  16. left: $(window).scrollLeft(),
  17. top: $(window).scrollTop(),
  18. width: $(window).width(),
  19. height: $(window).height()
  20. }
  21. }
  22. if( box.left+box.width-container.left > 0 &&
  23. box.left < container.width+container.left &&
  24. box.top+box.height-container.top > 0 &&
  25. box.top < container.height+container.top
  26. ) return true;
  27. return false;
  28. }
  29. })
  30. jQuery.fn.isOnScreen = function (container) {
  31. for(var i in container){container[i] = parseFloat(container[i])};
  32. if(!container){
  33. container = {
  34. left: $(window).scrollLeft(),
  35. top: $(window).scrollTop(),
  36. width: $(window).width(),
  37. height: $(window).height()
  38. }
  39. }
  40. if( $(this).offset().left+$(this).width()-container.left > 0 &&
  41. $(this).offset().left < container.width+container.left &&
  42. $(this).offset().top+$(this).height()-container.top > 0 &&
  43. $(this).offset().top < container.height+container.top
  44. ) return true;
  45. return false;
  46. }
  47. })(jQuery);