我目前使用這個腳本在一個WordPress主題我的下拉菜單項:http://jsfiddle.net/i_like_robots/6JbtX/鍵盤可以使用導航功能
$(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)
{
e.preventDefault();
}
// Hide other open dropdowns
$dropdowns.removeClass('hover');
$this.addClass('hover');
// Hide dropdown on touch outside
document.addEventListener('touchstart', closeDropdown = function(e)
{
e.stopPropagation();
$this.removeClass('hover');
document.removeEventListener('touchstart', closeDropdown);
});
}
}
}, false);
});
}
});
不過,我需要這些物品是鍵盤訪問。
任何人都可以指向正確的方向嗎?
謝謝!