显示/隐藏多个元素悬停与JQuery和HTML
Show/hide multiple elements on hover with JQuery and HTML
这是我目前所看到的:
<div style = "position: relative;">
<a href = "#games">
<div class="sidenavOff">
<img src = "images/card_normal.png" />
<img src = "images/category_icons/icon_games.png" style = "position: absolute; top: 10px; left: 40px;" />
<img src = "images/category_titles/title_games.png" style = "position: absolute; top: 160px; left: 40px;" />
</div>
<div class = "sidenavOver">
<img src = "images/hover/card_hover.png" />
<img src = "images/category_titles/title_games.png" style = "position: absolute; top: 10px; left: 40px;" />
<img src = "images/hover/card_hover_separator.png" style = "position: absolute; top: 40px; left: 40px;" />
Show a bunch of text here
<img src = "images/button_start_normal.png" style = "position: absolute; top: 200px; left: 40px;" />
/div>
</a>
</div>
所以card.png是一个有多个透明图像叠加在上面的记事卡。当鼠标远离卡片时,它会在卡片上显示icon_games.png和title_games.png。我希望当鼠标悬停在card.png, icon_games.png或title_games.png上时(换句话说,每当鼠标指针在卡片上时),卡片会垂直显示元素title_games.png, card_hover_separator.png,文本描述和button_start_normal.png(这个位置应该是可编辑的,因为它可能与不悬停时显示的图像不同)。
这是我的jquery代码(我以前从未使用过它,所以我很确定这是关闭的。我不太明白):
$(document).ready(function() {
$(“div.sidenavOff”).mouseover(function(){
$(this).removeClass().addClass(“sidenavOver”);
}).mouseout(function(){
$(this).removeClass().addClass(“sidenavOff”);
});
});
在一个更容易理解的格式,没有悬停:
http://img834.imageshack.us/img834/7026/screenshot20130606at122.png 与徘徊:http://imageshack.us/photo/my-images/855/screenshot20130606at122.png/这是我的jquery代码[…]。我不太明白
$(document).ready(function () {/* function body */});
当document
(作为一个jQuery对象$
)为ready
时,按照/* function body */
$("div.sidenavOff")
使用jQuery $
获取所有与CSS选择器div.sidenavOff
.mouseover(function () {
$(this).removeClass().addClass("sidenavOver");
})
当鼠标移到其中一个元素(mouseover
)上时,删除类 undefined
(括号之间没有任何内容,removeClass
),然后将类 sidenavOver
(addClass
)添加到鼠标所经过的元素(this
)上。这里的class可以理解为与HTML属性class的含义相同;<a class="xyz">
.mouseout(function () {
$(this).removeClass().addClass("sidenavOff");
})
当鼠标离开其中一个元素(mouseout
)时,类似于鼠标经过这些元素时,不同之处在于这次为该元素添加了sidenavOff
类。
你很接近了,但可能想要这样的代码
$(document).ready(function () {
$("div.sidenavOff").mouseover(function () { // add visibility flag
$("div.sidenavOver").addClass("showme"); // to div.sidenavOver
}).mouseout(function () { // remove visibility flag
$("div.sidenavOver").removeClass("showme"); // from div.sidenavOver
});
});
其中showme
类与一些CSS相关,以强制元素显示
.showme {display: block;}
这听起来就像你只是试图显示或隐藏"sidenavOver"div当鼠标悬停在容器。对吗?
我使用html创建了这个jsfiddle,但是注释掉了所有缺失的图像,并在容器div中添加了一个类。http://jsfiddle.net/joshvito/GaZQ6/
。
//on hover over the container; you can use the a tag too, which ever element you want to bind the event to
$('.container').on({
mouseenter: function () {
$(".sidenavOff").hide(); //On mouseover, hode the first div
$(".sidenavOver").show();
},
mouseleave: function () {
$(".sidenavOff").show();
$(".sidenavOver").hide();
}
});
JQUERY API Documentation中.on
- Jquery html() 和保留元素名称
- 函数jquery.html()不提供数据属性集值
- 如何逃离<脚本>jquery html()中的标记.我使用了innerHTML和outerHTML来解决这个
- 防止“;jQuery(html)"防止触发浏览器请求图像和其他参考内容
- 使用jquery.html()获取元素本身的html
- 在iframe的情况下,jQuery html()将失败
- 如何在使用jQuery.html()时防止javascript执行
- jquery.html有效,但是.add不行;t
- jQuery.html()方法语法
- jQuery.html()赢得't替换javascript图表
- 如何在Jquery html elemet属性中附加字符串变量
- 等效于 AngularJS 中的 jQuery html()
- jQuery .html('
') 在 FF 和 Opera 中不起作用
- jquery html()编写函数以包含语义html元素
- jQuery Html负载剥离<表单>标签
- 用jQuery.html更新span内部html在IE中不起作用
- jquery html更新可以在电脑上使用,但不能在智能手机上使用
- 使用jQuery.html设置带有提交按钮的表单
- 将jquery HTML方法中的数据检索到数组中
- 来自对象的jQuery.html()返回未定义