A jQuery plug-in to control the dynamic centering of web page elements

From , 5 Years ago, written in JavaScript, viewed 94 times.
URL https://pastebin.vip/view/c404a5ad
  1. (function($)
  2. {
  3.     $.fn._center = function(self, parent, dimension)
  4.     {
  5.         if(!dimension.vertical && !dimension.horizontal)
  6.             return; //won't do anything anyway
  7.        
  8.         if(parent)
  9.             parent = self.parent();
  10.         else
  11.             parent = window
  12.        
  13.         self.css("position", "absolute");
  14.        
  15.         if(dimension.vertical)
  16.         {
  17.             self.css("top", Math.max(0, (($(parent).height() - $(self).outerHeight()) / 2) +
  18.                                                         $(parent).scrollTop()) + "px");
  19.         }
  20.        
  21.         if(dimension.horizontal)
  22.         {
  23.             self.css("left", Math.max(0, (($(parent).width() - $(self).outerWidth()) / 2) +
  24.                                                         $(parent).scrollLeft()) + "px");
  25.         }
  26.        
  27.         return self;
  28.     };
  29.    
  30.     $.fn.center = function(parent, args)
  31.     {
  32.         if(!args)
  33.         {
  34.             args = {horizontal: true, vertical: true};
  35.         }
  36.        
  37.         return this.each(function()
  38.         {
  39.             var obj = $(this);
  40.            
  41.             obj._center(obj, parent, args);
  42.            
  43.             function callback()
  44.             {
  45.                 obj._center(obj, parent, args);
  46.             }
  47.            
  48.             callback();
  49.             $(window).resize(callback);
  50.         });
  51.     };
  52. })(jQuery);
  53. //javascript/8234

Reply to "A jQuery plug-in to control the dynamic centering of web page elements"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website