For smaller viewports I built an icon which opens on click and shows the navigation. On larger viewports the icon should not be visible. Unfortunately the button only appears/disappears on resizing but not initially when the page is loaded.
jQuery(document).ready(function($) {
var responsive_viewport = $(window).width();
$(window).resize(function(){
if (responsive_viewport < 768) {
$('.top-nav').css('display', 'none'),
$('.menutop').css('display', 'block')
$(function(){
$('.menutop').toggle(function(){
$('.top-nav').show('slow');
}, function(){
$('.top-nav').hide('slow');
});
});
}
if (responsive_viewport >= 768) {
$('.menutop').css('display', 'none')
$('.top-nav').css('display', 'block')
}
});
});
If I do not include the window resize function the .menutop button appears only on smaller viewports. Thanks for looking at it. It seems so simple but why is this button showing up on viewports >768?
As I do not know how to delete my question (and maybe someone stumbles over the same difficulties), I found the solution.
Resize function and responsive window should be seperated: http://jsfiddle.net/MiauMiau/wtzKW/ It still seems a bit laborious?
var windowSize = $(window).width();
if (windowSize < 768) {
$('.menu-top').css('display', 'block');
$('.top-nav').css('display', 'none');
$('.menu-top').click(function () {
$('.top-nav').toggle('slow');
});
} else {
$('.menu-top').css('display', 'none'),
$('.top-nav').css('display', 'block');
}
$(window).on('resize', function (event) {
var windowSize = $(window).width();
if (windowSize < 768) {
$('.menu-top').css('display', 'block');
$('.top-nav').css('display', 'none');
} else {
$('.menu-top').css('display', 'none'),
$('.top-nav').css('display', 'block');
}
});