html - 在悬停另一个元素时更改元素的属性

标签 html css

我有两个不同的 div,悬停在一个 div 上时,我试图更改此 div + 另一个 div 的属性(框阴影)。即在下面的例子中,div one-two 悬停时,one-two 和 one-one 都应该添加 box-shadow。

我在 stackoverflow 中找到了一些答案,上面写着 .div1:hover + .div2,但这似乎不起作用。 (Is there any way to hover over one element and affect a different element?)。还有其他解决方案还是我做错了什么?

@import url(https://fonts.googleapis.com/css?family=Open+Sans:700,300);
.container {
  display: block;
  max-width: 100%;
}
.header {
  width: 100%;
  position: absolute;;
  /*border-bottom: 1px solid black;*/
  height: 10%;
  background-color: teal;
  margin: -9px;
  padding-top: 15px;
  box-shadow: 2px 5px 4px #ccc ;
  text-align: center;
  font-size: 20px;
  text-transform: uppercase;
  font-family: sans-serif;
  font: Helvetica;
  color: #ccc

}
.canvas-frame {
  position: absolute;
  left: 30%;
  top: 20%;
  width: 600px;
  height: 400px;
  background: linear-gradient(to left bottom, #2DBE60, #246DE9);
  box-shadow: 3px 3px 5px 2px #ccc;
}
.frame {
  position: absolute;
  left: 30%;
  top: 20%;
  width: 600px;
  height: 400px;
  background: linear-gradient(to left bottom, #2DBE60, #246DE9);
  box-shadow: 3px 3px 5px 2px #ccc;
}
.one-one {
   position: absolute;
   z-index: 0;
   top: 0;
   left: 36px;
   height: 40px;
   width: 20px;
   background: #fff;
  margin-top:30px;
   border-radius: 3px;
   transform: rotate(50deg);
   box-shadow: 0 0 13px 0 rgba(0,0,0,0.2);
 }
 .one-two {
   position: absolute;
   background: #fff;
   height: 100px;
   width: 20px;
   border-radius: 3px;
   margin-top : -20px;
   top: 51px;
   left: 51px;
   z-index: 1;
   box-shadow: 0 0 13px 0 rgba(0,0,0,0.2);
 }
 .zero-one,.zero-two {
   position: absolute;
   z-index: 2;
   top: 50;
   left: 62px ;
   box-sizing: border-box;
   height: 100px;
   width: 100px;
   margin-top : 30px;
   border-radius: 50%;
   border: 24px solid #fff;
   box-shadow: 0 0 13px 0 rgba(0,0,0,0.2);
 }
 .zero-two {
   left: 152px !important;
 }
.overlap-text {
position: absolute;
z-index: 1;
}
.big{
  font-family: sans-serif;
  font: Helvetica;
  font-size: 80px;
  color: #fff;
  text-transform: capitalize;
}
.days{
  position: absolute;
  top: 90px;
  left: 140px;
}
.one-two:hover + .one-one{
  box-shadow: 5px 5px 8px 0 #545050;
  cursor: -webkit-grab;
}
.one-two:active{
  box-shadow: 6px 6px 4px 0 #545050;
  cursor: -webkit-grab;
}
.one-two:hover + .one-one{
  box-shadow: 5px 5px 8px 0 #545050;
  cursor: -webkit-grab;
}
.one-two:active{
  box-shadow: 6px 6px 4px 0 #545050;
  cursor: -webkit-grab;
}
<html>
<title>
100 days Challenge
</title>
<link rel="stylesheet" href="style.css">
<body>
  <div class="container">
  <nav class="header"> css challenge </nav>
  <div class="frame">
  <!-- <canvas class="canvas-frame"> -->
  <!-- <h1 class="overlap-text"> 100 </h1> -->
  <div class="one-one" id="one-one"></div>
  <div class="one-two" id="one-two"></div>
  <div class="zero-one"></div>
  <div class="zero-two"></div>
  <div class="days"><h1 class="big"> days </h1></div>

  <!-- <h2> css challenge </h2> -->
</div>
<!-- </canvas> -->
</div>
</body>
</html>

最佳答案

是的,所以你想使用相邻的选择器 +

.one-one:hover + .one-two { box-shadow: 5px 5px 8px 0 #545050; }

以为你做错了顺序就完了

查看这个简单的代码笔示例:http://codepen.io/StefanBobrowski/pen/ZeZGmQ

关于html - 在悬停另一个元素时更改元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43216520/

相关文章:

javascript - 如何添加淡入淡出效果

javascript - 为每个级别的 JS 元素设置样式

css - 如何设置所选选择输入的占位符颜色?

html - 如何在一个 div(nav) 中有不同的对齐方式?

javascript - 在执行过程中动态自动缩放 Phaser 3 游戏

php - HTML 表单不会发布数据($_POST[] 为空)

javascript - 如何刷新 div 以从数据库获取新传入的数据

css - SASS 函数迭代 nth-child(n+1) 和 lighten/darken($color, 5n%)

WordPress 插件的 CSS 显示在某些浏览器上,而不是其他浏览器

css - 具有 3 个随机大小列和底部的 div 模板