html - 在 <div> 悬停时更改 <body> 背景图像

标签 html css

假设我在 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");
});

Fiddle here

关于html - 在 <div> 悬停时更改 <body> 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42025348/

相关文章:

html - footer不能放到网站底部

javascript - Chrome 快速内存泄漏。运行 Javascript Profiler 修复泄漏时如何调试?

javascript - 如何将选项添加到 knockout js选项中的第一个?

html - 根据元素数量以不同宽度显示列表元素

javascript - 修复了容器中的右侧边栏,bootstrap

html - 有没有办法让 svg 元素可以在 html 页面上突出显示?

javascript - 增加整个 HTML 页面的大小?

javascript - 无法获取元素动画以等待 jquery 中的另一个元素动画

css - 大型数据集的 jQuery 网格建议?

html - 我需要在滚动条上放置一个 div