如何在div上添加背景(灯箱效果)?

时间:2016-04-19 08:54:13

标签: html angularjs less lightbox opacity

我正在编辑AngularJS项目,我想在用户点击搜索输入框时在主DIV上添加背景(如Bootstrap Modal)。

enter image description here

以下是HTML代码:

    <div class="main-box">
        <div class="search-function" ng-click="showInputForm()">
            <img src="../images/my_project/search.png">
        </div>

        <div class="search-form" ng-if="showForm">
            <form ng-submit="textSearch()">
                <input type="text" autofocus class="search-input" ng-model="text.value" />
            </form>
        </div>
    </div>

正如您所见,当用户点击搜索图标时,会出现白色框,现在我想在主DIV上添加背景,白色框除外。

这是LESS代码:

.search-function {
  margin-left: 30%;
}

.search-form {
  padding-left: 15%;
  padding-right: 15%;
  position: relative;
  z-index: 0;

  .search-input {
    color: #2b84a6 !important;
    background-color: #fff !important;
    font-weight: 300;
    font-size: 16px;
  }
}

我怎么能达到这个目标?

2 个答案:

答案 0 :(得分:2)

像这样的东西

 <div class="main-box">
    <div class="backdrop" ng-if="showForm"></div>
    <div class="search-function" ng-click="showInputForm()">
        <img src="../images/my_project/search.png">
    </div>

    <div class="search-form" ng-if="showForm">
        <form ng-submit="textSearch()">
            <input type="text" autofocus class="search-input" ng-model="text.value" />
        </form>
    </div>
</div>

将CSS添加为

.backdrop {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: black;
    opacity: 0.5;
}

答案 1 :(得分:1)

这是一篇关于实现所需内容的不同方法的完整文章

CSS Overlay Techniques