I've created a script for displaying a dropdown menu on small screens with a click (or tap on mobile), but I want it to change to hover when the screen size is larger. Here's my code:
$(document).ready(function() {
var open = false;
if ($(window).width() > 767) {
$(".dropdown").hover(
function(){
$('ul.nav li.dropdown:hover > ul.dropdown-menu').css('display','block');
console.log('display block de nav HOVER');
});
}
else {
console.log('smaller');
$(".dropdown").click(
function(){
if (open){
$('ul.nav li.dropdown:hover > ul.dropdown-menu').css('display','none');
console.log('display none de nav');
} else {
$('ul.nav li.dropdown:hover > ul.dropdown-menu').css('display','block');
console.log('display block de nav');
}
open = !open;
});
}
});
The HTML code:
<nav id="navbar" class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#typo-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@if(Sentinel::guest())
<a class="navbar-brand" href="{{ route('auth.login') }}">Typografics Academy</a>
@elseif(Sentinel::inRole('user'))
<a class="navbar-brand" href="{{ route('user.dashboard') }}">Typografics Academy</a>
@elseif(Sentinel::inRole('admin') || Sentinel::inRole('super'))
<a class="navbar-brand" href="{{ route('admin.dashboard') }}">Typografics Academy</a>
@endif
</div>
<div class="collapse navbar-collapse" id="typo-navbar-collapse-1">
<ul class="nav navbar-nav">
@if(!Sentinel::guest())
@if(Sentinel::inRole('user'))
<li {{ Request::is('/results*') ? 'class=active' : ''}}><a href="{{ route('user.results') }}">Results</a></li>
@elseif(Sentinel::inRole('admin') || Sentinel::inRole('super'))
<li {{ Request::is('admin/users*') ? 'class=active' : ''}}><a href="{{ route('users.overview') }}">Users</a></li>
<li {{ Request::is('admin/tests*') ? 'class=active' : ''}}><a href="{{ route('tests.overview') }}" >Tests</a></li>
<li {{ Request::is('admin/profiles*') ? 'class=active' : ''}}><a href="{{ route('profiles.overview') }}">Profiles</a></li>
<li {{ Request::is('admin/results*') ? 'class=active' : ''}}><a href="{{ route('results.overview') }}">Results</a></li>
@if(Sentinel::inRole('super'))
<li {{ Request::is('admin/questions*') ? 'class=active' : ''}}><a href="{{ route('questions.overview') }}">Questions</a></li>
<li {{ Request::is('admin/categories*') ? 'class=active' : ''}}><a href="{{ route('categories.overview') }}">{{trans('master.categories')}}</a></li>
<li {{ Request::is('admin/companies*') ? 'class=active' : ''}}><a href="{{ route('companies.overview') }}">Companies</a></li>
@endif
<li class="dropdown">
<a href="#" role="button" aria-haspopup="true" aria-expanded="false"> <span class="glyphicon glyphicon-plus"></span> Add New <span class="caret"></span></a>
<ul class="dropdown-menu">
<li {{ Request::is('admin/users/new') ? 'class=active' : ''}}><a href="{{ route('users.new') }}">{{trans('master.user')}}</a></li>
<li {{ Request::is('admin/tests/new') ? 'class=active' : ''}}><a href="{{ route('tests.new.1') }}">{{trans('master.test')}}</a></li>
<li {{ Request::is('admin/profiles/new') ? 'class=active' : ''}}><a href="{{ route('profiles.new') }}">{{trans('master.profile')}}</a></li>
@if(Sentinel::inRole('super'))
<li {{ Request::is('admin/questions/new') ? 'class=active' : ''}}><a href="{{ route('questions.new') }}">{{trans('master.question')}}</a></li>
<li {{ Request::is('admin/categories/new') ? 'class=active' : ''}}><a href="{{ route('categories.new') }}">{{trans('master.category')}}</a></li>
<li {{ Request::is('admin/subcategories/new') ? 'class=active' : ''}}><a href="{{ route('subcategories.new.without') }}">{{trans('master.subcategory')}}</a></li>
<li {{ Request::is('admin/companies/new') ? 'class=active' : ''}}><a href="{{ route('companies.new') }}">{{trans('master.company')}}</a></li>
@endif
</ul>
</li>
@endif
@endif
</ul>
@if(!Sentinel::guest())
<ul class="nav navbar-nav navbar-right">
<li><a class="userprofile" href="#">{{ucwords(Sentinel::check()->first_name)}}</a></li>
<li><a class="logout" href="{{ route('auth.logout') }}">Logout</a></li>
</ul>
@else
<ul class="nav navbar-nav navbar-right">
<li {{ Request::is('auth/login') ? 'class=active' : ''}}><a href="{{ route('auth.login') }}">Login</a></li>
</ul>
@endif
</div>
</div>
</nav>