模态窗口(弹出式)和背景
modal window (pop-up) and background
我想创建一个模态对话框,当我点击商店的页面上立即购买,所以我实现了:
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>
但它不起作用,我尝试了很多东西与z-index和其他东西在CSS,因为窗口出现在我的弹出的背景后面。我想做这样的东西:http://sohtanaka.developpez.com/tutoriels/javascript/creez-fenetre-modale-avec-css-et-jquery/fichiers/
我希望你能帮助我谢谢Leana我已经为你做了一个示例代码
$(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>
这是一个jsFiddle:示例
你也可以看看Bootstrap Modals,这是一个很好的库。
相关文章:
- 在元素悬停上显示带有javascript的弹出式网站
- Chrome扩展弹出式安装
- 普通(弹出式)Chrome 扩展程序与在开发者工具中添加标签页的扩展程序之间的区别
- 仅加载弹出式javascript
- Javascript弹出式Google Chrome扩展
- Javascript中的Mac/Windows弹出式虚拟键盘
- 弹出式浏览器兼容性
- 弹出式引导+棱角分明
- 弹出式视频播放器显示在Firefox中的奇数位置
- 打开弹出式html页面
- 弹出式签名不起作用
- Facebook 发送按钮 - 操作弹出式按钮
- 如何在 YII 中使用 ajaxvalidation 实现弹出式登录表单
- 在弹出式显示和隐藏上遇到样式问题
- 弹出式:每个会话和加载仅一次
- 后台代码在任何弹出式单击时运行
- 弹出式背景不'关闭时不要隐藏
- 禁用或不可访问的弹出式背景区域
- 模态窗口(弹出式)和背景
- 将背景设置为不透明度0.5,除了弹出式表单