Modal window (popup) and background

I want to create a modal dialog when I clicked buy now on the store page, so I implemented this:

jQuery(function($){               //Lorsque vous cliquez sur un lien de la classe btn-default $('a.btn-default').on('click', function() {  var popID = $(this).data('rel'); //Trouver la pop-up correspondante  var popWidth = $(this).data('width'); //Trouver la largeur  //Faire apparaitre la pop-up et ajouter le bouton de fermeture  $('#' + popID).fadeIn().css({ 'width': popWidth}).prepend('<a href="#" class="close"><img src="./assets/img/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
  
  //Récupération du margin, qui permettra de centrer la fenêtre - on ajuste de 80px en conformité avec le CSS
  var popMargTop = ($('#' + popID).height() + 80) / 2;
  var popMargLeft = ($('#' + popID).width() + 80) / 2;
  
  //Apply Margin to Popup
  $('#' + popID).css({    'margin-top' : -popMargTop,   'margin-left' : -popMargLeft  });
  
  //Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) pour corriger les bogues d'anciennes versions de IE
  $('body').append('<div id="fade2"></div>');
  $('#fade2').css({'filter' : 'alpha(opacity=80)'}).fadeIn();    return false; });
	
	
 //Close Popups and Fade2 Layer
 $('body').on('click', 'a.close, #fade2', function() { //Au clic sur le body...  $('#fade2 , .popup_block').fadeOut(function() {   $('#fade2, a.close').remove();   }); //...ils disparaissent ensemble    return false; });	})(jQuery);
      

#fade2 {    
display: none;    
background: #000;     
position: fixed; left: 0; top: 0;     
width: 100%; height: 100%;    
opacity: .80;    
z-index: 999;  }

.popup_block{    
display: none;    
background: #fff;    
padding: 20px;      
border: 20px solid #ddd;    
float: left;    
font-size: 1.2em;    
position: fixed;    
top: 50%; 
left: 50%;    
z-index: 9999999 !important;    
-webkit-box-shadow: 0px 0px 20px #000;    
-moz-box-shadow: 0px 0px 20px #000;    
box-shadow: 0px 0px 20px #000;    
-webkit-border-radius: 10px;    
-moz-border-radius: 10px;    
border-radius: 10px;}

img.btn_close {    
float: right;     
margin: -55px -55px 0 0;}

.popup p {    
padding: 5px 10px;    
margin: 5px 0;}
/*--Making IE6 Understand Fixed Positioning--*/
*html #fade2 {    position: absolute;}
*html .popup_block {    position: absolute;}
      

<div class="shop-button">
  <a href="#" data-width="500" data-rel="popup1" class="btn btn-default btn-sm">Buy now
  </a>
</div>
      

Run codeHide result


But it doesn't work, I've tried a lot of things with z-index and other things in CSS because the windows appear behind the background of my popup. I would like to do something like this: http://sohtanaka.developpez.com/tutoriels/javascript/creez-fenetre-modale-avec-css-et-jquery/fichiers/

Hope you help me thanks to Liang

+3


source to share


1 answer


I made a sample code for you

$(document).ready(function(){
						   		   
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="http://www.cyrankakolno.pl/img/close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
        	$('a.close, #fade').click(function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
        
		return false;
	});
	
	
	//Close Popups and Fade Layer


	
});
      

#fade {
	display: none;
	background: #000; 
	position: fixed; left: 0; top: 0; 
	z-index: 10;
	width: 100%; height: 100%;
	opacity: .80;
	z-index: 9999;
}
.popup_block{
	display: none;
	background: #fff;
	padding: 20px; 	
	border: 20px solid #ddd;
	float: left;
	font-size: 1.2em;
	position: fixed;
	top: 50%; left: 50%;
	z-index: 99999;
	-webkit-box-shadow: 0px 0px 20px #000;
	-moz-box-shadow: 0px 0px 20px #000;
	box-shadow: 0px 0px 20px #000;
	-webkit-border-radius: 10px;
	-moz-border-radius: 10px;
	border-radius: 10px;
}
img.btn_close {
	float: right; 
    width:50px;
	margin: -55px -55px 0 0;
}
.popup p {
	padding: 5px 10px;
	margin: 5px 0;
}
/*--Making IE6 Understand Fixed Positioning--*/
*html #fade {
	position: absolute;
}
*html .popup_block {
	position: absolute;
}
      

<a href="#?w=500" rel="popup1" class="poplight">Width = 500px</a>

<div id="popup1" class="popup_block">
    <h2>Popup #1</h2>


    
</div>
      

Run codeHide result




Here is a jsFiddle: Example

Also you can have a look at Bootstrap Modals which is a great library.

0


source







All Articles