[英]Make div appear when hovering over list item?
I'm having a ton of trouble making a div appear on my site when the user hovers over a list item.当用户将鼠标悬停在列表项上时,我在让 div 出现在我的网站上时遇到了很多麻烦。 Basically, I have a list of links.基本上,我有一个链接列表。 When the user hovers over one of them, I need two divs to appear - one being a background box that is common to all of the hovers, and the other being full of content that is relevant to that list item.当用户将鼠标悬停在其中一个上时,我需要出现两个 div - 一个是所有悬停通用的背景框,另一个是与该列表项相关的内容。 I also need all the divs to disappear when the mouse leaves the "row2" area (which everything is contained within).当鼠标离开“row2”区域(所有内容都包含在其中)时,我还需要所有 div 消失。 The script I have below is semi-working, however, if a user moves their mouse quickly from one list item to the next, the items "stack up" and don't disappear properly.我在下面的脚本是半工作的,但是,如果用户将鼠标从一个列表项快速移动到下一个列表项,则项目“堆叠”并且不会正确消失。
You can see this issue here:你可以在这里看到这个问题:
Here is the jQuery I currently have:这是我目前拥有的 jQuery:
$(document).ready(function()
{
$(".subjectarea_box,.subjectarea_box > div").hide();
$(".subjectarea_topics li[class!='arts-culture-recreation']").mouseover( function()
{
$("div.arts-culture-recreation").hide();
});
$("li.arts-culture-recreation").mouseover( function()
{
$("div.arts-culture-recreation,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.arts-culture-recreation,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='civic-vitality']").mouseover( function() {
$("div.civic-vitality").hide();
});
$("li.civic-vitality").mouseover( function() {
$("div.civic-vitality,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.civic-vitality,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='demographics']").mouseover( function() {
$("div.demographics").hide();
});
$("li.demographics").mouseover( function() {
$("div.demographics,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.demographics,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='economy']").mouseover( function() {
$("div.economy").hide();
});
$("li.economy").mouseover( function() {
$("div.economy,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.economy,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='education']").mouseover( function() {
$("div.education").hide();
});
$("li.education").mouseover( function() {
$("div.education,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.education,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='environment-transportation']").mouseover( function()
{
$("div.environment-transportation").hide();
});
$("li.environment-transportation").mouseover( function() {
$("div.environment-transportation,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.environment-transportation,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='financial-self-sufficiency']").mouseover( function() {
$("div.financial-self-sufficiency").hide();
});
$("li.financial-self-sufficiency").mouseover( function() {
$("div.financial-self-sufficiency,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.financial-self-sufficiency,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='health']").mouseover( function() {
$("div.health").hide();
});
$("li.health").mouseover( function() {
$("div.health,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.health,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='housing']").mouseover( function() {
$("div.housing").hide();
});
$("li.housing").mouseover( function() {
$("div.housing,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.housing,div.subjectarea_box,.subjectarea_box > div").hide();
});
$(".subjectarea_topics li[class!='public-safety']").mouseover( function() {
$("div.public-safety").hide();
});
$("li.public-safety").mouseover( function() {
$("div.public-safety,div.subjectarea_box").fadeIn();
});
$("div.row2").mouseleave( function() {
$("div.public-safety,div.subjectarea_box,.subjectarea_box > div").hide();
});
});
</script>
... it's a monster, I know (and probably terribly inefficient). ......这是一个怪物,我知道(而且可能非常低效)。
Can someone recommend a better and less buggy way to accomplish this task?有人可以推荐一种更好且错误更少的方法来完成此任务吗?
Thank you!!!谢谢!!!
Maybe something a little less verbose would be more manageable, I knocked together a rough example where I'm holding the reference of the div to show in a rel attribute of the link.也许稍微不那么冗长的东西会更易于管理,我拼凑了一个粗略的例子,其中我持有 div 的引用以显示在链接的 rel 属性中。 It cuts down on the shear amount of code you need for a relatively simple task - although this example may not be exactly as you describe it - it's a much simpler approach.它减少了执行相对简单的任务所需的代码量——尽管这个例子可能与你描述的不完全一样——这是一种更简单的方法。
http://jsfiddle.net/tkVZ4/ http://jsfiddle.net/tkVZ4/
HTML: HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<ul id="links">
<li><a rel="one" href="#">one fish</a></li>
<li><a rel="two" href="#">two fish</a></li>
<li><a rel="red" href="#">red fish</a></li>
</ul>
<div id="container">
container
<div id="one">one</div>
<div id="two">two</div>
<div id="red">red</div>
</div>
</body>
</html>
CSS: CSS:
#container, #container div{
display: none;
}
Javascript: Javascript:
$(function(){
$('#links li a').mouseover(function(){
var toShow = '#' + $(this).attr('rel');
$('#container').show();
$(toShow).show();
}).mouseout(function(){
$('#container, #container div').hide();
})
});
To be honest, I'm not taking a very hard look at this, but what if you swapped out your "mouseovers" with "mouseenters"?老实说,我并没有认真考虑这一点,但是如果您将“mouseovers”换成“mouseenters”会怎样? (That's what you usually pair with "mouseleave"...) (这就是你通常与“mouseleave”配对的......)
It sounds like you are attempting to accomplish nice looking tooltips/balloon messages.
听起来您正在尝试完成漂亮的工具提示/气球消息。
While I dont always like just suggesting a jquery plugin to do things, this would be a good use for one.
虽然我并不总是喜欢仅仅建议一个 jquery 插件来做事,但这对一个人来说是一个很好的用途。
This is a pretty good plugin
http://flowplayer.org/tools/tooltip/index.html , as well as
http://jquery.bassistance.de/tooltip/demo/
这是一个相当不错的插件
http://flowplayer.org/tools/tooltip/index.html ,还有
http/tooltipde//jquery/bassistance。
Sorry I dont have an immediate response, little hard to trouble shoot without working with some code.抱歉,我没有立即回复,如果不使用一些代码很难解决问题。 If no one else posts an acceptable response, consider putting up a jsfiddle demo.如果没有其他人发布可接受的响应,请考虑放置一个jsfiddle演示。
This is a little unrelated, but while what you have probably works fine and I just havent seen the syntax before, it is much more common to see selectors like:这有点不相关,但是虽然你可能工作得很好而且我以前没有见过语法,但更常见的是看到如下选择器:
$(".subjectarea_topics li[class!='arts-culture-recreation']")
written as:写成:
$(".subjectarea_topics li:not(.arts-culture-recreation)")
You relaly need to work on cutting down on redundant code, it will make the problem a lot easier to solve.您需要努力减少冗余代码,这将使问题更容易解决。
I suggest you setup your elements with the information in themselves to specify which div will display on mouseover.我建议您使用自身的信息设置元素,以指定鼠标悬停时将显示哪个 div。
try something like:尝试类似:
$("li.arts-culture-recreation, li.civic-vitality, li.demographics") //Etc etc
.mouseover(function
{
var cssClass = $(this).attr("class");
$("div.subjectarea_box, div." +cssClass).show();
}).mouseleave(function()
{
var cssClass = $(this).attr("class");
$("div.subjectarea_box, div." + cssClass).hide();
});
jsfiddle example: http://jsfiddle.net/9jHyD/ jsfiddle 示例: http://jsfiddle.net/9jHyD/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.