Let's create PullDown Menu by HTML + CSS + jQuery

# PullDown Menu by HTML + CSS + jQuery

This is [demo](https://wizardly-hamilton-398288.netlify.com/)

This is all [Source Code](https://github.com/obscure723/PullDownMenuByCssAndJquery)

### 1. Prepare folder and files
First, please prepare project for pulldown practice.

```
├ assets /
│       ├ css /
│       │   ├ reset.css
│       │   ├ style.css
│       │
│       ├ js /
│           ├ app.js
├ index.html
```

<img width="257" alt="スクリーンショット 2019-08-12 14.07.56.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/a47a8404-a8ca-b4dd-f2c5-27e4252d3f16.png">


### 2. Prepare add HTML and reset css code.

<details><summary>index.html</summary><div>


```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./assets/css/reset.css">
  <link rel="stylesheet" href="./assets/css/style.css">
</head>
<body>
  <header>
    <nav>
      <ul class="menu">
        <li><a href="#">Nav1</a></li>

        <li><a href="#">Nav2</a>
          <ul>
            <li><a href="#">Nav2-1</a></li>
            <li><a href="#">Nav2-2</a></li>
          </ul>
        </li>

        <li><a href="#">Nav3</a>
          <ul>
            <li><a href="#">Nav3-1</a></li>
            <li><a href="#">Nav3-2</a></li>
            <li><a href="#">Nav3-3</a></li>
          </ul>
        </li>
        <li><a href="#">Nav4</a></li>
      </ul>
    </nav>
  </header>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="./assets/js/app.js"></script>
</body>
</html>
```

</div></details>

<details><summary>reset.css</summary><div>


```css
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}

body {
line-height:1;
}

article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
display:block;
}

nav,ul {
list-style:none;
}

blockquote, q {
quotes:none;
}

blockquote:before, blockquote:after,
q:before, q:after {
content:'';
content:none;
}

a {
margin:0;
padding:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}

/ change colours to suit your needs /
ins {
background-color:#ff9;
color:#000;
text-decoration:none;
}

/ change colours to suit your needs /
mark {
background-color:#ff9;
color:#000;
font-style:italic;
font-weight:bold;
}

del {
text-decoration: line-through;
}

abbr[title], dfn[title] {
border-bottom:1px dotted;
cursor:help;
}

table {
border-collapse:collapse;
border-spacing:0;
}

/* change border colour to suit your needs
*/
hr {
display:block;
height:1px;
border:0;
border-top:1px solid #cccccc;
margin:1em 0;
padding:0;
}

input, select {
vertical-align:middle;
}

```

</div></details>

If you put these code, the site looks like this.

<img width="221" alt="スクリーンショット 2019-08-12 14.09.15.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/2ed65edf-cd5f-ee68-6217-028d1baf658e.png">


### 3. Let's add CSS code.

```css:&nbsp;style.css
.menu {
  width: 750px;
  margin: 0 auto;
  padding: 0;
  display: flex;
}

.menu li {
  display: block;
  width: 150px;
  height: 40px;
  text-align: center;
  background-color: aqua;
  line-height: 40px;
  border: 1px solid #eee;
  box-sizing: border-box;
  cursor: pointer;
}

.menu li a {
  display: block;
  width: 100%;
  text-decoration: none;
  color: #222;
}
```

<img width="635" alt="スクリーンショット 2019-08-12 14.21.29.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/214fdd73-a58e-1b94-922a-8d696372ac53.png">

### 3. Hide children navs.

```css:&nbsp;style.css
.menu {
  width: 750px;
  margin: 0 auto;
  padding: 0;
  display: flex;
}

.menu li {
  display: block;
  width: 150px;
  height: 40px;
  text-align: center;
  background-color: aqua;
  line-height: 40px;
  border: 1px solid #eee;
  box-sizing: border-box;
  cursor: pointer;
}

.menu li a {
  display: block;
  width: 100%;
  text-decoration: none;
  color: #222;
}

.menu li > ul {
  display: none;
}
```

<img width="635" alt="スクリーンショット 2019-08-12 14.22.54.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/300ee287-3dfd-dee1-3456-0a2ab873f3b5.png">

### 4. add mouseover event to ul tag

```javascript:&nbsp;app.js
$(function() {
  $(".menu li").mouseenter(function(){
    console.log('mouseover');
  });
})
```

![aaa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/4c9919c7-03cd-c0e6-32c1-d7f9d9481989.gif)

### 5. display children navs

slideDown: Display the matched elements with a sliding motion.

[about slideDown](https://api.jquery.com/slideDown/)

```javascript:&nbsp;app.js
$(function() {
  $(".menu li").mouseenter(function(){
    $(this).find("ul").slideDown(150);
  });
})
```

![aaa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/40088596-3384-8f41-c4a8-ee64dd3ee6ce.gif)

### 5. display children navs, hide another children navs

find: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

[about find](https://api.jquery.com/find/)

hide: Hide the matched elements.

[about find](https://api.jquery.com/hide/)

```javascript:&nbsp;app.js
$(function() {
  $(".menu li").mouseenter(function(){
    $(this).siblings().find("ul").hide();
    $(this).find("ul").slideDown(150);
  });
})
```

![aaa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/975fe909-71e1-57a4-904e-5febd035c9d1.gif)

### 6. when leave nav, hide all children navs.

slideUp: Hide the matched elements with a sliding motion.

[about slideUp](https://api.jquery.com/slideUp/)


```javascript:&nbsp;app.js
$(function() {
  $(".menu li").mouseenter(function(){
    $(this).siblings().find("ul").hide();
    $(this).find("ul").slideDown(150);
  });

  $(".menu").mouseleave(function() {
    $(".menu li").siblings().find("ul").slideUp(150);
  });
})
```

![aaa.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/475289/e7fe21fb-b39c-2f0b-6f98-4c269c7952e9.gif)