绑定鼠标事件时出现问题
我有一张图像表。当鼠标悬停在每一行上时,会在之前隐藏的 div 中显示其图像,该 div 已经绝对定位。当我将鼠标悬停在现在显示的图像上时,我想取消绑定 tr mouseleave 事件,以便图像不会闪烁,然后在离开图像 div 时重新绑定 mouseleave 事件。
我能够取消绑定 mouseleave 事件,但重新绑定会导致发生闪烁。相关代码:
<table border="1" id="photoTable">
<tbody>
<tr class="item">
<td class="filename">
GraphDataCAQ3UW88.png
</td>
</tr>
</tbody>
</table>
<div id="thisPhoto"></div>
css:
#thisPhoto{
display:none;
position:absolute;
left:250px;
top:150px;
border: 1px solid black;
background-color:white;
padding:20px;
z-index:2;
}
js:
$(document).ready(function(){
(function($){
$.fn.getThisPhoto = function(){
return $(this).each(function(){
//I've left out the code which gets the data to pass to $.get()
$.get('administration/PhotoManagement/thisPhoto.cfm',
data,
function(response){
$('#thisPhoto').html(response).show();
}
);
});
};
})(jQuery);
$('tr.item').hover(function(){
$(this).getThisPhoto();
},
function(){
$('#thisPhoto').hide();
});
$('#thisPhoto').mouseenter(function(){
$('tr.item').unbind('mouseleave');
}).mouseleave(function(){
$('tr.item').bind('mouseleave', function(){
$('#thisPhoto').hide();
});
});
});
编辑:我通过将整个shebang包装在div中并在该div上设置mouseout来触发hide()和bind()函数来破解它......不完全是我想要的,但它'紧要关头我会做的。
I have a table of images. Each row, when moused over, shows its image in a previously hidden div that's been absolutely positioned. When I mouse over the now shown image, I want to unbind the tr mouseleave events so that the image doesn't flicker, and then rebind the mouseleave events when I leave the image div.
I'm able to unbind the mouseleave event, but rebinding causes the flickering to occur. The relevant code:
<table border="1" id="photoTable">
<tbody>
<tr class="item">
<td class="filename">
GraphDataCAQ3UW88.png
</td>
</tr>
</tbody>
</table>
<div id="thisPhoto"></div>
css:
#thisPhoto{
display:none;
position:absolute;
left:250px;
top:150px;
border: 1px solid black;
background-color:white;
padding:20px;
z-index:2;
}
js:
$(document).ready(function(){
(function($){
$.fn.getThisPhoto = function(){
return $(this).each(function(){
//I've left out the code which gets the data to pass to $.get()
$.get('administration/PhotoManagement/thisPhoto.cfm',
data,
function(response){
$('#thisPhoto').html(response).show();
}
);
});
};
})(jQuery);
$('tr.item').hover(function(){
$(this).getThisPhoto();
},
function(){
$('#thisPhoto').hide();
});
$('#thisPhoto').mouseenter(function(){
$('tr.item').unbind('mouseleave');
}).mouseleave(function(){
$('tr.item').bind('mouseleave', function(){
$('#thisPhoto').hide();
});
});
});
EDIT: I hacked it by wrapping the whole shebang in a div and setting mouseout on that div to trigger the hide() and bind() functions... not exactly what I wanted, but it'll do in a pinch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定此模型是否适合您的要求,但请考虑将
div#thisPhoto
制作成tr
内的单独div
。这样,当您将鼠标悬停在图像上时,您仍然将鼠标悬停在表格行上。例如,标记类似于:如果您为
div.thePhoto
提供position:relative
样式,则可以为div.thePhoto > 提供
一个position:relative
样式。 imgposition:absolute
样式,并将其相对于表格单元格的左上角定位。这样,您只需将.hover()
事件绑定到每个表格行到.find("div.thePhoto")
并显示或隐藏其子img
元素。有关示例,请参阅此小提琴。
I am uncertain whether this model will work for your requirements, but consider making the
div#thisPhoto
into individualdiv
s inside yourtr
. This way, while you are moused over the image, you are still moused over the table row. For instance, the markup would be something like:If you give
div.thePhoto
aposition: relative
style, you can then give thediv.thePhoto > img
aposition: absolute
style and position it relative to the top left corner of the table cell. This way, you only bind a.hover()
event to each table row to.find("div.thePhoto")
and display or hide its childimg
element.See this fiddle for an example.
您不需要绑定和取消绑定事件,您需要使用不同的设计模式。
看看这个小提琴: http://jsfiddle.net/Diodeus/gHa4u/1/
You don't need to bind and unbind the event, you need to use a different design pattern.
Take a look at this fiddle: http://jsfiddle.net/Diodeus/gHa4u/1/