Reputation: 68
I am trying to make a CSS and JavaScript side-nav. Here is the code I have so far:
var nav = document.getElementById('nav');
function show(){
nav.style.display = "block";
}
.side-nav{
background-color:black;
height:100%;
width:250px;
position: absolute;
display:none;
}
#myLink{
color:gray;
text-decoration: none;
display:block;
margin-left:15px;
margin-bottom:10px;
font-size:25px;
transition: .5s;
font-family: 'Marck Script', cursive;
margin-top:10px;
}
#myLink:hover{
color:white;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css2?family=Marck+Script&display=swap" rel="stylesheet">
</head>
<body>
<div class = "side-nav" id = "nav">
<div id = "myLinks">
<a href = "#" id = "myLink">Home</a>
<a href = "#" id = "myLink">Contact</a>
<a href = "#" id = "myLink">Blog</a>
<a href = "#" id = "myLink">Products</a>
</div>
</div>
<a href = "#" onclick = "show();">Show nav</a>
<script src="script.js"></script>
</body>
</html>
How do I achieve a smooth slide from left to right with the side nav? Do I still need to use JavaScript? Or is there some way to do that with just CSS? Thanks in advance!!!
Upvotes: 1
Views: 1412
Reputation: 784
If you want to use jQuery you would need something like this. I've also added the close button.
$( '.show-nav' ).click( function() {
$( '#nav' ).animate( {width: 'toggle'} );
} );
.side-nav{
background-color:black;
height:100%;
width:250px;
position: absolute;
display: none;
}
#myLink{
color:gray;
text-decoration: none;
display:block;
margin-left:15px;
margin-bottom:10px;
font-size:25px;
transition: .5s;
font-family: 'Marck Script', cursive;
margin-top:10px;
}
#myLink:hover{
color:white;
}
#nav .show-nav {
position: absolute;
top: 0;
right: 0;
padding: 10px;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css2?family=Marck+Script&display=swap" rel="stylesheet">
</head>
<body>
<div class = "side-nav" id = "nav">
<div id = "myLinks">
<a href = "#" id = "myLink">Home</a>
<a href = "#" id = "myLink">Contact</a>
<a href = "#" id = "myLink">Blog</a>
<a href = "#" id = "myLink">Products</a>
<a class="show-nav" href = "#">Close</a>
</div>
</div>
<a class="show-nav" href = "#">Show nav</a>
<script src="script.js"></script>
</body>
</html>
Upvotes: 0
Reputation: 965
Do you want like this ?
var nav = document.getElementById('nav');
function show(){
nav.classList.toggle("active");
}
.side-nav{
background-color:black;
height:100%;
width:250px;
position: absolute;
display:none;
}
#myLink{
color:gray;
text-decoration: none;
display:block;
margin-left:15px;
margin-bottom:10px;
font-size:25px;
transition: .5s;
font-family: 'Marck Script', cursive;
margin-top:10px;
}
#myLink:hover{
color:white;
}
.side-nav.active{
display:block;
animation:animate 1s linear forwards;
}
@keyframes animate{
from{
opacity:0;
width:0;
}
to{
width:250px;
opacity:1;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css2?family=Marck+Script&display=swap" rel="stylesheet">
</head>
<body>
<div class = "side-nav" id = "nav">
<div id = "myLinks">
<a href = "#" id = "myLink">Home</a>
<a href = "#" id = "myLink">Contact</a>
<a href = "#" id = "myLink">Blog</a>
<a href = "#" id = "myLink">Products</a>
</div>
</div>
<a href = "#" onclick = "show();">Show nav</a>
<script src="script.js"></script>
</body>
</html>
Upvotes: 1