假设我在 body 标签上有一个黑色/白色的背景图像,我希望它在悬停在 div id="上时切换到城市天际线的 background-image 图像1”,以及悬停在 div id="2"上时同一天际线的背景图像。我该怎么做?
我已经为此编写了一些 html/css,以及“不错的”python 知识,但没有使用脚本语言(如 javascript)的经验。我不想复制大段代码,因为我是新手并且想学习。
这是我目前所拥有的:
/*css*/
#bg{background:url('a');
}
#header{
position:absolute;
border:double 30pt;
width:95%;
height:15%;
}
#header img{height:120px;
}
#header img:hover{
transform: rotate(-10deg);
}
#allies{
border:double 30pt;
position:relative;
width:45%;
top:250px;
}
#allies:hover ~ #bg{background:url('b');
}
#enemies{
border:double 30pt;
position:relative;
width:45%;
/*top:55px;*/
top:-67px;
left:967px;
}
#enemies:hover ~ #bg{background:url('c')
}
div img{
width:120px;
height:100px;
display:block;
margin-left:auto;
margin-right:auto;
padding-top:10px;
padding-bottom:5px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AE</title>
<link href="AE.css" rel="stylesheet" type="text/css">
</head>
<body id="bg">
<div id="header">
<a href="index.htm">
<img src="http://www.iconsdb.com/icons/preview/black/home-5-xxl.png">
</a>
</div>
<div id="allies">
<img src="http://fontmeme.com/permalink/170203/949c0c2c2c0c34c044b9b45585717a70.png">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div id="enemies">
<img src="http://fontmeme.com/permalink/170203/e8d4db59e876e5c94bd8f072137e33e2.png">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>
最佳答案
我得到了你想要的,使用 Jquery :
J查询:
$( "#allies" ).hover(function() {
$('body').css("background", "url(https://cdn.pixabay.com/photo/2013/04/14/20/24/water-103817_960_720.jpg");
});
$( "#enemies" ).hover(function() {
$('body').css("background", "url(http://weknowyourdreams.com/images/fire/fire-02.jpg");
});
关于html - 在 <div> 悬停时更改 <body> 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42025348/