In order to make our website responsive to mobile devices, I am utilizing CSS media queries to switch between a header bar and a hamburger menu.
I had the idea of adding a subtle animation when desktop users resize their browser window beyond the defined media query bounds. As a trial run, I have been testing the transition between our large logo and the smaller version.
Within my animations.scss file, you will find these two animations:
@mixin ToSmallLogo() {
background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 1s;
animation-name: SwapToSmallLogo;
@keyframes SwapToSmallLogo {
0% {
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
}
100% {
background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;
}
}
}
@mixin SwapToBigLogo() {
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 1s;
animation-name: SwapToBigLogo;
@keyframes SwapToBigLogo {
0% {
background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
}
100% {
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
}
}
}
Furthermore, I have a specific SCSS file for the media queries, containing:
@import '../../Variables/Sizes.scss';
@import '../Animations/animations.scss';
@media screen and (max-width: $bigToSmallFISLogo) {
#franklin {
@include ToSmallLogo();
}
}
@media screen and (min-width: $bigToSmallFISLogo){
#franklin{
@include SwapToBigLogo();
}
}
Before applying the media queries, the CSS code is as follows:
#franklin {
display: block;
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
background-repeat: no-repeat;
background-size: 100%;
height: 72px;
}
The issue I'm facing is that while shrinking the screen works smoothly, going back up snaps to the larger image without any animation. I initially thought the problem lay in having the image defined in the CSS before the media queries were applied, so I removed it; however, this resulted in no image being displayed.
During the process of writing this question, I came up with an idea to use a media query to control the direction of the animation:
@mixin SwapLogo() {
/* background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;*/
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 1s;
animation-name: SwapLogo;
@media screen and (max-width: $bigToSmallFISLogo) {
animation-direction: normal;
background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;
}
@media screen and (min-width: $bigToSmallFISLogo) {
animation-direction: reverse;
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
}
@keyframes SwapLogo {
0% {
width: 100%;
background-image: url('../../../PageAssets/Fis.png');
}
100% {
background-image: url('../../../../PageAssets/fissmall.png');
width: 7%;
}
}
}
While this method resulted in the animation running on page load, it did not function during resizing. Is there a way to achieve what I'm aiming for?