扩展输入时隐藏标题,输入最小化时显示标题

时间:2017-11-07 05:43:40

标签: javascript jquery html css material-design

我有一个示例代码,显示带有css动画的搜索查询。

我想知道,如何在展开搜索查询时隐藏“标题”类,并在搜索查询最小化时再次显示它?

以下是两种状态的示例图像(正如您可以看到“标题”可在搜索文本后面查看,我不想要):

enter image description here

我知道在jquery中我可以像这样隐藏它(再次显示它我不知道逻辑会如何):

$("p").hide();

但我怎么能用CSS做到这一点?

该代码片段正在运行,但它无法正常显示,但我不知道原因。

$('.opener').click(function() {
  $(this).parent('.search').toggleClass('inactive').find('input').focus();
});
.row {
  border: 1px solid red;
}

.title {
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  margin: 6px 0;
}

.input-group {
  display: flex;
  height: 36px;
  width: 100%;
  overflow: hidden;
  padding: 0;
  /*background-color: rgba(100, 100, 100, .1);*/
  cursor: pointer;
  /*border-radius: 2px;*/
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
}

.input-group:active,
.input-group:hover {
  /*background-color: rgba(100, 100, 100, .1);*/
}

.input-group.centered {
  margin: 0 auto;
}

.input-group .input-group-addon {
  justify-content: center;
  width: 36px;
  background: transparent;
  border: none;
  display: flex;
  text-align: center;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.input-group .input-group-addon i {
  height: 24px;
  width: 24px;
  align-self: center;
  color: #777;
}

.input-group input {
  font-family: "Roboto", sans-serif;
  background: transparent;
  border: none;
  box-shadow: none !important;
  font-size: 16px;
  padding: 0;
}

.inactive {
  width: 36px;
  border-radius: 18px;
  background: transparent;
  box-shadow: none;
}

.inactive .input-group-addon:not(:first-child) {
  opacity: 0;
}

.row {
  margin-top: 1em;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="container">
    <div class="row text-center">
        <h1>Search Inputs</h1>
    </div>
    <div class="row">
        <div class="title">Title</div>
        <div class="input-group search inactive pull-right">
            <span class="input-group-addon opener">
              <i class="material-icons">search</i>
            </span>
            <input type="text" class="form-control" placeholder="Search">
            <span class="input-group-addon opener">
              <i class="material-icons">clear</i>
            </span>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以使用siblings选择div.title。 使用display:none属性添加另一个类并切换该类

&#13;
&#13;
$("p").hide();
$('.opener').click(function() {
  $(this).parent('.search').toggleClass('inactive').find('input').focus();
  $(this).parent('.search').siblings('div.title').toggleClass('hideTitle')
});
&#13;
.row {
  border: 1px solid red;
}

.title {
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  margin: 6px 0;
}

.input-group {
  display: flex;
  height: 36px;
  width: 100%;
  overflow: hidden;
  padding: 0;
  /*background-color: rgba(100, 100, 100, .1);*/
  cursor: pointer;
  /*border-radius: 2px;*/
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
}

.input-group:active,
.input-group:hover {
  /*background-color: rgba(100, 100, 100, .1);*/
}

.input-group.centered {
  margin: 0 auto;
}

.input-group .input-group-addon {
  justify-content: center;
  width: 36px;
  background: transparent;
  border: none;
  display: flex;
  text-align: center;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.input-group .input-group-addon i {
  height: 24px;
  width: 24px;
  align-self: center;
  color: #777;
}

.input-group input {
  font-family: "Roboto", sans-serif;
  background: transparent;
  border: none;
  box-shadow: none !important;
  font-size: 16px;
  padding: 0;
}

.inactive {
  width: 36px;
  border-radius: 18px;
  background: transparent;
  box-shadow: none;
}

.inactive .input-group-addon:not(:first-child) {
  opacity: 0;
}

.row {
  margin-top: 1em;
}
.hideTitle{
 display:none;

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<div class="container">
  <div class="row text-center">
    <h1>Search Inputs</h1>
  </div>

  <div class="row">
    <div class="title titleDiv">Title</div>
    <div class="input-group search inactive pull-right">
      <span class="input-group-addon opener">
				<i class="material-icons">search</i>
			</span>
      <input type="text" class="form-control" placeholder="Search">

      <span class="input-group-addon opener">
				<i class="material-icons">clear</i>
			</span>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将标签和输入包含在一个div中,位置相对并使用siblings jquery来隐藏标题

&#13;
&#13;
$('.opener').click(function() {
  $(this).parent('.search').toggleClass('inactive').find('input').focus();
  $(this).parent('.search').siblings('label').toggleClass('hide');
});
&#13;
.row {
  border: 1px solid red;
}
.search-form {
  position:relative;
}

.search-form .title {
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  margin: 6px 0;
}

.input-group {
  display: flex;
  height: 36px;
  width: 100%;
  overflow: hidden;
  padding: 0;
  /*background-color: rgba(100, 100, 100, .1);*/
  cursor: pointer;
  /*border-radius: 2px;*/
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
}

.input-group:active,
.input-group:hover {
  /*background-color: rgba(100, 100, 100, .1);*/
}

.input-group.centered {
  margin: 0 auto;
}

.input-group .input-group-addon {
  justify-content: center;
  width: 36px;
  background: transparent;
  border: none;
  display: flex;
  text-align: center;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.input-group .input-group-addon i {
  height: 24px;
  width: 24px;
  align-self: center;
  color: #777;
}

.input-group input {
  font-family: "Roboto", sans-serif;
  background: transparent;
  border: none;
  box-shadow: none !important;
  font-size: 16px;
  padding: 0;
}

.inactive {
  width: 36px;
  border-radius: 18px;
  background: transparent;
  box-shadow: none;
}

.inactive .input-group-addon:not(:first-child) {
  opacity: 0;
}

.row {
  margin-top: 1em;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="container">
    <div class="row text-center">
        <h1>Search Inputs</h1>
    </div>
    <div class="row">
        <div class="search-form">
          <label class="title">Title</label>
          <div class="input-group search inactive pull-right">
              <span class="input-group-addon opener">
                <i class="material-icons">search</i>
              </span>
              <input type="text" class="form-control" placeholder="Search">
              <span class="input-group-addon opener">
                <i class="material-icons">clear</i>
              </span>
          </div>
         </div>
    </div>
</div>
&#13;
&#13;
&#13;