当您使用Javascript点击或停止悬停时,如何使CSS悬停内容保持原位? [英] How can you make CSS on-hover content stay in place when you click or stop hovering using Javascript?

查看:429
本文介绍了当您使用Javascript点击或停止悬停时,如何使CSS悬停内容保持原位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想实现的身体系统功能。当用户悬停在身体​​部位上时,它突出显示该特定身体部位的信息。我已经按照我想要的方式对CSS进行编码,但我不知道任何JavaScript ,以便在点击正文部分或鼠标离开悬停状态时保持信息。

I have a body system feature I'd like to implement. When the user hovers over a body part, it highlights and shows information on that specific body part. I've coded the CSS the way I want it, but I don't know anything about JavaScript to get the information to stick when the body part is clicked or the mouse leaves the hover state.

我搜索了论坛,发现了类似的问题,花了几个小时试图从别人的javascript解决方案中找出自己 - 我在我需要问

I've searched the forum and found similar issues and have spent hours trying to figure this out myself from others' javascript solutions - I'm at the point where I need to ask for help.

这是一个闪光灯原型我所需的效果:

Here is a flash prototype I made of my desired effect:

http://inwavemedia.com/temp/proto/Main.html

如果你想看看我现在的情况,这里是现在的HTML:

Here is the live HTML if you want to take a look at what I have now:

http://inwavemedia.com/temp/excretory.html

以下是我的代码:

<style type="text/css">
#bodysystem-excretory {
 width: 618px;
 height: 504px;
 background: url(excretory.png) no-repeat;
 margin: 10px auto; padding: 0;
 position: relative;
 border: 1px solid #999;
}
#bodysystem-excretory li {
    margin: 0;
    padding: 0;
    list-style: none;
    display: block;
    position: absolute;
}

#bodysystem-excretory a {
    display: block;
/*  text-indent: -9999px;*/
    text-decoration: none;
}

#esoph {
    left: 85px;
    top: 41px;
    width: 46px;
    height: 94px;
    z-index: 10;
}

#lungs {
    left: 76px;
    top: 84px;
    width: 84px;
    height: 68px;
    z-index: 20;
}

#bladder {
    left: 87px;
    top: 148px;
    width: 64px;
    height: 104px;
    z-index: 30;
}

#esoph a {
    height: 94px;
}

#lungs a {
    height: 67px;
}

#bladder a {
    height: 104px;
}


#esoph a:hover {
    background-image: url(excretory.png);
    background-repeat: no-repeat;
    background-position: -25px -561px;
}

#lungs a:hover {
    background-image: url(excretory.png);
    background-repeat: no-repeat;
    background-position: -105px -523px;
}

#bladder a:hover {
    background-image: url(excretory.png);
    background-repeat: no-repeat;
    background-position: -114px -618px;
}

.info span{ 
    display: none

}

.info{
    position:relative;
    z-index:1124;
    color:#000;
}

.info:hover{
    z-index:1125;

}

.info:hover span{
    display:block;
    position:absolute;
    top:-30px;
    left:155px;
    width:370px;
    color:#000;
    background-color:#FFFFFF;
}
</style>

</head>

<body>
<ul id="bodysystem-excretory">
  <li id="esoph">
    <a href="#" class="info"><span id="esoph-info"><h3>Esophagus</h3><p>This is esophagus information. This is esophagus information. This is esophagus information. This is esophagus information. This is esophagus information. This is esophagus information. This is esophagus information. </p></span></a>
  </li>
<li id="lungs"><a href="#" class="info"><span id="lungs-info"><h3>Lungs</h3></span></a></li>
<li id="bladder"><a href="#" class="info"><span id="bladder-info"><h3>Bladder</h3></span></a></li>
</ul>


推荐答案

做你想要的。这是我的建议。请注意,CSS已更改,以及在末尾添加JavaScript。

I don't think either of the previous answers quite do what you were looking for. This is my suggestion. Note that the CSS is altered as well as javascript added at the end.

<html>
<head><style type="text/css">
#bodysystem-excretory {
     width: 618px; height: 504px;
     background: url("excretory.png") no-repeat;
     margin: 10px auto; padding: 0;
     position: relative;
     border: 1px solid #999;
}
#bodysystem-excretory li {
    margin: 0; padding: 0;
    list-style: none;
    display: block;
    position: absolute;
}
#bodysystem-excretory a {
    display: block;
    /*  text-indent: -9999px;*/
    text-decoration: none;
}
#esoph {
    left: 85px; top: 41px;
    width: 46px; height: 94px;
    z-index: 10;
}
#lungs {
    left: 76px; top: 84px;
    width: 84px; height: 68px;
    z-index: 20;
}
#bladder {
    left: 87px; top: 148px;
    width: 64px; height: 104px;
    z-index: 30;
}
#esoph a {
    height: 94px;
}
#lungs a {
    height: 67px;
}
#bladder a {
    height: 104px;
}
#esoph:hover, #esoph.selected {
    background-image: url("excretory.png") no-repeat -25px -561px;
}
#lungs:hover, #lungs.selected {
    background-image: url("excretory.png") no-repeat -105px -523px;
}
#bladder:hover, #bladder.selected {
    background-image: url("excretory.png") no-repeat -114px -618px;
}
.info span{ 
    display: none
}
.info{
    position:relative;
    z-index:1124;
    color:#000;
}
.selected .info{
    z-index:1125;
}
.selected .info span {
    display:block; position:absolute;
    top:-30px; left:155px;
    width:370px;
    color:#000; background-color:#FFFFFF;
}​</style></head>
<body>
<ul id="bodysystem-excretory">
<li id="esoph">
<a href="#" class="info">
<span id="esoph-info"><h3>Esophagus</h3>
<p>
This is esophagus information. This is esophagus information. 
This is esophagus information. This is esophagus information.
This is esophagus information. This is esophagus information.
This is esophagus information. </p></span></a>
</li>
<li id="lungs"><a href="#" class="info">
<span id="lungs-info"><h3>Lungs</h3></span></a>        
</li>
<li id="bladder"><a href="#" class="info">
<span id="bladder-info"><h3>Bladder</h3>
</span></a></li>
</ul>​<script type="text/javascript">
// Get the <li> elements as a list called 'parts'
var parts = 
    document.getElementById('bodysystem-excretory').getElementsByTagName('li');
function getClickFunction(part) {
    return function() {
// This is the function that will be called when one of the <li>s is clicked
        if (part.className == 'selected') {    // if the body part is already selected
            part.className = '';               // ... then deselect it
        }
        else {                                 // otherwise,
            // first deselect all of the body parts
            for (var i = 0; i < parts.length; i++) {
                parts[i].className = '';
            }
            // then select the one that's been clicked
            part.className = 'selected';
        }
    }
}
// Now, attach this function to all of the <li> elements representing body parts      
for (var i = 0; i < parts.length; i++) {
    parts[i].onclick = getClickFunction(parts[i]);
}​
</script></body></html>

这篇关于当您使用Javascript点击或停止悬停时,如何使CSS悬停内容保持原位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