html - Add browser max-width (media query) to javascript when hide header? -
could use help*
i want control on width hide header (javascript) starts working lets say: @ width 667px , downwards. mobile viewing, don't want use:
if( /android|webos|iphone|ipad|ipod|blackberry/i.test(navigator.useragent) ) {
because doesn't let me control larger screen sizes big tablets/phones.
i saw media query scripts javascript, can't work code see below:
// hide header on scroll down // var didscroll; var lastscrolltop = 0; var delta = 44; var navbarheight = $('header').outerheight(); $(window).scroll(function(event){ didscroll = true; }); setinterval(function() { if (didscroll) { hasscrolled(); didscroll = false; } }, 250); function hasscrolled() { var st = $(this).scrolltop(); // make sure scroll more delta if(math.abs(lastscrolltop - st) <= delta) return; // if scrolled down , past navbar, add class .nav-up. // necessary never see "behind" navbar. if (st > lastscrolltop && st > navbarheight){ // scroll down $('header').addclass('nav-up'); } else { // scroll if(st + $(window).height() < $(document).height()) { $('header').removeclass('nav-up'); } } lastscrolltop = st; }
if sole desire hide header (your question not clear that) when viewport smaller n pixels not need js that. use simple css media queries:
@media screen , (max-width: 667px) { .your-header-class { display: none; } }
but if want go mobile first should do:
/* general + mobile css */ .your-header-class { display: none; } /* tablet + desktop css */ @media screen , (min-width: 667px) { .your-header-class { display: block; } }
otherwise check out this answer gave couple of days ago.
i repost code couple of comments might of use you:
$(window).resize(function() { // make sure 'media query' run, // once viewport has stopped changing in size cleartimeout(resizetimer); resizetimer = settimeout(breakpointchange(), 400); }); function breakpointchange() { // window width width = window.innerwidth; // adapt values (i.e. 667) here if (!mobile && width < 667) { tablet = desktop = false; mobile = true; // mobile, execute js here } if (!tablet && width > 401 && width < 768) { mobile = desktop = false; tablet = true; console.log('is tablet'); } if (!desktop && width > 769) { mobile = tablet = false; desktop = true; console.log('is desktop'); } } // you'll need call $(window).resize(); first time script loads $(window).resize();
this allows control media breakpoints - define functions next these two, , call them inside appropriate conditional.
Comments
Post a Comment