<!-- IF YOU WANT TO TRY THIS ON A TOUCH SCREEN DEVICE YOU WILL NEED TO VIEW WITHIN THE FULL SCREEN PREVIEW -->
<ul class="menu">
<li class="dropdown">
<a href="#" class="toplevel">This is a dropdown menu</a>
<ul class="submenu">
<li><a href="#">Lorem</a></li>
<li><a href="#">Ipsum</a></li>
<li><a href="#">Doler</a></li>
<li><a href="#">Sit amet</a></li>
</ul>
</li>
</ul>
$(function()
{
var $dropdowns = $('li.dropdown'); // Specifying the element is faster for older browsers
/**
* Mouse events
*
* @description Mimic hoverIntent plugin by waiting for the mouse to 'settle' within the target before triggering
*/
$dropdowns
.on('mouseover', function() // Mouseenter (used with .hover()) does not trigger when user enters from outside document window
{
var $this = $(this);
if ($this.prop('hoverTimeout'))
{
$this.prop('hoverTimeout', clearTimeout($this.prop('hoverTimeout')));
}
$this.prop('hoverIntent', setTimeout(function()
{
$this.addClass('hover');
}, 250));
})
.on('mouseleave', function()
{
var $this = $(this);
if ($this.prop('hoverIntent'))
{
$this.prop('hoverIntent', clearTimeout($this.prop('hoverIntent')));
}
$this.prop('hoverTimeout', setTimeout(function()
{
$this.removeClass('hover');
}, 250));
});
/**
* Touch events
*
* @description Support click to open if we're dealing with a touchscreen
*/
if ('ontouchstart' in document.documentElement)
{
$dropdowns.each(function()
{
var $this = $(this);
this.addEventListener('touchstart', function(e)
{
if (e.touches.length === 1)
{
// Prevent touch events within dropdown bubbling down to document
e.stopPropagation();
// Toggle hover
if (!$this.hasClass('hover'))
{
// Prevent link on first touch
if (e.target === this || e.target.parentNode === this)
{
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.