Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Задержка setTimeout и mouseenter (https://javascript.ru/forum/misc/41226-zaderzhka-settimeout-i-mouseenter.html)

webinsoul 04.09.2013 20:07

Задержка setTimeout и mouseenter
 
Код
$(document).ready(function(){
	$(".annotation span").mouseenter(function() {
	$(this).next("em").animate({opacity: "show", top: "-70"}, "fast");	});


	$(".annotation span").mouseleave(function() {
	$(this).next("em").animate({opacity: "hide", top: "-70"}, "fast");	}); 
});


Нужно, чтоб выполнялся animate только при задержке курсора на ".annotation span" от 1 секунды. На сколько я понял, следует вставить setTimeout(). Пробовал - безрезультатно.
Ткните плз, носом :)

То есть, чтоб при случайном наведении не выполнялся "animate".

рони 04.09.2013 20:56

webinsoul,
Реакция на нааведение мыши

webinsoul 04.09.2013 21:36

рони,
спасибо за ссылку. Подставил свои значения (скорей всего накосячил):

$(document).ready(function () {
var timer;
    $(".annotation span").mouseenter(function () {
        timer = window.setTimeout(function () {
		 $(this).next("em").stop(true, true).animate({opacity: "show", top: "-70"}, 100);
        }, 1000)



    }).mouseleave(function () {
        window.clearTimeout(timer)
         $(this).next("em").animate({opacity: "hide", top: "-40"}, 100);
    });
});



mouseleave работает, а mouseenter - нет. Подскажите где ошибка.

Яростный Меч 04.09.2013 21:46

$(".annotation span").mouseenter(function () {
        var th = $(this);
        timer = window.setTimeout(function () {
         th.next("em").stop(true, true).animate({opacity: "show", top: "-70"}, 100);
        }, 1000)

рони 04.09.2013 21:52

webinsoul,
откуда таймеру знать что есть this? сохраните в переменной и в функции замените $(this)
$(document).ready(function () {
var timer;
    $(".annotation span").mouseenter(function () {
        var el =  $(this);
        timer = window.setTimeout(function () {
		 el.next("em").stop(true, true).animate({opacity: "show", top: "-70"}, 100);
        }, 1000)



    }).mouseleave(function () {
        window.clearTimeout(timer)
         $(this).next("em").animate({opacity: "hide", top: "-40"}, 100);
    });
});


Часовой пояс GMT +3, время: 06:15.