javascript - 增加弹出背景的高度

标签 javascript jquery html css onload

我想增加弹出窗口背景的高度,同时从下拉列表中选择元素。选择元素时,弹出窗口的背景应该等于下拉列表的高度。

此外,如果我单击弹出背景的外部,它就会消失。我想显示弹出背景,直到从下拉列表中选择任何元素为止。

HTML

<div class="maintext">
    <h2> Main text goes here...</h2>
</div>
<div id="boxes">
    <div id="dialog" class="window">
        <div id="san">
            <section>
                <select class="cs-select cs-skin-elastic">
                    <option value="" disabled selected>Select a Country</option>
                    <option value="france" data-class="flag-france">France</option>
                    <option value="brazil" data-class="flag-brazil">Brazil</option>
                    <option value="argentina" data-class="flag-argentina">Argentina</option>
                    <option value="south-africa" data-class="flag-safrica">South Africa</option>
                </select>
            </section>
        </div>
    </div>
    <div style="width: 2478px; font-size: 32pt; color: white; height: 1202px; display: none; opacity: 0.4;" id="mask"></div>
</div>

CSS

#mask {
  position:absolute;
  left:0;
  top:0;
  z-index:9000;
  background-color:#26262c;
  display:none;
}  
#boxes .window {
  position:absolute;
  left:0;
  top:0;
  width:440px;
  height:850px;
  display:none;
  z-index:9999;
  padding:20px;
  border-radius: 5px;
  text-align: center;
}
#boxes #dialog {
  width:450px; 
  height:auto;
  padding: 10px 10px 10px 10px;
  background-color:#ffffff;
  font-size: 15pt;
}

.agree:hover{
  background-color: #D1D1D1;
}
.popupoption:hover{
    background-color:#D1D1D1;
    color: green;
}
.popupoption2:hover{
    color: red;
}

Jquery

$(document).ready(function() {  

        var id = '#dialog';

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(500); 
        $('#mask').fadeTo("slow",0.9);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);     

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });     

});

DEMO HERE

最佳答案

将此 javascript 添加到您的代码中,它将解决您的高度问题。如果尚未选择选择框,它也会限制关闭弹出窗口。从选择选项中选择任何值后,您将能够使弹出窗口消失。

$(document).ready(function() {  

    var id = '#dialog';

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect     
    $('#mask').fadeIn(500); 
    $('#mask').fadeTo("slow",0.9);  

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //transition effect
    $(id).fadeIn(2000);     

//if close button is clicked
$('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();

    $('#mask').hide();
    $('.window').hide();
});     

//if mask is clicked
$('#mask').click(function () {
   var val =  $( ".cs-select option:selected" ).text();
   if(val == 'Select a Country'){
    return;
    }
    $(this).hide();
    $('.window').hide();
});         

   $(document).click(function () {
       if (!$(".cs-select ").is(":focus")) {
        $('#dialog').css({'height':23});
       }else{
        var height = 17;
        $('.cs-select option').each(function (item) {
        height = height +17;
        })
       if($('#dialog').height() < 25){
       $('#dialog').css({'height':height});
      }else{
     $('#dialog').css({'height':23});
      }
     }
  });   

});

试试这个代码。它将根据选择输入中列出的选项设置弹出窗口的高度。无论有多少个选项,它都会动态计算高度。

关于javascript - 增加弹出背景的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42364904/

相关文章:

javascript - 创建一个通知栏

javascript正则表达式非捕获比根本没有括号更快

javascript - 定位和操作 setInterval 计时器值

Jquery获取JSON数据不起作用

javascript - 如何动态创建嵌入到页面的多个 YouTube 视频?

HTML CSS - Angular 对象具有不锐利的边缘

javascript - 如何使用 facebook javascript sdk 在页面粉丝选项卡内获取 facebook 页面 ID

html - 带有图像的复选框和选中状态的叠加图像

jquery - jQuery Mobile 未设置动态附加按钮的样式

javascript - 将带有文件输入的多个图像上传到 Canvas 元素