创建Hover Meaga菜单 - 单击其他位置

时间:2016-08-02 10:15:31

标签: jquery css

我知道stackoverflow有一些关于它的主题,但我的代码与它们不同。

我想做这项工作

  • 如果我点击搜索框(该网站的白色方块)打开我的搜索框
  • 如果我打开搜索框,点击其他人,我的搜索框关闭

但是我的搜索框没有打开。在这里你可以找到我的代码。

code

您可以找到我的fiddle

1 个答案:

答案 0 :(得分:0)

使用以下点击事件& CSS

$('.nav-search').click(function() {
  $('.search-content').toggleClass('cliked-search');
});
$('*').click(function(e) {
  if ($(e.target).is(':not(.nav-search *,.search-content *)')) {
    $('.search-content').removeClass('cliked-search');
  }
});

的CSS:

.cliked-search {
  transform: scaleY(1);
  -webkit-transform: scaleY(1);
  opacity:1;
}

演示:https://jsfiddle.net/u8t3k7jx/1/



$('.nav-search').click(function() {
  $('.search-content').toggleClass('cliked-search');
});
$('*').click(function(e) {
  if ($(e.target).is(':not(.nav-search *)') && $(e.target).closest('.search-content').length == 0) {
    $('.search-content').removeClass('cliked-search');
  }
});

/* Style */


/* -------------------- */


/* ----- Elements ----- */


/* color : #3498db , #e67e22
/* -------------------- */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  direction: rtl
}

@font-face {
  font-family: 'Yekan';
  src: url(/fonts/Yekan.eot);
  src: local('Yekan'), url(fonts/Yekan.woff) format('woff'), url(fonts/Yekan.ttf) format('truetype');
  font-weight: 400;
  font-style: normal
}

@font-face {
  font-family: 'Samim’;
 src: url(fonts/Samim/Samim.eot?#) format('eot'), url(fonts/Samim/Samim.woff) format('woff'), url(‘fonts/Samim/Samim.ttf’) format('truetype');
}

html {
  font-family: 'Samim', 'Yekan', sans-serif;
  overflow-x: hidden;
  background-color: #f2f2f2;
  font-size: 16px;
  text-rendering: optimizeLegibility;
}

.clearfix {
  zoom: 1;
}

.clearfix:after {
  content: '.';
  clear: both;
  display: block;
  height: 0;
  visibility: hidden
}

.row {
  width: 1191px;
  margin: 0 auto;
}

section {
  padding: 80px 0
}

.box {
  padding: 1%
}


/* ----------------- */


/* ------ Nav ------ */


/* ----------------- */

.up-nav {
  display: block;
  height: 40px;
  background-color: #111111;
  font-size: 90%;
}

.up-nav a {
  text-decoration: none;
  color: #777;
  display: inline-block;
  transition: color 0.22s;
}

.up-nav a:hover,
.up-nav a:active {
  color: #fc3d28;
}

.up-nav li {
  list-style: none;
  line-height: 40px;
  margin-left: 10px;
}

.right-up-nav li:first-child {
  margin-right: 10px;
}

.right-up-nav li {
  float: right;
}

.left-up-nav li {
  float: left;
}

.left-up-nav li:first-child {
  margin-left: 10px;
}

.down-nav {
  display: block;
  height: 44px;
  background-color: #19232d;
  border-bottom-color: #19232d;
}

.down-nav-category {
  margin-right: 0;
  margin-left: 50px;
}

#navigation .down-nav {
  margin-top: -2px;
}

.down-nav li {
  list-style: none;
  display: inline-block;
  line-height: 44px;
  padding: 0 15px;
}

.down-nav li i {
  line-height: 44px;
  padding-right: 2px;
  ;
  vertical-align: middle;
  font-size: 90%;
}

.down-nav a {
  text-decoration: none;
  color: #fff;
}

.down-nav>.row {
  position: relative;
  height: 44px;
}

#navigation {
  border-bottom-color: #c8c8c8;
  border-top-color: #000;
  background: #fff;
  border: 1px solid #dfdfdf;
  border-left: 0;
  border-right: 0;
  height: 50px;
  line-height: 44px;
  position: relative;
  z-index: 110;
  clear: both;
}

#navigation .nav-buttons {
  right: auto;
  left: 0;
  top: 0;
  position: absolute;
}

.nav-button:first-child {
  border-left-width: 1px;
}

#navigation .nav-button {
  background: #fff;
  color: #000;
  border-top: 1px solid #e4e4e4;
  border-bottom: 1px solid #e4e4e4;
  border-color: #e4e4e4;
  height: 44px;
  line-height: 44px;
  position: relative;
  float: left;
  border: 1px solid #dfdfdf;
  min-width: 48px;
  text-align: center;
  cursor: pointer;
  z-index: 1001;
  font-size: 23px;
  font-weight: normal;
}

.nav-button i {
  transition: all 0.2s;
}

.nav-button i:hover {
  color: #e54e53;
}


/* ------ Search Box ------ */

