Skip to content

Commit

Permalink
mobile menu animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjorn Zschernack committed Nov 8, 2023
1 parent 6fe2bb1 commit 93c0d36
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 213 deletions.
222 changes: 12 additions & 210 deletions js/myjs.js
Original file line number Diff line number Diff line change
@@ -1,223 +1,25 @@
import {toggle} from './toggle.js';
const services = {

moveCircle: function(left) {
const circle = document.querySelector('.circle');
if(!circle) { return; }
circle.setAttribute('style','margin-left: ' + left + 'px');
},

handleCirclePosition: function() {

const page = document.querySelector('#page-wrap');
const share = document.querySelector('#share');

setTimeout(() => {document.querySelector('.circle').classList.remove('hidden')}, 300);
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*1.5) + 'px');
if(document.body.clientWidth < 1680) {
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*1.5) + 'px');
}
if(document.body.clientWidth < 1500) {
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*1.8) + 'px');
}
if(document.body.clientWidth < 1300) {
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*1.9) + 'px');
}
if(document.body.clientWidth < 1150) {
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*2) + 'px');
}
if(document.body.clientWidth < 1050) {
document.querySelector('.circle').setAttribute('style', 'margin-left: ' + (page.clientWidth - share.clientWidth*2.2) + 'px');
}
if(document.body.clientWidth < 992) {
if(document.querySelector('.sub-bg')) {
//document.querySelector('.sub-bg').setAttribute('style', 'padding-top: ' + (document.getElementById('top-header').clientHeight) + 'px !important');
}
document.querySelector('.circle').setAttribute('style', 'margin-right: ' + (share.clientWidth*.1) + 'px');
}
},

initializeSlickSlider: function() {
if( jQuery('.slider') ) {
jQuery('.slider').slick({
dots: true,
infinite: false,
speed: 500,
slidesToShow:4,
arrows: true,
responsive: [
{
breakpoint: 1280,
settings: {
arrows: false,
slidesToShow: 2
}
},
{
breakpoint: 768,
settings: {
arrows: false,
slidesToShow: 1,
dots: false,
autoplay: true,
}
}
]
});
}
},


hasLocalStorage: function(storageKey) {
return localStorage.getItem(storageKey) ? true : false;
},

/* fetch products as fas as they are not stored in localstorage */
fetchProducts: function() {

if(services.hasLocalStorage('wc-products')) {
return;
}
const shop_url = 'https://shop.reuss-gelenkwellen.de/wp-json/wc/v2/products/?per_page=100';

fetch(shop_url, {headers: {'Authorization': 'Basic ' + btoa('ck_b5ed3fcc3b91aef1762867b2b24a5806afea703b' + ":" + 'cs_b6e5569d016a07c18bf503eeceb4522b43a89ed4')}}).
then(response => response.json()).
then(data => {
//store data globally on pageload
searchable_content['products'] = data;
localStorage.setItem('wc-products', JSON.stringify(data));
});

},

fetchCustomAPI: function() {
if(services.hasLocalStorage('custom-content')) {
return;
}
const shop_url = '/wp-json/wp/v2/custom';

fetch(shop_url).
then(response => response.json()).tea
then(data => {
//store data globally on pageload
searchable_content['custom'] = data;
localStorage.setItem('custom-content', JSON.stringify(data));
});
},

setLocalStorageTimeStamps: function() {
if(!services.hasLocalStorage('custom-content-timestamp')){
localStorage.setItem('custom-content-timestamp', Date.now());
}
//300 Tage
if(Date.now() - localStorage.getItem('custom-content-timestamp') > 25920000000) {
localStorage.setItem('custom-content-timestamp', Date.now());
localStorage.removeItem('wc-products');
localStorage.removeItem('custom-content');
}

},

renderSingleContentAsGrid: function(single, headline) {
let list = '<b>'+headline+'</b><ul class="grid">';
let listcontent = '';
if(headline === 'Produkte') {
listcontent = single.map(result => {
return '<li class=""><img src="/wp-content/themes/bbt_rare/images/dummy.png" class="preview-img" data-image='+result.images[0].src+'><a target="_blank" href="'+result.permalink+'">'+result.name+'</a></li>';
});
} else {
listcontent = single.map(result => {
return '<li><a href="'+result.permalink+'">'+result.title.rendered +'</a></li>';
});
}
return list += listcontent.join('') + '</ul>';
},



listenForKeydown: function() {

toggle.handleToggle({contenttype:'search', event:'keyup', element: document.querySelector('.searchfield'), btn: document.querySelector('.btn-close'), target: document.querySelector('#resultcontainer')});

},

handleCloseBtnClick: function() {

const closebtn = document.querySelector('.btn-close');
closebtn.addEventListener('click', e => {

toggle.closeSearch()

});
},


handleMenuLeftMargin: function() {
const menubar = document.querySelector('.menuopener');
const mainmenu = document.querySelector('#mainmenu');
mainmenu.setAttribute('style', `margin-left: 54px`);
},

handleSlickSliderArrows: function() {
const arrow_left = document.querySelector('#arrows li.left');
const arrow_right = document.querySelector('#arrows li.right');
if(!arrow_left || !arrow_right) { return; }
arrow_left.addEventListener('click', function(e) {
jQuery('.slider').slick('slickPrev');
});
arrow_right.addEventListener('click', function(e) {
jQuery('.slider').slick('slickNext');
});
},

