【问题标题】:Increase height of the pop up background增加弹出背景的高度
【发布时间】:2017-02-21 10:34:53
【问题描述】:

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

此外,如果我单击弹出背景的外部,它就会消失。我想在下拉列表中选择任何项目之前显示弹出背景。

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 jquery html css onload


    【解决方案1】:

    将此javascript 添加到您的代码中,它将解决您的身高问题。如果尚未选择选择框,它还将限制关闭弹出窗口。一旦您从select options 中选择任何值,您就可以让您的弹出窗口消失。

    $(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});
          }
         }
      });   
    
    });
    

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

    【讨论】:

    • 一旦我选择下拉列表值,它工作正常..我再次选择相同的值它不工作
    • @IvinRaj 是的,你是对的,更新了我的答案。请立即检查。谢谢
    • @IvinRaj 在选项打开时单击选择框外仍然存在问题,解决了该问题。 :)
    • @IvinRaj 编辑了我的答案。您现在可以检查。如果您发现任何问题,请告诉我。谢谢 :)
    【解决方案2】:

    检查这个 sn-p,假设 &lt;select&gt; 高度是 20px。

    $(document).ready(function() {	
    
    		var id = '#dialog';
    	
    		//Get the screen height and width
    		var maskHeight = $(document).height();
    		var maskWidth = $(window).width();
    		
    		var selectElement = $('.cs-select');
    		
    		selectElement.parent('section').css('height', (20 * selectElement.children().length) + 'px');
    	
    		//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();
    	});		
    	*/
    	$(selectElement).on('change', function(){
    		$('#mask, .window').hide();
    	});
    });
    #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;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
                    <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>
                                        <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>

    【讨论】:

    • 我可以看到这里的高度是固定的 20 px 。我想动态增加高度作为下拉列表的高度@Vilas Kumkar
    • @IvinRaj 当你点击 中添加了更多选项,而 jQuery 代码完成了其余的事情来管理弹出窗口的高度。
    【解决方案3】:

    检查下面的sn-p,当用户选择国家时,你可以恢复dialogheight

    我已根据您的要求删除了以下代码

    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });
    

    $(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();
    	});*/		
      
      $("select.cs-select").on('click', function(){
      	$("#dialog").height($('select.cs-select option').length*22)
      })
      
    	
    });
    #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;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <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>

    【讨论】:

    • 它现在正在工作@aje,如果我点击弹出背景的外部,它就会消失。我想显示弹出背景,直到从下拉列表中选择任何项目。
    • 无论如何谢谢@aje
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2016-12-18
    • 2013-12-20
    • 2014-11-14
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多