.search-content {
  position: absolute;
  width: 306px;
  right: auto;
  left: 0px;
  top: 100%;
  opacity: 0;
  transform: scaleY(0);
  -webkit-transform: scaleY(0);
  -moz-transform: scaleY(0);
  -webkit-transition: all 0.2s ease-in;
  -moz-transition: all 0.2s ease-in;
  -webkit-transition: all .2s ease-in;
  transition: all 0.2s ease-in;
  border-top: 0;
}

.sw-show {
  transform: scaleY(1);
  -webkit-transform: scaleY(1);
  -moz-transform: scaleY(1);
  opacity: 1;
}

.active {
  border-bottom-color: #fff;
}

.bk-search-box {
  background-color: #fff;
  border: 0.1px solid #e0e0e0;
  padding: 15px;
}

.search-item-form {
  overflow: hidden;
  position: relative;
}

input {
  height: 40px;
  padding: 0 8px;
}

.text-box-search {
  width: 227px;
  float: right;
  border-right-width: 1px;
  border-left-width: 0;
  background: #f8f8f8;
  border: 1px solid #eaeaea;
  font-family: 'Samim';
  font-size: 90%;
}

.button-search {
  height: 40px;
  background-color: #19232d;
  width: 45px;
  float: left;
  line-height: 38px;
  border: 0;
  background: #19232d;
  color: #fff;
  font-size: 90%;
  transition: all 0.2s ease-in;
  cursor: pointer;
}

.button-search:hover {
  background: #e54e53;
  color: #fff;
}


/* ------ Mega Menu ------ */

.mega-menu-mom,
.mega-menu-mom2 {
  position: absolute;
  visibility: hidden;
  opacity: 0;
  -webkit-transform: scaleY(0);
  transform: scaleY(0);
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  background-color: #fff;
  width: 98.8%;
  top: 100%;
  left: 0;
  right: auto;
  z-index: 1001;
  border: 1px solid #dfdfdf;
  border-top: 0;
  height: 211px;
  box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.06);
  line-height: 1.4;
  box-sizing: border-box;
  overflow: hidden;
  -webkit-transition: all 0.2 ease-in;
  transition: all 0.28 ease-in-out;
}

.mega-menu-sw {
  opacity: 1;
  visibility: visible;
  -webkit-transform: scaleY(1);
  transform: scaleY(1);
  transition: all 0.28s ease-in;
}

.item-of-5 {
  width: 20%;
  height: 211px;
  display: block;
  float: right;
}

.mega-menu-bitem {
  height: 211px;
}

.mega-menu-mom a {
  text-decoration: none;
}

.mega-menu-mom img {
  margin-top: 12px;
  max-width: 100%;
  height: 139px;
}

.p-mega-menu {
  color: #000;
  line-height: 145%;
  margin-top: 5px;
}

.cliked-search {
  transform: scaleY(1);
  -webkit-transform: scaleY(1);
  opacity: 1;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <div class="header">

    <div class="up-nav">

      <div class="row">

        <ul class="right-up-nav">

          <li>
            <a href="#">
                                    About Us
                                    </a>
          </li>
          <li>
            <a href="#">
                                    News
                                    </a>
          </li>

        </ul>
        <ul class="left-up-nav">

          <li>
            <a href="#">
                                    Login
                                    </a>
          </li>
          <li>
            <a href="#">
                                    Sign Up
                                    </a>
          </li>

        </ul>

      </div>

    </div>

  </div>

  <nav class="nav" id="navigation">

    <div class="down-nav">

      <div class="row">

        <ul class="down-nav-category">

          <li>
            <a href="#" class="new-posts">
                                            New Posts
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>

            <div class="mega-menu-mom">

              <ul class="mega-menu-bitem">

                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>

              </ul>

            </div>

          </li>
          <li class="itnews" id="itnews">
            <a href="#" class="it">
                                            IT 
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>
            <div class="mega-menu-mom2">

              <ul class="mega-menu-bitem">

                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>
                <li class="item-of-5">

                  <a href="#"><img class="img-mega-menu"></a>
                  <a href="#">
                    <p class="p-mega-menu"></p>
                  </a>

                </li>

              </ul>

            </div>
          </li>
          <li class="leraningnews">
            <a href="#" class="learn">
                                            Learn                           
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>
          </li>
          <li class="biologynews">
            <a href="#" class="biology">
                                            Biology 
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>
          </li>
          <li class="funnews">
            <a href="#" class="fun">
                                            Fun
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>
          </li>
          <li class="multimedianews">
            <a href="#" class="multimedia">
                                            MultiMedia
                                        <i class="ion-ios-arrow-down"></i>
                                        </a>

          </li>
        </ul>

        <div class="nav-buttons">

          <span class="nav-button nav-search">
                                        
                                        <li>
                                        <i class="ion-search"></i>
                                        </li>

                                    </span>
          <div class="search-content" id="search-content">

            <div class="bk-search-box">

              <div class="search-item-form">

                <form method="get" action="#">

                  <input class="text-box-search" type="text" placeholder="Search ..." autocomplete="off">
                  <button class="button-search" type="submit">
                    <i class="ion-search"></i>
                  </button>

                </form>

              </div>

            </div>

          </div>

        </div>

      </div>


    </div>


  </nav>

</body>
&#13;
&#13;
&#13;