const toggleMenu = () => {
const menuToggle = document.querySelector('#nav-icon');
const navigation = document.querySelector('#mainmenu');
menuToggle.addEventListener('click', function(e) {
e.preventDefault();
menuToggle.classList.toggle('open');
navigation.classList.toggle('open');
});

}


document.addEventListener('DOMContentLoaded', function(e) {

toggle.handleToggle({contenttype:'menu', event:'click', key:'', element:document.querySelector('.menuopener'), target:document.querySelector('#mainmenu'), height: (document.querySelector('#mainmenu').querySelectorAll('li').length * 70)+50});

toggle.handleToggle({contenttype:'menu', event:'click', key:'', element:document.querySelector('#share-opener'), target:document.querySelector('#sharelist'), height: (document.querySelector('#mainmenu').querySelectorAll('li').length * 70)+50});



services.initializeSlickSlider();
services.fetchProducts();
services.fetchCustomAPI();
services.setLocalStorageTimeStamps();
services.listenForKeydown();
services.handleCloseBtnClick();
services.handleCirclePosition();
toggle.handleEscapeKey();
services.handleSlickSliderArrows();

// document.querySelector('.circle').setAttribute('style','height:'+document.querySelector('#circlecontainer').clientWidth*.85+'px;width:'+document.querySelector('#circlecontainer').clientWidth*.85+'px;margin-left:'+document.querySelector('#circlecontainer').clientWidth*.6+'px');
toggleMenu();
});

window.addEventListener('resize', function(e) {
// toggle.closeSearch();//toggle.closeAll()
services.handleCirclePosition();

});

window.addEventListener('scroll', function(e) {
if(window.scrollY > 300) {
document.querySelector('.circle').classList.add('d-none-important');
if(document.querySelector('.bg-blue-mobile') && document.body.clientWidth < 992){
document.querySelector('.bg-blue-mobile').classList.add('d-none-important');
}
if(document.querySelector('.bg-blue-mobile-pages') && document.body.clientWidth < 992){
document.querySelector('.bg-blue-mobile-pages').classList.add('d-none-important');
}
}else {
document.querySelector('.circle').classList.remove('d-none-important');

if(document.querySelector('.bg-blue-mobile') && document.body.clientWidth < 992){
document.querySelector('.bg-blue-mobile').classList.remove('d-none-important');
}
if(document.querySelector('.bg-blue-mobile-pages') && document.body.clientWidth < 992 ){
document.querySelector('.bg-blue-mobile-pages').classList.remove('d-none-important');
}
}


});

40 changes: 39 additions & 1 deletion scss/includes/_menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
@media (max-width: 1200px) {
flex-direction: column;
max-width: 100%;
height: 1px;
overflow: hidden;
transition: all .3s ease-in-out;

&.open {
height: 100vh;
overflow: auto;
}
}
li {
padding:0;
Expand Down Expand Up @@ -141,18 +149,48 @@
#nav-icon {
width: 40px;
height: 30px;

margin-top: -10px;
cursor: pointer;
span {
position: absolute;
text-align: end;
background-color: #fff;
width: 40px;
height: 3px;
margin-bottom: 8px;
display: block;
&:nth-child(1) {
margin-top:0
}
&:nth-child(2) {
width: 30px;
margin-left: 10px;
margin-top:24px;
}
&:nth-child(3) {
margin-top:48px;
}
}

&.open span:nth-child(1) {
margin-top: 18px;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}

&.open span:nth-child(2) {
opacity: 0;
left: -60px;
}

&.open span:nth-child(3) {
margin-top: 20px;
-webkit-transform: rotate(-135deg);
-moz-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
transform: rotate(-135deg);
}
}
}
35 changes: 35 additions & 0 deletions scss/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ body header .contact .clock:before {
#menucontainer ul {
flex-direction: column;
max-width: 100%;
height: 1px;
overflow: hidden;
transition: all 0.3s ease-in-out;
}
#menucontainer ul.open {
height: 100vh;
overflow: auto;
}
}
#menucontainer ul li {
Expand Down Expand Up @@ -181,18 +188,46 @@ body header .contact .clock:before {
#menuopener #nav-icon {
width: 40px;
height: 30px;
margin-top: -10px;
cursor: pointer;
}
#menuopener #nav-icon span {
position: absolute;
text-align: end;
background-color: #fff;
width: 40px;
height: 3px;
margin-bottom: 8px;
display: block;
}
#menuopener #nav-icon span:nth-child(1) {
margin-top: 0;
}
#menuopener #nav-icon span:nth-child(2) {
width: 30px;
margin-left: 10px;
margin-top: 24px;
}
#menuopener #nav-icon span:nth-child(3) {
margin-top: 48px;
}
#menuopener #nav-icon.open span:nth-child(1) {
margin-top: 18px;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}
#menuopener #nav-icon.open span:nth-child(2) {
opacity: 0;
left: -60px;
}
#menuopener #nav-icon.open span:nth-child(3) {
margin-top: 20px;
-webkit-transform: rotate(-135deg);
-moz-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
transform: rotate(-135deg);
}

.posts-grid {
Expand Down
4 changes: 2 additions & 2 deletions template-parts/header/menubars.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="d-flex w-100 justify-content-end align-items-center d-xl-none">
<div id="menuopener" class="d-flex flex-column justify-content-center align-items-center">
<div id="nav-icon" class="d-flex flex-column justify-content-center align-items-center">
<div id="menuopener" class="d-flex flex-column align-items-center">
<div id="nav-icon" class="d-flex flex-column justify-content-center">
<span></span>
<span></span>
<span></span>
Expand Down

0 comments on commit 93c0d36

Please sign in to comment.