单击时滑出输入字段

时间:2017-04-25 05:14:13

标签: javascript jquery html css

每当我点击搜索图标时,我都会将搜索输入字段设置为不透明度0。

我想让它从右边滑动而不是只是出现。每当我点击X图标或元素外部(文档中的任何位置)时,再次向后滑动

实现这一目标的最佳方法是什么?通过jQuery或CSS动画?

DEMO https://jsfiddle.net/p1zLh65g/4/

HTML

<nav class="navbar navbar-default">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="modal" data-target="#modal_sidebar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">
        <img src="img/logo.png" alt="">
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown nav-categories"><a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-bars"></i>أقسام </a>
          <ul class="dropdown-menu">
            <li><a href="#">رأي وتحليل</a></li>
            <li><a href="#">جدل</a></li>
            <li><a href="#">سلطة خامسة</a></li>
            <li><a href="#">تحقيق</a></li>
            <li><a href="#">ثقافة</a></li>
            <li><a href="#">بصريات</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-left">
        <li class="nav-date"><a href="#">22 مارس 2017</a></li>
        <li><a href="#">EN </a></li>
        <li><a href="#">تسجيل الدخول</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#" class="slide"><i class="fa fa-search search-icon"></i></a></li>
      </ul>
      <form class="navbar-form" role="search">
        <div class="input-group">
          <input type="text" class="form-control pull-right search" placeholder="عن ماذا تبحث؟">
          <span class="input-group-btn"></span>
        </div>
      </form>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

CSS

.search {
    opacity: 0;
}

input.form-control.pull-right.search.expanded {
    border-radius: 0;
    width: 300px;
}

.search.expanded {
    opacity: 1;
}

.nav-categories {
    margin-left: 15px;
}

的Javascript

$('.search-icon').click(function () {
  $('.search').toggleClass('expanded');
  $('.search-icon').toggleClass('fa-times');
});

1 个答案:

答案 0 :(得分:2)

  

这是一个简单的例子,使用简单的css3过渡和你的   jquery和bootstrap代码。

我刚刚添加了transition property,使animation能够正常工作。

.search {
    opacity: 0;
    -webkit-transition: all .5s ease;
   -moz-transition: all .5s ease;
   transition: all .5s ease;
   width:0px;
}

以下是工作代码段jsFiddle