Unchecking a checkbox when clicking on an anchor (CSS only) - html

I'm currently trying to do a sort of drop-down menu for the navbar of an app, in CSS, only.
My navbar, thanks to a checkbox, when checked, expands and when unchecked, hides itself with an animation, what is translated to the user as when he clicks anywhere on the navbar, it hides or expands itself. All the anchors in my navbar are linking to ids.
My problem is that, when I click on any link, the checkbox doesn't uncheck, so the navbar doesn't execute the "draw back" animation.
Here's the navbar what it looks like in html (jade-lang) :
header
input(type='checkbox')#btn
label(for='btn')#menu
nav
ul
li: a(href='#presentation') Accueil
And, to give just an idea of what looks like the css :
header input {
display: none;
}
#menu {
position: fixed;
left: 100%;
height: 100%;
width: 100%;
background-color: #333333;
font-weight: 500;
}
header input:checked + #menu {
-webkit-animation: showMenu 0.5s forwards;
animation: showMenu 0.5s forwards;
}
#-webkit-keyframes showMenu { from { left: 100%; } to { left: 0%; } }
#keyframes showMenu { from { left: 100%; } to { left: 0%; } }
header input + #menu {
-webkit-animation: hideMenu 0.5s forwards;
animation: hideMenu 0.5s forwards;
}
#-webkit-keyframes hideMenu { from { left: 0%; } to { left: 100%; } }
#keyframes hideMenu { from { left: 0%; } to { left: 100%; } }
Simple as that. But I don't get how to have the checkbox unchecking when clicking on any of the links of the navbar.
Thanks :).
EDIT I reworded my question.

You can not check/uncheck a checkbox using CSS, you'd need to use some JavaScript. As I understand the point of this excercise is to create a menu toggle type of button in pure CSS.
You can use :focus + * instead of :checked + *, so you'd need a focusable element, like <a>, to toggle the menu:
HTML:
Toggle menu
<nav id="menu">
Link<br>
Link 2
</nav>
** CSS **
#menu {
position: fixed;
left: 100%;
top:0;
width: 100%;
transition: left 300ms 300ms; /* note the additional transition delay */
}
a:focus + #menu {
left:0;
transition-delay:0;
}
Demo: http://jsfiddle.net/nt87koye/2/
This way the menu slides in when the link has focus, and slides out once the focus is lost (user clicks anywhere outside the link).
You could also use the :target pseudoclass and place <a href="#menu"> anywhere on the page, so the feature doesn't rely on specific markup, which is required with the adjacent sibling solution: http://jsfiddle.net/nt87koye/4/

Related

Getting rid of the closing mobile menu animation on window resize

I'm building a small website without JS for school and I'm stuck on an animation problem.
I want to get rid of the closing animation on my mobile menu when resizing the window. Because currently, if I reduce the size of the window the menu will appear for a brief moment before going to the side (outside of the viewport).
My menu general style in the media query is the following :
.menu {
position: fixed;
z-index: 80;
width: 19rem;
transform: translateX(100%);
height: 100%;
top: 0;
right: 0;
padding-top: 4.4rem;
justify-content: revert;
text-align: right;
box-shadow: var(--b-shadow-l);
background-color: seagreen;
/* todo */
transition: 800ms;
}
When the menu is opened :
#mobile:checked ~ .menu {
transform: translateX(0%);
transition-property: transform;
transition-duration: 800ms;
}
Codepen to better see the situation : https://codepen.io/aayko/pen/OJEErBM
My only solution so far is to remove the closing animation ...
I'm looking for anything, even if it means changing the way I style my mobile menu.
Just remove the whole transform in your code above, instead give the right: -100% when normal and right: 0 when checked, the animation is the same without the flash disappear.
.menu {
position: fixed;
z-index: 80;
width: 19rem;
height: 100%;
top: 0;
right: -100%;
padding-top: 4.4rem;
justify-content: revert;
text-align: right;
box-shadow: var(--b-shadow-l);
background-color: seagreen;
/* todo */
transition: 800ms;
}
#mobile:checked ~ .menu {
right: 0;
}
I recently came across this exact same issue and ended up figuring out a pretty good solution to it. I documented it all at https://stevenwoodson.com/blog/solving-animation-layout-flickering-caused-by-css-transitions/ if you're still in need of a fix!
The gist is that the transition needs to be added separately in a different class so you can remove it when you're not actively opening or closing the menu.

Animations in my slideshow will play again at the end of a slide

