Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made several changes to smooth out animation #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 39 additions & 46 deletions css/dayNight.css
Original file line number Diff line number Diff line change
@@ -1,61 +1,54 @@
.dayNight {
/* shapes the day/night switch */
#dayNight {
/* allows js to change pseudo elements */
--day-opacity: 1;
--night-opacity: 0;

cursor: pointer;
position: absolute;
top: 20px;
right: 30px;
width: 36px;
height: 36px;
border-radius: 50%;
background-color: var(--moon-color);
transition: background-color 0.3s linear,
transform .3s linear;
}

.dayNight input {
display: none;
#dayNight.day {
background-color: var(--sun-color);
transform: scale(.5) rotate(45deg);
}

.dayNight input + div {
border-radius: 50%;
/* makes the moon shape */
#dayNight::after {
content: '';
width: 36px;
height: 36px;
position: relative;
box-shadow: inset 16px -16px 0 0 var(--moon-color);
transform: scale(1) rotate(-2deg);
transition: box-shadow 0.5s ease 0s, transform 0.4s ease 0.1s;
}

.dayNight input + div:before {
content: "";
width: inherit;
height: inherit;
border-radius: inherit;
position: absolute;
left: 0;
top: 0;
transition: background 0.3s ease;
border-radius: 50%;
background-color: #374151;
position: absolute;;
left: 15px;
bottom: 15px;
opacity: var(--night-opacity);
}

.dayNight input + div:after {
content: "";
width: 8px;
height: 8px;
/* makes the sun shape */
#dayNight::before {
content: '';
width: 10px;
height: 10px;
border-radius: 50%;
margin: -4px 0 0 -4px;
position: absolute;
top: 50%;
left: 50%;
box-shadow: 0 -23px 0 var(--sun-color), 0 23px 0 var(--sun-color), 23px 0 0 var(--sun-color), -23px 0 0 var(--sun-color), 15px 15px 0 var(--sun-color), -15px 15px 0 var(--sun-color), 15px -15px 0 var(--sun-color), -15px -15px 0 var(--sun-color);
transform: scale(0);
transition: all 0.3s ease;
}

.dayNight input:checked + div {
box-shadow: inset 32px -32px 0 0 var(--moon-color);
transform: scale(0.5) rotate(0deg);
transition: transform 0.3s ease 0.1s, box-shadow 0.2s ease 0s;
}

.dayNight input:checked + div:before {
background: var(--sun-color);
transition: background 0.3s ease 0.1s;
}

.dayNight input:checked + div:after {
transform: scale(1.5);
transition: transform 0.5s ease 0.15s;
position: absolute;
box-shadow: 0 -36px 0 #EDA97F,
0 36px 0 #EDA97F,
36px 0 0 #EDA97F,
-36px 0 0 #EDA97F,
23px 23px 0 #EDA97F,
-23px 23px 0 #EDA97F,
23px -23px 0 #EDA97F,
-23px -23px 0 #EDA97F;
opacity: var(--day-opacity);
}
5 changes: 1 addition & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
<title>Andy Young - Software Engineer</title>
</head>
<body class="light-mode">
<label class="dayNight hidden">
<input type="checkbox" checked>
<div></div>
</label>
<div id="dayNight" class="day"></div>
<div class="container">
<header class="hero">
<div class="hero-content">
Expand Down
31 changes: 15 additions & 16 deletions js/dayNight.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const body = document.querySelector('body');
const wrapper = document.querySelector('.dayNight');
const toggle = wrapper.querySelector('input');
const body = document.body;
const dayNight = document.getElementById('dayNight');

if (localStorage.getItem('mode') == null) {
localStorage.setItem('mode', 'light-mode');
}

body.classList.value = localStorage.getItem('mode');
toggle.checked = localStorage.getItem('mode') === 'light-mode' ? true : false;
wrapper.classList.remove('hidden');

toggle.addEventListener('change', () => {
const mode = toggle.checked ? 'light-mode' : 'dark-mode';
body.classList.value = mode;
localStorage.setItem('mode', mode);
});
dayNight.addEventListener('click', function() {
dayNight.classList.toggle('day');
if (dayNight.classList.value === 'day') {
dayNight.style.setProperty('--night-opacity', '0');
dayNight.style.setProperty('--day-opacity', '1');
body.classList.value = 'light-mode';
} else {
dayNight.style.setProperty('--night-opacity', '1');
dayNight.style.setProperty('--day-opacity', '0');
body.classList.value = 'dark-mode';
}
})