У меня есть изображение, и при наведении мыши оно исчезает в две стрелки (слева и справа), затем при наведении мыши оно исчезает. Моя проблема заключается в том, что когда пользователь наводит курсор на стрелки, изображение считает его мышиным (поскольку стрелки всплывают над изображением) и затухает стрелки, вызывая бесконечную петлю затухания, пока вы не отодвинете мышь. Каков наилучший способ предотвратить исчезновение стрелок при наведении курсора мыши на них? Я попробовал один метод, который вы увидите ниже, но он не сработал ...
Вот код:
$(".arrow").mouseover(function() {
overArrow = 1;
$("#debug_oar").text(1)
})
$(".arrow").mouseout(function() {
overArrow = 0;
$("#debug_oar").text(0)
})
$("#image").mouseout(function() {
$(".arrow").fadeOut(250,function() { $(".arrow").remove();})
})
$("#image").mouseover(function() {
if(overArrow == 0) {
$("#image").after("<div class='arrow' id='lArrow' style='display:none;position:absolute;'>←</div><div class='arrow' id='rArrow' style='display:none;position:absolute;'>→</div>")
// Define variables...
aWidth = $("#lArrow").width();
aHeight = $("#lArrow").height();
height = $("#image").height()
width = $("#image").width()
pos = $("#image").position();
// Calculate positioning
nHeight = height/2
y = pos.top + nHeight - (aHeight/2)
// Position the left arrow
$("#lArrow").css("top",y)
$("#lArrow").css("left",pos.left+10)
// Now the right...
$("#rArrow").css("top",y)
$("#rArrow").css("left",pos.left+width-aWidth-20)
// Display 'em
$(".arrow").fadeIn(250);
// Debug info
$("#debug_x").text(pos.left)
$("#debug_y").text(y)
$("#debug_height").text(height)
}
})
Спасибо
Для тех, кто интересуется финальным кодом:
$("#image").mouseenter(function() {
$("#image").append("<div class='arrow' id='lArrow' style='display:none;position:absolute;'>←</div><div class='arrow' id='rArrow' style='display:none;position:absolute;'>→</div>")
// Define variables...
aWidth = $("#lArrow").width();
aHeight = $("#lArrow").height();
height = $("#image").height()
width = $("#image").width()
pos = $("#image").position();
// Calculate positioning
nHeight = height/2
y = pos.top + nHeight - (aHeight/2)
// Position the left arrow
$("#lArrow").css("top",y)
$("#lArrow").css("left",pos.left+10)
// Now the right...
$("#rArrow").css("top",y)
$("#rArrow").css("left",pos.left+width-aWidth-20)
// Display 'em
$(".arrow").fadeIn(250);
// Debug info
$("#debug_x").text(pos.left)
$("#debug_y").text(y)
$("#debug_height").text(height)
});
$("#image").mouseleave(function() {
$(".arrow").fadeOut(250,function() { $(".arrow").remove();})
})