I am trying to make a floating social share button that closed by default and will not open until the trigger is clicked. I got some codes from the internet, but the problem is the social icons is open by default and cover the page, how can I make it close by default? This is the codes:
const trigger = document.querySelector("menu > .trigger");
trigger.addEventListener('click', (e) => {
e.currentTarget.parentElement.classList.toggle("open");
});
menu {
--size: 2.1rem;
--radius: 6rem;
--padding: .5rem;
--bg-color: rgba(255, 255, 255, 0.9);
--fg-color: rgba(0, 0, 0, 0.7);
--hi-color: #12192c;
font-size: 29px;
position: fixed;
bottom: var(--padding);
right: var(--padding);
}
menu > * {
position: absolute;
display: grid;
place-content: center;
border-radius: 50%;
font-size: 29px;
background: var(--bg-color);
color: var(--fg-color);
text-decoration: none;
box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.6);
}
menu > .action {
--factor: 0;
width: 2.5rem;
height: 2.5rem;
right: calc(1.35 * var(--size));
bottom: calc(1.35 * var(--size));
opacity: 0;
transform: rotate(calc(-1 * var(--angle))) translateY(calc(-1 * var(--radius) * var(--factor))) rotate(var(--angle));
transition: transform 250ms ease-in-out, opacity 250ms ease-in-out, box-shadow 250ms ease-in-out, color 250ms ease-in-out;
}
menu > .action:hover, menu > .trigger:hover {
color: var(--hi-color);
box-shadow: 0px 0px 0px 0.35rem rgba(0, 0, 0, 0.2);
}
menu.open > .action {
--factor: 1;
font-size: 20px;
opacity: 1;
}
menu > .action:nth-child(1) {
--angle: 0deg;
transition-delay: 0ms;
}
menu > .action:nth-child(2) {
--angle: 30deg;
transition-delay: 50ms;
}
menu > .action:nth-child(3) {
--angle: 60deg;
transition-delay: 100ms;
}
menu > .action:nth-child(4) {
--angle: 90deg;
transition-delay: 150ms;
}
menu > .trigger {
width: calc(1.3 * var(--size));
height: calc(1.3 * var(--size));
bottom: 0;
right: 0;
font-size: 1.5rem;
transition: box-shadow 250ms ease-in-out, color 250ms ease-in-out;
}
menu > .trigger > i {
transition: transform 250ms ease-in-out;
}
menu.open > .trigger > i {
transform: rotate(-360deg);
}
<head>
<meta charset="UTF-8">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css'><link rel="stylesheet" href="style.css">
</head>
<body>
<menu >
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
</menu>
<script src="script.js"></script>
</body>
I got the codes here: https://www.learningrobo.com/2021/11/floating-social-media-buttons-using.html?sc=1659803757099#c6812736580691024803
Thank you.
CodePudding user response:
change from <menu >
to <menu >
in html.
CodePudding user response:
You don't need javascript for this, you can use a hidden checkbox to open/close menu:
.hidden
{
display: none;
}
menu {
--size: 2.1rem;
--radius: 6rem;
--padding: .5rem;
--bg-color: rgba(255, 255, 255, 0.9);
--fg-color: rgba(0, 0, 0, 0.7);
--hi-color: #12192c;
font-size: 29px;
position: fixed;
bottom: var(--padding);
right: var(--padding);
}
menu > * {
position: absolute;
display: grid;
place-content: center;
border-radius: 50%;
font-size: 29px;
background: var(--bg-color);
color: var(--fg-color);
text-decoration: none;
box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.6);
}
menu > .action {
--factor: 0;
width: 2.5rem;
height: 2.5rem;
right: calc(1.35 * var(--size));
bottom: calc(1.35 * var(--size));
opacity: 0;
transform: rotate(calc(-1 * var(--angle))) translateY(calc(-1 * var(--radius) * var(--factor))) rotate(var(--angle));
transition: transform 250ms ease-in-out, opacity 250ms ease-in-out, box-shadow 250ms ease-in-out, color 250ms ease-in-out;
}
menu > .action:hover, menu > .trigger:hover {
color: var(--hi-color);
box-shadow: 0px 0px 0px 0.35rem rgba(0, 0, 0, 0.2);
}
menu > input[type="checkbox"]:checked ~ .action { /* changed */
--factor: 1;
font-size: 20px;
opacity: 1;
}
menu > .action:nth-child(2) { /* changed */
--angle: 0deg;
transition-delay: 0ms;
}
menu > .action:nth-child(3) { /* changed */
--angle: 30deg;
transition-delay: 50ms;
}
menu > .action:nth-child(4) { /* changed */
--angle: 60deg;
transition-delay: 100ms;
}
menu > .action:nth-child(5) { /* changed */
--angle: 90deg;
transition-delay: 150ms;
}
menu > .trigger {
width: calc(1.3 * var(--size));
height: calc(1.3 * var(--size));
bottom: 0;
right: 0;
font-size: 1.5rem;
transition: box-shadow 250ms ease-in-out, color 250ms ease-in-out;
cursor: pointer;
}
menu > .trigger > i {
transition: transform 250ms ease-in-out;
}
menu > input[type="checkbox"]:checked ~ .trigger > i { /* changed */
transform: rotate(-360deg);
}
<head>
<meta charset="UTF-8">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css'><link rel="stylesheet" href="style.css">
</head>
<body>
<menu>
<input id="trigger" type="checkbox">
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<a href="#" ><i ></i></a>
<label for="trigger" ><i ></i></label>
</menu>
<script src="script.js"></script>
</body>