是ie&萤火虫
is(:hover) alternative for ie & firefox
我有一个菜单,滚动时会显示一个不是菜单子级的div,当滚动时会再次隐藏该div。div直接放置在菜单项的下方,模仿子菜单。
html看起来像这样-
这是我的菜单-
<div id="nav">
<ul>
<li class='with_panel'>
<span id='panel1' class='current'><img src='theImage' /></span>
</li>
<!-- more list items -->
</ul>
</div>
在一个不相关的div中,我有这个-
<div id="panels">
<div style="" id="panel1_panel" class="panel">
<img src="myImage.png">
</div>
<div>
我有一些jquery,当你滚动li-时,它会显示和隐藏相关的面板
$("#nav .with_panel").mouseenter(function(){
var id = $(this).find("span").attr("id");
$(".panel").removeClass("open");
$panel = $("#" + id + "_panel");
$panel.addClass("open");
$img = $(this).find("span img");
$img.addClass("on");
var hide = function(){
if(!$panel.is(":hover")){
$img.removeClass("on");
$panel.removeClass("open");
}
}
$panel.mouseleave(hide);
$(this).mouseleave(hide);
})
这似乎只适用于Chrome,我相当确定这是由于ie&Firefox无法识别。is(":悬停")。
我无法更改html结构,只能更改javascript。所以我很难让它跨浏览器工作。有什么想法吗?
下面的代码给你200毫秒的超时时间(hide_to
),让你在隐藏菜单之前离开菜单并进入面板。如果鼠标进入菜单项或面板,隐藏的超时将被取消,并在鼠标离开其中任何一个时重新启动。
$("#nav .with_panel").each(function() {
var id = $(this).find("span").attr("id"),
$panel = $("#" + id + "_panel"),
$img = $(this).find("span img"),
hide_to = null;
var hide = function() {
// start hide timeout
hide_to = window.setTimeout(function () {
$img.removeClass("on");
$panel.removeClass("open");
},200);
};
var show = function() {
// clear hide timeout
window.clearTimeout(hide_to);
if (!$panel.is(".open"))
{
// open panel, only if it is not open already
$(".panel").removeClass("open");
$panel.addClass("open");
$img.addClass("on");
}
};
$(this).mouseenter(function() {
show();
}).mouseleave(function() {
hide();
});
$panel.mouseenter(function() {
show();
}).mouseleave(function() {
hide();
});
});
试着用mouseover代替mouseenter,用mouseout代替mouseleave。
例如:
$("#nav .with_panel").mouseover(function(){
var id = $(this).find("span").attr("id");
$(".panel").removeClass("open");
$panel = $("#" + id + "_panel");
$panel.addClass("open");
$img = $(this).find("span img");
$img.addClass("on");
var hide = function(){
if(!$panel.is(":hover")){
$img.removeClass("on");
$panel.removeClass("open");
}
}
$panel.mouseout(hide);
$(this).mouseout(hide);
})
从jQuery 1.9.1开始,所有浏览器的.is(":hover")似乎都已损坏。真倒霉以下是用jQuery 1.9.1在FF、IE10和Chrome(抱歉没有Opera)上测试的。Opera用户,请评论这是否适用于Opera。
function isHovered(elt){
var temp = $(elt).parent().find(":hover");
return temp.length == 1 && temp[0] == elt;
}
然后更换
$panel.is(":hover")
通过
isHovered($panel[0])
否则,保留所有代码。
小提琴:http://jsfiddle.net/mathheadinclouds/BxL4w/
相关文章:
- jquery点击函数select&取消选择
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- Angular js-返回一个包含类似
- 一点javascript元编程&可链接的设置器
- 通过JSON&比较时间
- Jquery模板,如果xx&&如果yy
- jQuery工具验证器自定义效果-添加&消除影响
- Twitter引导程序Typeahead-Id&标签
- AngularJS&JSON-从Previous&下一个对象
- 什么是&&在没有if的行中的变量之间
- 关于引入外部javascript文件的问题&css通过https
- 指令的模板必须只有一个根元素:With restrict E&替换true
- 使用ajax的服务器端分页&jQuery
- Javascript,如果条件在没有&&逻辑运算符当&&它不起作用
- 如何使用dropzone&vueJs
- 如何准确执行加载脚本&退出弹出窗口
- 如何添加CSS&JavaScript进入Windows窗体应用程序C#.net
- 视差/translate3d在safari&萤火虫
- YSlow&萤火虫得分99/100(最后一分)
- 是ie&萤火虫