Reactjs Showing Sidebar Menu with animation - css

I need some insight pls.
I tried to build a sidebar menu with an animation that will show from left -100% to 100% fullscreen when state is true and close from 100% fullscreen to left -100% when the state is false.
The animation works when the state is true but the animation doesn't work when closing the sidebar menu.
Btw if there's a better approach for this pattern please let me know. Thank you in advance !
const PhoneNavigation = ({ handleClose, open }) => {
return (
<nav className={open ? "show_side_bar" : "close_side_bar"}>
<span onClick={handleClose}>X</span>
<ul>
<li className="subMenu navItem">
Courses
</li>
<li className="navItem">
Pricing
</li>
<li className="navItem">
Mission
</li>
<li className="subMenu navItem">
About us
</li>
</ul>
</nav>
);
};
export default PhoneNavigation;
// STYLING SHEET
.show_side_bar {
position: fixed;
background-color: brown;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99;
animation: showMenu 2s;
}
.close_side_bar {
animation: showMenu 2s reverse;
}
#keyframes showMenu {
from {
left: -100%;
}
to {
left: 0%;
}
}

This will not work on closing the sidebar because animations are applied only on the entry of an element. Instead, you can use transition like this :
const PhoneNavigation = ({ handleClose, open }) => {
return (
// when open is true, there will be an additional class show_side_bar
<nav className={open ? "show_side_bar side_bar" : "side_bar"}>
<span onClick={handleClose}>X</span>
<ul>
<li className="subMenu navItem">
Courses
</li>
<li className="navItem">
Pricing
</li>
<li className="navItem">
Mission
</li>
<li className="subMenu navItem">
About us
</li>
</ul>
</nav>
);
};
export default PhoneNavigation;
And the following CSS will make it work :
.side_bar {
position: fixed;
background-color: brown;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99;
// transition applies to every render, i.e., adding and removing any class from
// the element
transition : all 2s ease;
// place it initially at -100%
transform : translate(-100%);
}
.show_side_bar {
// when this class is active, sidebar will be visible
transform : translate(0);
}

Related

show drop down menu outside its sidebar parent,and on scrolling make the menu also scroll according to its parent,

Here is the code below,
I need the the dropMenu div after scrolling the nav to change its position (as much as nav scrolling). to stay below the (i) element
<nav>
<ul>
<li>
<a onclick={this.setState(prevState => {showDropMenu:!prevState.showDropMeny})}>
<i class="fas fa-ellipsis-h" />
</a>
</li>
{showDropMenu &&<div className="dropMenu"></div> }
</ul>
</nav>
nav {
overflow-y: visible;
overflow-x: hidden;
max-height: 70vh;
}
ul {
list-style-type: none;
min-width: 15vw;
padding-left: 0;
}
is there any big or small change can I make to achieve that ?
You can take a look on this example https://www.w3schools.com/howto/howto_js_sticky_header.asp
Below you can see quick example which I have made with your code
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
showDropMenu: true
};
}
render() {
return (
<div className="App">
<nav>
<ul>
<li>
<a
onClick={() =>
this.setState({ showDropMenu: !this.state.showDropMenu })
}
>
<i className="fas fa-ellipsis-h" /> Click Me
</a>
</li>
{this.state.showDropMenu && (
<div className="dropMenu">Drop Menu</div>
)}
</ul>
</nav>
<div className="content">
Some Text Some Text Some Text Some Text Some Text
</div>
</div>
);
}
}
nav {
background-color: blue;
position: fixed;
top: 0;
width: 100%;
}
ul {
list-style-type: none;
min-width: 15vw;
padding-left: 0;
}
.content {
margin-top: 100px;
}

Make CSS height transition smoother

I have the following HTML markup and CSS to build navigation menus:
.submenu {
max-height: 0;
overflow-y: hidden;
transition: max-height 0.3s ease-in;
}
.submenu.opened{
max-height: 200px;
}
<ul class="navigation navigation-main navigation-accordion">
<li>Dashboard</li>
<li>
Products
<ul class="submenu">
<li>All Products</li>
<li>Popular Products</li>
</ul>
</li>
</ul>
I toggle the class opened on submenu using JavaScript. The submenu slides up and down on click with the help of the transition. But I want the transitions as smooth as possible.
How can I improve the above CSS code to make it as smooth as possible, as it doesn't work well if the Product link is clicked in quick succession.
I guess everybody has another definition of smooth, but I would start here with changing the ease function from ease-in to ease-in-out and than just play with the duration of the transition. 0.3s is a bit fast in this situation, increase the duration until it fits for you. Here is an example with a duration of 1.5s:
document.querySelector('#prod').addEventListener('click', function() {
document.querySelector('.submenu').classList.toggle('opened');
}, false);
.submenu {
max-height: 0;
overflow-y: hidden;
transition: max-height 1.5s ease-in-out;
}
.submenu.opened{
max-height: 200px;
}
body {
font-size: 1.5em;
font-family: sans-serif;
line-height: 1.7
}
<ul class="navigation navigation-main navigation-accordion">
<li>Dashboard</li>
<li>
Products
<ul class="submenu">
<li>All Products</li>
<li>Popular Products</li>
</ul>
</li>
</ul>

How to adapt bootstrap navbar drop-down to be inline

