Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee46f3f

Browse files
committed
How to create drop down, drop up, drop left and drop right
1 parent5d980ba commitee46f3f

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8"/>
5+
<metaname="viewport"content="width=device-width, initial-scale=1.0"/>
6+
<linkrel="stylesheet"href="style.css"/>
7+
<title>Dropdown, Dropup, Dropleft, Dropright</title>
8+
</head>
9+
<body>
10+
<divclass="container">
11+
<divclass="drop-wrapper">
12+
<buttonclass="btn"data-target="#dropleft">dropleft</button>
13+
<divclass="drop-menu dropleft"id="dropleft">
14+
<ahref="#">Link 1</a>
15+
<ahref="#">Link 2</a>
16+
<ahref="#">Link 3</a>
17+
<ahref="#">Link 4</a>
18+
<divclass="divider"></div>
19+
<ahref="#">Link 5</a>
20+
</div>
21+
</div>
22+
<divclass="drop-wrapper">
23+
<buttonclass="btn"data-target="#dropdown">dropdown</button>
24+
<divclass="drop-menu dropdown"id="dropdown">
25+
<ahref="#">Link 1</a>
26+
<ahref="#">Link 2</a>
27+
<ahref="#">Link 3</a>
28+
<ahref="#">Link 4</a>
29+
<divclass="divider"></div>
30+
<ahref="#">Link 5</a>
31+
</div>
32+
</div>
33+
<divclass="drop-wrapper">
34+
<buttonclass="btn"data-target="#dropup">dropup</button>
35+
<divclass="drop-menu dropup"id="dropup">
36+
<ahref="#">Link 1</a>
37+
<ahref="#">Link 2</a>
38+
<ahref="#">Link 3</a>
39+
<ahref="#">Link 4</a>
40+
<divclass="divider"></div>
41+
<ahref="#">Link 5</a>
42+
</div>
43+
</div>
44+
45+
<divclass="drop-wrapper">
46+
<buttonclass="btn"data-target="#dropright">dropright</button>
47+
<divclass="drop-menu dropright"id="dropright">
48+
<ahref="#">Link 1</a>
49+
<ahref="#">Link 2</a>
50+
<ahref="#">Link 3</a>
51+
<ahref="#">Link 4</a>
52+
<divclass="divider"></div>
53+
<ahref="#">Link 5</a>
54+
</div>
55+
</div>
56+
</div>
57+
58+
<scriptsrc="script.js"></script>
59+
</body>
60+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
constbtns=document.querySelectorAll('.btn');
2+
constdropMenus=document.querySelectorAll('.drop-menu');
3+
4+
btns.forEach(btn=>{
5+
btn.addEventListener('click',()=>{
6+
removeActive();
7+
btn.classList.add('active');
8+
document.querySelector(btn.dataset.target).classList.add('active');
9+
})
10+
})
11+
12+
constremoveActive=()=>{
13+
btns.forEach(btn=>btn.classList.remove('active'));
14+
dropMenus.forEach(dropmenu=>dropmenu.classList.remove('active'));
15+
}
16+
17+
window.onclick=(e)=>{
18+
if(!e.target.matches('.btn')){
19+
removeActive()
20+
}
21+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
body{
2+
display: grid;
3+
place-items: center;
4+
height:100vh;
5+
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
6+
}
7+
8+
.container{
9+
display: flex;
10+
justify-content: space-around;
11+
width:992px;
12+
}
13+
14+
.btn{
15+
padding:0.5rem1.5rem;
16+
border: none;
17+
outline: none;
18+
background-color:#0f81fb;
19+
color:#f3f3f3;
20+
border-radius:3px;
21+
}
22+
23+
.btn.active{
24+
box-shadow:0000.125remrgba(13,128,250,0.5);
25+
}
26+
27+
.drop-wrapper{
28+
position: relative;
29+
}
30+
31+
.drop-menu{
32+
display: none;
33+
position: absolute;
34+
width:150px;
35+
border:1px solid#ccc;
36+
box-shadow:1px1px3px0#ccc;
37+
}
38+
39+
.drop-menu.active{
40+
display: block;
41+
}
42+
43+
.dropdown{
44+
top:100%;
45+
margin-top:0.125rem;
46+
}
47+
48+
.dropup{
49+
bottom:100%;
50+
margin-bottom:0.125rem;
51+
}
52+
53+
54+
.dropleft{
55+
right:100%;
56+
top:0%;
57+
margin-right:0.125rem;
58+
}
59+
60+
.dropright{
61+
left:100%;
62+
top:0%;
63+
margin-left:0.125rem;
64+
}
65+
66+
.drop-menua{
67+
display: block;
68+
padding:0.5rem1rem;
69+
text-decoration: none;
70+
color:#343434;
71+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp