CSS悬停来改变其他元素

CSS Hover to change other elements

本文关键字:其他 元素 改变 悬停 CSS      更新时间:2023-09-26

我用CSS做了这个导航栏,我希望当我把鼠标悬停在3个导航图标中的一个上时,它会改变主图像的背景位置。

我试了几种方法,但都没能使它们正常工作。

CSS

    /* Nav Button 1 */
.nav1 {
    float:left;
    padding-top:20px;
    border:none;
    outline:none;
}
#nav1
{
  display: block;
  width: 92px;
  height: 107px;
  background: url("images/triangle.png") no-repeat 0 0;
}
#nav1:hover
{ 
  background-position: 0 -107px;
}

/* Nav Button 2 */
.nav2 {
    float:left;
    padding-top:20px;
    border:none;
    outline:none;
    padding-right: 0px;
}
#nav2
{
  display: block;
  width: 92px;
  height: 107px;
  background: url("images/triangle.png") no-repeat 0 0;
}
#nav2:hover
{ 
  background-position: 0 -107px;
}
/* Nav Button 3 */
.nav3 {
    float:left;
    padding-top:20px;
    border:none;
    outline:none;
    padding-right: 0px;
}
#nav3
{
  display: block;
  width: 92px;
  height: 107px;
  background: url("images/triangle.png") no-repeat 0 0;
}
#nav3:hover
{ 
  background-position: 0 -107px;
}
/* Nav Name */
.navname {
    float:left;
    padding-top:20px;
    border:none;
    outline:none;
    padding-right: 0px;
}
#navname
{
  display: block;
  width: 228px;
  height: 81px;
  background: url("images/blank.png") no-repeat 0 0;
}

<div id="navigation">
    <div class="nav1">
    <a id="nav1" href="#"></a>
    </div>
    <div class="nav2">
    <a id="nav2" href="#"></a>
    </div>
    <div class="nav3">
    <a id="nav3" href="#"></a>
    </div>
    <div class="navname"><a id="navname" href="#"></a>
    </div>
</div>

你知道怎么做吗?当鼠标悬停在nav1, nav2, nav3上时,我想改变'navname'元素的背景位置。

谢谢:D

这是非常简单的使用jQuery:

$('element-to-initiate-change').hover(function(){
    //this will happen when your mouse moves over the object
    $('element-to-change').css({
        "property":"value",
        "property":"value",
    });
},function(){
    //this is what will happen when you take your mouse off the object
    $('element-to-change').css({
        "property":"value",
        "property":"value",
    });
});

下面是一个例子:http://jsfiddle.net/dLbDF/258/

如果你用你的代码做了一个jsfiddle,我会对你的代码这样做,这样你可以看得更清楚。问问题的时候一定要用jsfiddle或者其他的例子

老问题,但这里有一个可能的css解决方案,为其他人遇到它。

https://jsfiddle.net/Hastig/ancwqda3/

html

<div id="navigation">
    <a class="nav n1" href="#"></a>
    <a class="nav n2" href="#"></a>
    <a class="nav n3" href="#"></a>
    <a class="title t0" href="#"></a>
    <a class="title t1" href="#">one</a>
    <a class="title t2" href="#">two</a>
    <a class="title t3" href="#">three</a>
</div>
css

.nav {
    display: inline-block;
    float: left;
    padding-top: 0px;
    width: 92px;
    height: 107px;
    background: url("http://dl.dropbox.com/u/693779/triangle.png") no-repeat 0 0;
}
.nav:hover { 
  background-position: 0 -107px;
}
.title {
    width: 228px;
    height: 107px;
    background: url("http://dl.dropbox.com/u/693779/blank.png") no-repeat 0 0;
    color: red;    font-weight: bold;
    text-decoration: none;
    font-size: 40px;    line-height: 107px;
    text-align: center;
}
.t0 {
    display: inline-block;
}
.t1, .t2, .t3 {
    display: none;
}
.nav:hover ~ .t0 {
    display: none;
}
.n1:hover ~ .t1, .n2:hover ~ .t2, .n3:hover ~ .t3 {
    display: inline-block;
}