I've created an autoplaying slideshow of images with a simple fade between each slide. The problem is that each slide seems to 'reload' right before transitioning to the next one. So if I add an animation to a slide e.g. sliding in from the left, image 1 will slide in, then as image 2 is fading in, image 1 will disappear and slide in from the left again. Here it is on JSfiddle, you can see the first image faintly sliding in again as the second appears. Any help is appreciated.
<div id="slideshow">
<div class=slidein-left>
<img src="https://www.port.ac.uk/-/media/images/news-events-and-blogs/news/2020/october/cat-eyes-closed-600x400.jpg">
</div>
<div>
<img src="https://cdn.shopify.com/s/files/1/0997/4496/files/Untitled_design_19_grande.jpg">
</div>
</div>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#slideshow {
position: relative;
width: 100%;
height: 80%;
margin: 0;
padding: 0;
}
#slideshow>div {
position: absolute;
overflow-x: hidden;
overflow-y: hidden;
width: 100%;
height: 100%;
}
.slidein-left {
position: relative;
left: -100%;
overflow-x: hidden;
overflow-y: hidden;
-webkit-animation: slideleft 1s forwards 0.5s;
animation: slideleft 1s forwards 0.5s;
}
#-webkit-keyframes slideleft {
100% {
left: 0;
}
}
#keyframes slideleft {
100% {
left: 0;
}
}
$("#slideshow > div:gt(0)").hide();
setInterval(function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
}, 3000);
In setInterval :
First you're hiding the first image
then showing the second image
then you append the first image to the slider
the problem is in step 3.
when you append the first image, its display will be block and according to your css when the first image is showing it definitely has "slideleft" animation and before the next cycle begins your first image is showing again.
You can solve this issue by hiding the first image when it placed after the second one:
#slideshow > div:not(.slidein-left) + .slidein-left {
display: none !important;
}
Add the code above right above your keyframes in css and you're good to go.
instead of
#slideshow > div:not(.slidein-left)
you can assign a class to your second div (inside #slideshow) and use .that_class instead.
===========================
Update
Better solution is delaying the step 3.
$("#slideshow > div:gt(0)").hide();
setInterval(function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
setTimeout(function() {$('#slideshow >
div:first').appendTo("#slideshow")}, 1000)
}, 3000);
and delete the css code that I mentioned before.
It's better to remove the css animation delay because it's ruining the fadeIn effect for image 1.
animation: slideleft 1s forwards;
====================
Update 2
The setTimeout part of the javascript code that I provided in the previous update changed when I uploaded that.
the $("") part of setTimeout function must be in one line and having a newline character (generated by Stackoverflow) in it will ruins the whole code.

Toggle Div Style Between Opened (Display: Block) and Closed (Display: None) With jQuery

I have two divs that appear like this:
The idea is that when you close the bottom div (click on the 'X'), it should disappear.
And when you close the top div, it should disappear, and also the bottom div should slide up and take its place.
I'm very new to jQuery, but this is my first attempt:
function initAnnouncements() {
$(document)
// Closes announcement modules
.on('click', 'annoucements-close', function () {
$('announcement-div').hide();
})
}
#keyframes slideInFromRight {
0% {
transform: translateX(100%);
}
.1%{
opacity: 1;
}
100% {
transform: translateX(0);
opacity: 1;
}
}
.announcements-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
width: 300px;
/* align-items: flex-end; */
}
.announcements-1 {
animation: slideInFromRight 0.4s ease;
opacity: 0;
animation-fill-mode: forwards;
}
.announcements-2 {
/* animation: 0.4s ease-out 0s 1 slideInFromRight; */
animation: slideInFromRight 0.4s ease;
opacity: 0;
animation-fill-mode: forwards;
animation-delay: .4s;
margin-top: 15px;
}
.annoucements-header {
background-color: #1481C3;
color: #ffffff;
font-family: "Proxima Nova Bold";
padding: 7px 10px;
}
.annoucements-close {
position: absolute;
right: 5px;
width: 24px;
height: 36px;
cursor: pointer;
opacity: .85;
}
.annoucements-close:hover {
opacity: 1;
}
.annoucements-close::before,
.annoucements-close::after {
content: '';
width: 24px;
height: 2px;
background: white;
position: absolute;
top: 7px;
left: 0;
}
.annoucements-close::before {
transform: rotate(-45deg);
}
.annoucements-close::after {
transform: rotate(45deg);
}
/*opened or closed*/
.announcement-div-opened {
display: none;
}
.announcement-div.opened .announcement-div-opened {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="announcements-container">
<div class="announcement-div announcements-1">
<div class="annoucements-header">
<span class="annoucement-type-quantity">2 School Announcements</span>
<i class="annoucements-close"></i>
</div>
</div>
<div class="announcement-div announcements-2">
<div class="annoucements-header">
<span class="annoucement-type-quantity">1 Admin Announcement</span>
<i class="annoucements-close"></i>
</div>
</div>
</div>
</body>
As you can see this isn't doing anything. I'm trying to toggle the class from 'open' (display:block) to 'closed' (display:none) when the annoucements-close <i> element is clicked on.
And ideally I would like for the second div to slide up when the top one is closed, but first I'd just like to get either one to disappear.
What's wrong with my code where that's not working as expected?
Link to JSFiddle
There are 2 issues with your code: the click() event is inside the function initAnnouncements that doesn't get called. You could move it outside of this function or call the function. Then you have issues with your selectors: It's
.on('click', '.annoucements-close', function () {
$('.announcement-div').hide();
})
instead of
.on('click', 'annoucements-close', function () {
$('announcement-div').hide();
})
for class selectors. Working Fiddle.
If you just want to hide the annoucement which was clicked upon, just change it to
.on('click', '.annoucements-close', function () {
$(this).closest('.announcement-div').hide();
})
I looked at your code and adjusted it a little to demonstrate:
Added your common class on the two announcements "announcement-div"
Attached the document click handler with the jQuery ready event
Used the delegated event selector to listen to clicks within the document that match that common selector
On click of one of the announcement-div's animate the height to 0 and then remove the element
Comments are included in the fiddle. Hope this is helpful!
// Fire this function when the document is ready
$(function() {
// Listen on the whole document for click events on the .announcement-div element
$(document).on('click', '.annoucements-close', function () {
// From the close button find the closest parent "announcement-div"
var annoucement = $(this).closest('.announcement-div');
// Function to run after animating the element (use .hide() to keep element but display:none)
function destroy() {
annoucement.remove();
}
// Animate the annoucement's height to 0 over 400ms and then call the destroy function
annoucement.animate({ height: "0px" }, 400, destroy);
});
});
Updated JS Fiddle

keyframe animation not working properly - CSS

I have an angular component that has a button and the button triggers a sidePanel and the side panels slidesOut over the component that has the button triggering the side panel to come out. I have 2 issues I can solve. The side panel has a width of 350px;
1) When the component with the button is loaded you can see the sidePanel doing the animation to hide
2) The slideIn works fine but the slideOut moves really fast
HTML
<div class="credit-card-container {{toggleSideBarFlag ? 'showSideBar': 'hideSideBar'}} {{ submittingPayment ? 'payment-success-container' : ''}}">
<app-bulk-pay-credit-card [ccTotalDue]="totalDue" [pickupAvailabilityList]='selectedEquipment' (submittingPayment)="isSubmittingPayment(true)"
(toggleSideBar)="onToggleSideBar(false)" (close)="onCloseBulkPay(false)"></app-bulk-pay-credit-card>
</div>
CSS
.bulk-pay-storage-container {
.showSideBar {
width: 100%;
display: block;
position: absolute;
z-index: 9999;
animation: slideIn 1s;
}
.hideSideBar {
width: 100%;
display: block;
position: absolute;
right: -350px;
z-index: 9999;
animation: slideOut 1s forwards;
}
}
#keyframes slideIn {
0% {
transform: translateX(350px);
}
100% {
transform: translate(0);
}
}
#keyframes slideOut {
100% {
transform: translateX(350px);
}
}
I would use transitions rather than animations in your example.
You can basically toggle between having showSideBar class and not having it.
Sidebar's start position is hidden outside of the viewport (left: -350px) and its visible state is left: 0.
Set left and transition properties on your sidebar and remove the animation property.
.bulk-pay-storage-container {
/ ... /
left: -350px;
transition: left 2s;
}
.bulk-pay-storage-container.showSideBar {
left: 0
}

Creating a Hover effect Menu in React

i need to make a slide-out menu , something like the one in these pictures:
EDIT: i tried using css but my side-bar wont show up, this is my css code:
.SideBar{
position: absolute;
left: -80px;
transition: 0.4s;
width: 80px;
font-size: 15px;
color: white;
transition: 0.3s
}
.SideBar:hover{
left: 0;
}
use OnHover, and then when will be on hover, change state. after change state you can use condititon.
something like this:
render() {
return (
<div>(this.state.isHovered) ? (<a>openedmenu</a>) :(<a onHover={this.hover.bind(this)}>closed menu</a>)</div>
)
}
hover() {
this.setState({isHovered:true})
}
and then you can experiment by your self