Advertisement
aeroson

jquery alert

Jan 30th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * jAlert v.1.0.0
  3. * Copyright (c) 2008 Julian Castaneda
  4. * http://www.smooka.com/blog/2009/02/22/jalert-jquery-alert-box-plugin/
  5. * Requires: jQuery 1.2+  
  6. *
  7. * Edit / Tweak by aeroson
  8. * example call  $('.lalal').alert("This is a jAlert success Box", { type:'success', timeout:'5000', width:'350', y_offset:0 } );
  9. * alert ( msg [, options ] )
  10. * options => type (default 'fatal') , timeout (default 0 = no timeout), width (default 350), y_offset (default -4-box height }
  11. */
  12.  
  13.  
  14. $(window).resize(function() {  
  15.   $(".msg-box-cont").each(function( index ) {
  16.     //align center all
  17.     var width = $(this).width();
  18.     var container=$("#"+$(this).attr('cont'));
  19.     var container_width = container.innerWidth();
  20.     var container_left = container.position().left;
  21.     var actual_left = ((container_width-width)/2)+container_left;
  22.     $(this).css("left",actual_left+"px");
  23.   });
  24. });
  25.  
  26.  
  27. (function($) {
  28.  
  29.     $.fn.alert = function (msg, options)
  30.     {
  31.        
  32.       if(!options) options=0;
  33.          
  34.       var container = this;
  35.         var overlay = 0;
  36.  
  37.         var uid = this.attr('id');
  38.        
  39.         if ($('#jalert_cont_'+uid).css('display') == 'block')
  40.         {    
  41.         milisec=(new Date()).getTime();
  42.         $('#jalert_cont_'+uid).attr('id','jalert_cont_t'+milisec);
  43.         $('#jalert_close_'+uid).attr('id','jalert_close_t'+milisec);
  44.         remove($('#jalert_cont_t'+milisec),500);
  45.         }
  46.        
  47.       var type=options.type||'fatal';
  48.  
  49.       var width=options.width||350;
  50.  
  51.        
  52.         if (overlay==1) {
  53.             $('<div id="jalert_overlay_'+uid+'"></div>').prependTo('body');
  54.                 var overlayWidth = $(window).width();
  55.                 var overlayHeight = $(document).height();
  56.                 var winHeight =  $(window).height();
  57.                 $("#jalert_overlay_"+uid).css({
  58.                       top: 0,
  59.                       left: 0,
  60.                       width: overlayWidth,
  61.                       height: winHeight,
  62.                       position: "fixed",
  63.                       display: "block",
  64.                       background: "#000",
  65.                       zIndex: "1000"
  66.                   });
  67.                 $("#jalert_overlay_"+uid).css("opacity", 0.7);
  68.         }
  69.        
  70.       $('<div class="msg-box-cont msg-'+type+'" cont="'+uid+'" id="jalert_cont_'+uid+'"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td><div class="msg-text"><div class="msg-icon msg-icon-'+type+'"></div>'+msg+'</div></td><td width="21" valign="top"><div id="jalert_close_'+uid+'" class="msg-btn close-'+type+'"></div></td></tr></table></div>').appendTo('body');
  71.        
  72.       var jqobj=$("#jalert_cont_"+uid);
  73.        
  74.       jqobj.width(width);
  75.        
  76.       //align center      
  77.             var width = jqobj.width();
  78.       var container_width = container.innerWidth();
  79.       var container_left = container.position().left;
  80.       var actual_left = ((container_width-width)/2)+container_left;
  81.       jqobj.css("left",actual_left+"px");
  82.      
  83.       var y_offset = options.y_offset||-4-jqobj.height();
  84.  
  85.      
  86.         var top=this.position().top + y_offset;
  87.  
  88.         jqobj.css("top",(top+20)+"px").animate({top:top+"px"},{duration:500,queue:false}).fadeIn(500);
  89.              
  90.    
  91.         $(document).click(function() {
  92.             $("#jalert_overlay_"+uid).fadeOut(100);
  93.             $("#jalert_overlay_"+uid).remove();
  94.         });
  95.    
  96.    
  97.         $('#jalert_close_'+uid).click(function() {
  98.         remove(jqobj);        
  99.         });
  100.      
  101.       if (options.timeout) {
  102.         var timeoutId=setTimeout(function(){
  103.           remove(jqobj);
  104.         },options.timeout);
  105.       };
  106.      
  107.      
  108.       function remove(id,t) {
  109.         if (t==undefined) t=300;      
  110.         if (timeoutId!=undefined) window.clearTimeout(timeoutId);
  111.         id.fadeOut(t, function(){
  112.           id.empty().remove();        
  113.         });  
  114.       }
  115.      
  116.     };
  117.  
  118. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement