0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-12-29

PullDown Menu by HTML + CSS + jQuery

This is demo

This is all Source Code

1. Prepare folder and files

First, please prepare project for pulldown practice.

├ assets /
│       ├ css /
│       │   ├ reset.css
│       │   ├ style.css
│       │
│       ├ js /
│           ├ app.js
├ index.html
スクリーンショット 2019-08-12 14.07.56.png

2. Prepare add HTML and reset css code.

index.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>
reset.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;
}

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

スクリーンショット 2019-08-12 14.09.15.png

3. Let's add CSS code.

 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;
}
スクリーンショット 2019-08-12 14.21.29.png

3. Hide children navs.

 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;
}
スクリーンショット 2019-08-12 14.22.54.png

4. add mouseover event to ul tag

 app.js
$(function() {
  $(".menu li").mouseenter(function(){
    console.log('mouseover');
  });
})

aaa.gif

5. display children navs

slideDown: Display the matched elements with a sliding motion.

about slideDown

 app.js
$(function() {
  $(".menu li").mouseenter(function(){
    $(this).find("ul").slideDown(150);
  });
})

aaa.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

hide: Hide the matched elements.

about find

 app.js
$(function() {
  $(".menu li").mouseenter(function(){
    $(this).siblings().find("ul").hide();
    $(this).find("ul").slideDown(150);
  });
})

aaa.gif

6. when leave nav, hide all children navs.

slideUp: Hide the matched elements with a sliding motion.

about slideUp

 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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?