I've been asked to have inline sub-menu items for a site I'm making using Bootstrap.
The default with Bootstrap for drop-down is a nice 'downdown-toggle' class where items are stacked and only width of items with some padding.
How can I adapt that class for the sub-menu background to be 100% screen width, then sub-menu items in container and items (li's) to be inline not stacked? See mock-up image of what I want to achieve.
Note that unlike the current sub-menu, this would need to push all content below down rather than hover over it.
EDIT after mk14 gave really good code below. This is what I get (see image below). I just need items to be in a container (i've shown in yellow) and to push the content of the page down and back up as menu shows and hides. To reiterate, I want the grey background to be 100% width of screen as I said, but items in the container.
Okay, I'm expanding on the first answer based on the comments you made under it and adding more specificity and the media query.
http://jsbin.com/rusup/1/edit
DEMO: http://jsbin.com/rusup/1/
CSS (add your breakpoint when the larger viewport menu kicks in )
#media (min-width:768px) {
body { padding-top: 60px } /* height of the menu collapsed */
.nav-bg {
height: 0px;
width: 100%;
position: absolute;
top: 50px;
background: #e7e7e7;
transition: all .5s ease-in-out;
}
.menu-open .nav-bg { height: 50px } /* change to your height of the child menu */
.menu-open #page {
padding-top:60px;
transition: all .2s ease-in-out;
}
.navbar-nav.nav > li { position: static }
.navbar-nav.nav .dropdown-menu {
left: 0 !important;
right: 0 !important;
box-shadow: none;
border: none;
margin: 0 auto;
max-width: 1170px;
background: transparent;
padding: 0;
}
.navbar-nav.nav .dropdown-menu > li { float: left }
.navbar-nav.nav .dropdown-menu > li > a {
width: auto !important;
background: transparent;
line-height: 49px;
padding-top: 0;
padding-bottom: 0;
margin: 0;
}
}
jQuery:
$('.navbar').append('<span class="nav-bg"></span>');
$('.dropdown-toggle').click(function() {
if (!$(this).parent().hasClass('open')) {
$('html').addClass('menu-open');
} else {
$('html').removeClass('menu-open');
}
});
$(document).on('click touchstart', function (a) {
if ($(a.target).parents().index($('.navbar-nav')) == -1) {
$('html').removeClass('menu-open');
}
});
HTML
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li>Link</li>
</ul>
<li class="dropdown">
Dropdown 2 <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Dropdown 2 Child 1</li>
<li>Dropdown 2 Child 2</li>
<li>Dropdown 2 Child 3</li>
<li>Dropdown 2 Child 4</li>
</ul>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container" id="page">
Me finks this is a page.
</div>
This post covers this topic as a general solution.
Here is an adaptation for bootstrap:
.nav > li{
position: static !important;
}
.dropdown-menu {
left: 0 !important;
right: 0 !important;
}
.dropdown-menu > li{
float: left !important;
}
.dropdown-menu > li > a{
width:auto !important;
}

Fixed menu at top but not the first element

How can i use position: fixed; for my menu, when it isn't the first element on the website.
My logo is the top item on the website, thereafter the menu. When i scroll down the page i want the menu to be the top element. How can i do this? And is it possible with only css?
This would require the use of javaScript, but can be done quite simply.
Example
JsFiddle
HTML
<header>
<p class="site-title">
Your logo here
</p>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</header>
CSS
ul{
background-color: red;
margin: 0;
}
ul li{
display: inline;
}
.fixed{
position: fixed;
top: 0;
left: 0;
width: 100%;
}
jQuery
$(function ()
{
// Get the initial position of the menu.
var menuTop = $('nav').offset().top;
$(window).scroll(function ()
{
// Check position
if ($(window).scrollTop() > menuTop)
{
$('nav').addClass('fixed');
}
else
{
$('nav').removeClass('fixed');
}
});
});

css sprite : div h2 title image with ul li

I am using SpriteMe for my webpage and have code below. The problem is it show full sprite image instead of single image inside it.
<div class="white_box">
<div class="white_box_top_JS" style="background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/75a3bcd651/spriteme2.png); border: 2px solid red; background-position: -279.5px -10px; ">
<h2>Job Seekers</h2>
<ul>
<li>
Register
</li>
</ul>
</div>
</div>
I have read and followed <ul> Sprite Issue but can't get it.
http://jsfiddle.net/6TaQt/30/
It is best to stay away from inline style whenever possible. Additionally, the li element should receive the sprite, not the anchor. Here is a start on the relevant changes:
(also in a forked fiddle )
HTML:
<ul>
<li id="register">
Register
</li>
<li id="post">
Post resumes
</li>
<li id="find">
<a href="/find-jobs/" class="seo" >Find jobs</a>
</li>
<li id="get">
Get jobs by email
</li>
</ul>
CSS:
#register { width: 40px;
background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/75a3bcd651/spriteme2.png);
background-position: -1315px -6px; }
#post { width: 40px;
background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/75a3bcd651/spriteme2.png) ;
background-position: -1360px -6px; }
#find { width: 40px;
background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/75a3bcd651/spriteme2.png);
background-position: -685px -5px; }
#get { width: 40px;
background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/75a3bcd651/spriteme2.png);
background-position: -544px -6px; }

Resources