具有悬停功能的 if-else 条件?
我可以使用悬停功能设置 if-else 条件吗?我想在将鼠标悬停时加载文本链接旁边的页面,并且希望能够将鼠标悬停/鼠标悬停在加载的内容上。但是这个加载的内容在两种情况下会被删除:
但是我遇到了 2 号情况的问题 - 如果我对 2 号情况应用悬停功能,则 1 号情况就不会发生。当我的鼠标离开文本链接框时,加载的内容会立即被删除。
因此,我正在考虑如果可能的话将 else-if 条件添加到悬停函数中(或者如果您有任何其他更好的想法?)我想仅在情况编号未发生时才删除加载的内容。如果我将鼠标悬停在加载的内容上,则不要应用情况 2,直到我的鼠标离开加载的内容区域。
下面是 jQuery(针对情况 1):
$(document).ready(function() {
$(".button").hover(function(e){
$('.wrapper-item-content').remove();
var parent = $(this).parent();
$(this).parent().addClass('current');
var parent_top = parent.offset().top-180;
var parent_left = parent.offset().left+80;
$("body").append('<div class="wrapper-item-content"></div>');
$(".wrapper-item-content").css({
top: parent_top,
left: parent_left,
position: 'absolute',
zIndex: '100',
width: '350px',
height: '100%',
overflow: 'visible',
border: '1px solid #000'
});
var path_url = $(this).attr('href');
var path_file = $(this).attr('rel');
var item_wrapper = $('.wrapper-item-content');
var array_url = path_url.split('/');
var pg_url = $(array_url).last()[0];
item_wrapper.load(path_file+'?url='+pg_url, function(){
item_wrapper.hover(function() {
item_wrapper.addClass('mouseenter');
},function(){
item_wrapper.removeClass('mouseenter');
parent.removeClass('current');
item_wrapper.remove();
});
parent.hover(function() {
//something
},function(){
if(item_wrapper.hasClass('mouseenter'))
{
//alert('has mouseenter');
}
else
{
//alert('has no mouseenter');
//parent.removeClass('current');
//item_wrapper.remove();
}
});
});
},
function(){
});
});
html:
<div class="box"><a href="#" class="button" rel="content.php">Hover me</a></div>
Can I set if-else condition with hover function? I want to load a page next to the text link when I hover it and I want to be able to hover/ mouseover on the loaded content. But this loaded content will be removed under two situations:
but I have the problem with the situation number 2 - if I apply the hover function on number-2, the number 1 just won't happen. The loaded content is removed immediately when my mouse leave the text link box.
So, I am thinking to put else-if condition to the hover function if possible (or any other better ideas if you have any?) I want to remove the loaded content only if the situation number does not occur. If I have moused over on the loaded content, then don't apply situation number 2, until my mouse leave the loaded content area.
Below is the jQuery (for the situation number 1):
$(document).ready(function() {
$(".button").hover(function(e){
$('.wrapper-item-content').remove();
var parent = $(this).parent();
$(this).parent().addClass('current');
var parent_top = parent.offset().top-180;
var parent_left = parent.offset().left+80;
$("body").append('<div class="wrapper-item-content"></div>');
$(".wrapper-item-content").css({
top: parent_top,
left: parent_left,
position: 'absolute',
zIndex: '100',
width: '350px',
height: '100%',
overflow: 'visible',
border: '1px solid #000'
});
var path_url = $(this).attr('href');
var path_file = $(this).attr('rel');
var item_wrapper = $('.wrapper-item-content');
var array_url = path_url.split('/');
var pg_url = $(array_url).last()[0];
item_wrapper.load(path_file+'?url='+pg_url, function(){
item_wrapper.hover(function() {
item_wrapper.addClass('mouseenter');
},function(){
item_wrapper.removeClass('mouseenter');
parent.removeClass('current');
item_wrapper.remove();
});
parent.hover(function() {
//something
},function(){
if(item_wrapper.hasClass('mouseenter'))
{
//alert('has mouseenter');
}
else
{
//alert('has no mouseenter');
//parent.removeClass('current');
//item_wrapper.remove();
}
});
});
},
function(){
});
});
The html:
<div class="box"><a href="#" class="button" rel="content.php">Hover me</a></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
hover()
事件可以采用另一个函数,该函数在鼠标离开时调用。http://api.jquery.com/hover/
the
hover()
event can take another function which is called when the mouse leaves.http://api.jquery.com/hover/