如何在单击“搜索”按钮时显示叠加层?

时间:2016-08-19 18:18:45

标签: javascript jquery css

我希望能够在单击搜索按钮时显示叠加层。

我是Jquery / JavaScript的新手,根据我在堆栈溢出和google上学习的内容,我能够提出以下代码:

function displayOverlay(text) {
    $("<table id='overlay'><tbody><tr><td>" + text + "</td></tr></tbody></table>").css({
        "position": "fixed",
        "top": "0px",
        "left": "0px",
        "width": "100%",
        "height": "100%",
        "background-color": "rgba(0,0,0,.6)",
        "z-index": "10000",
        "vertical-align": "middle",
        "text-align": "center",
        "color": "#fff",
        "font-size": "40px",
        "font-weight": "bold",
        "cursor": "wait",
    }).appendTo(".btn");
}

function removeOverlay() {
    $("#overlay").remove();
}

function closeOverlay{
	if($(".btn").data('clicked')){
		displayOverlay("Loading...");
	}
	else{
		$("#overlay").remove();
	}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<div class="panel panel-default">
	<div class="panel-body">
<!---	<div id="loader" style="position: fixed; top:0; left:0; width:100%; height: 100%; background: url('loader.gif') center center #efefef"></div><!--Progress bar--->
		<form name="providerSearch" ng-submit="SearchProvider(searchParam);" novalidate="" role="form">
			<div class="form-group"><input class="form-control" id="physiciansfirstname" ng-model="searchParam.FirstName" placeholder="First name:" type="text" /></div>

			<div class="form-group"><input class="form-control" id="physicianslastname" ng-model="searchParam.LastName" placeholder="Last name:" type="text" /></div>

			<div class="form-group"><select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty"><option disabled="disabled" selected="selected" value="">Specialty</option>
            <option value=""></option><option>Family practice</option><option>General practice</option><option>Internal medicine</option><option>Pediatrics</option> </select></div>

			<div class="form-group">
             <SELECT name="proCity" class="form-control" id="city" placeholder="City" ng-model="searchParam.City">
					   <option disabled="disabled" selected="selected" value="">City</option> 
						  <option value=""></option>
						  <cfoutput query="cityFind">
                           <option value=#city#>#city#</option>
						</cfoutput> 
					  </select>
                      
            <!---<select class="form-control" id="city" ng-model="searchParam.City"><option disabled="disabled" selected="selected" value="">City</option><option ng-repeat="c in Cities">{{c.City}}</option> </select>---->
            </div>

			<div class="row">
				<div class="col-xs-6 no-right-padding paddingLanguage">
					<div class="form-group widthLanguage">
              
                      
                    <select name="language" class="form-control" ng-model="searchParam.Language">
					    <option disabled="disabled" selected="selected" value="">Language</option>
                        <option value=""></option>
					 <cfoutput query="Languages">
						  <option value=#Language#>#Language#</option>
						</cfoutput> 
					  </select>
                      
                      
                      
					  <!---<select name="language" class="form-control widthLanguage" id="language" ng-model="searchParam.Language">
					    <option disabled="disabled" selected="selected" value="">Language</option>
					    <option ng-repeat="l in Languages">{{l.Lang}}</option>
				      </select>--->
					</div>
				</div>

				<div class="col-xs-6 no-left-padding">
					<div class="form-group"><select class="form-control" name="gender" ng-model="searchParam.Gender">
                    <option disabled="disabled" selected="selected" value="">Gender</option>
                    <option value=""></option>
                    <option>Male</option><option>Female</option> </select></div>
				</div>
			</div>
			
			<hr class="hrDoctor" />
			<div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div>
			
			<div class="row">
				<div class="col-xs-7 no-right-padding">
					<div class="form-group">
						<div class="input-group"><select class="form-control" name="distance" ng-model="searchParam.distance"><option selected="selected">5</option><option selected="selected">10</option><option selected="selected">15</option><option selected="selected">20</option> </select>

							<div class="input-group-addon">mi</div>
						</div>
					</div>
				</div>

				<div class="col-xs-5 no-left-padding widthZip">
					<div class="form-group"><input allow-pattern="[\d\-]" class="form-control" id="zip" maxlength="5" ng-model="searchParam.Zip" placeholder="Zip code" type="text" data-default=""/></div>
				</div>
			</div>

			<div class="form-group"><input class="btn btn-warning btn-block" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" /></div>
			<!---<div class="form-group buttonWidth resetButton"><input class="btn btn-primary btn-block" type="reset" value="Reset"  onClick="window.location.reload()"/></div>--->
		</form>
	<!---</div><!---Progress bar--->--->
	</div>
</div>

但是,单击搜索按钮时不会显示叠加层。

更新

以下是调用正文并在访问页面时显示叠加层的情况。

$(function () {
    $("body").click(function () {
        if ($("#overlay").length > 0) {
            removeOverlay();
        } else {
            displayOverlay("Loading...");
        }
    });
});

这是我调用setTimeout()函数的行:

<div class="form-group"><input class="btn btn-warning btn-block ignore" ng-click="gotoElement('SearchResultsAnchor');" onclick="setTimeout(overlayDisplayButton,3000)" type="submit" value="Search" /></div>

我目前更新了我所做的事情,这就是我用来显示叠加层的内容:

    function displayOverlay(text) {
    $("<table id='overlay'><tbody><tr><td>" + text + "</td></tr></tbody></table>").css({
        "position": "fixed",
        "top": "0px",
        "left": "0px",
        "width": "100%",
        "height": "100%",
        "background-color": "rgba(0,0,0,.6)",
        "z-index": "10000",
        "vertical-align": "middle",
        "text-align": "center",
        "color": "#fff",
        "font-size": "40px",
        "font-weight": "bold",
        "cursor": "wait",
        "overflow-y":"hidden"
    }).appendTo("body");
}

function removeOverlay() {
    $("#overlay").remove();
}

$(function overlayDisplayButton() {
    $(".btn").click(function () {
        if ($("#overlay").length > 0) {
            removeOverlay();
        } else {
            displayOverlay("Loading...");
        }
        displayOverlay("Loading...");
    });
});
</script>

我所做的小改变

    $(function overlayDisplayButton() {
    $("#submit").click(function () {
        if ($("#overlay").length > 0) {
            removeOverlay();
        } else {
           displayOverlay("Loading...")
        }
    });

});

我仍然不明白在检索数据时如何进行回调并在屏幕上显示,以便叠加层消失

任何帮助都会受到赞赏。 感谢

1 个答案:

答案 0 :(得分:0)

只需使用div和css和js的组合,您就可以更轻松地创建html用户交互阻止层。您只需显示或隐藏此类的div。我没有看到任何理由使用桌子。您只需将该div放在页面底部,其中没有任何内容。 css将它浮动在其他所有内容之上,它扩展到覆盖所有内容。 css高度100%仍然需要js的一点帮助,因为当我调用窗口改变尺寸时它不会自动调整。

css:

div.BlockUserInteractionWithPage {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 2000;
    padding-top: 0px;
    padding-right: 0px;
    padding-bottom: 0px;
    padding-left: 0px;
    background-color: #ffffff;
    opacity: 0.0;
    filter: alpha(opacity=0); /* filter:alpha for IE8 and earlier */
    height: 100%;
    width: 100%;    
}

js:

show

var pageLength = document.body.clientHeight;        
var blockingLayerObject = $("#blockingLayer")[0];
blockingLayerObject.style.height=pageLength;
$(blockingLayerObject ).show();

hiding is easy of course, once it is hidden it is no longer blocking

html:

<div id="blockingLayer" class="BlockUserInteractionWithPage" style="display:none;"></div>