Я пытаюсь отложить добавление и удаление классов в элементы, когда пользователь наводится на элемент:
$('#thumbs div').mouseenter(function() {
$('#thumbs div').removeClass('hovered');
$(this).addClass('hovered');
});
Есть ли способ сделать это? Например, мне нужна задержка около 0,3 секунды.
Благодарю!
Попробуй это:
$("#thumbs div").mouseenter(function() {
$(this).removeClass('hovered');
setTimeout("addClassCustom", 300);
});
function addClassCustom() {
$("#thumbs div").addClass('hovered');
}
$("#thumbs div").mouseenter(function() {
var self = $(this);
self.removeClass('hovered');
window.setTimeout(function() {
self.addClass('hovered');
}, 300);
});
Вы можете попробовать что-то вроде этого
$("#thumbs div").on('mouseenter', function() {
var el = $(this);
setTimeout(function() {
$('#thumbs div').removeClass('hovered');
el.addClass('hovered');
}, 300);
}).on('mouseleave', function(){
var el = $(this);
setTimeout(function() {
el.removeClass('hovered');
}, 300);
});
Вы можете поставить в очередь:
$("#thumbs div").mouseenter(function() {
$(this).delay(300).queue(function(next){
$('#thumbs div').removeClass('hovered');
$(this).addClass('hovered');
next();
});
});
Вероятно, вы захотите отменить его/очистить очередь, если мышь уйдет с чем-то вроде http://api.jquery.com/clearQueue/.