更改div属性悬停在另一个div上

时间:2017-05-15 07:45:23

标签: html css css3

我知道这个问题已被多次询问过,但是我的网页中没有提出任何解决方案。 我只使用HTML和CSS3,因为我没有时间学习JS或jQuery并添加它。 我左边有一个侧边栏,有一堆图标。当我将这些图标悬停时,右侧会出现一个弹出窗口。在弹出窗口下我有我的主要内容。现在,当我将鼠标悬停在图标上时,我想降低内容的不透明度。 代码就是这样(某些部分缺失):



/* INIT REGION: sidebar menu customization */
#menu {
	position: fixed;
	top: 132px;
	height: 100%;
	width: 64px;
	background-color: rgb(25, 151, 156);
	/*padding-left: 10px;*/
	color: rgb(0, 0, 63);
	display: block;
}
#menu a {
	display: block;
	text-decoration: none;
	color: rgb(0, 0, 63);
	background-color: rgb(25, 151, 156);
	transition: background-color 0.5s ease, border-left 0.5s linear;
	border-left: 0px groove rgb(255, 140, 64);
	position: relative;
	display: block;
	z-index: -1;
}
#menu a:hover {
	border-left: 6px groove rgb(255, 140, 64);
	background-color: rgb(255, 140, 64);
	z-index: 0;
}
#menu a .hinttext {
	display: table;
	/*visibility: hidden;*/
	width: 120px;
	height: 52px;
	background-color: rgb(255, 140, 64);
	color: white;
	padding: 3px 7px;
	position: absolute;
	top: 0;
	z-index: -1;
	left: -200%;
	border-top-right-radius: 2px;
	border-bottom-right-radius: 2px;
	opacity: 0;
	transition: opacity 0.75s ease, left 1s ease;
}
#menu a:hover ~ .content {
	z-index: -2;
	color: rgba(0, 0, 63, 0.3);
}
#menu a:hover .hinttext {
	visibility: visible;
	background-color: rgb(255, 140, 64);
	z-index: 0;
	opacity: 1;
	left: 100%;
}
/* END REGION */

/* INIT REGION: main content customization */
.content {
	position: relative;
	margin-left: 64px;
	margin-top: 120px;
	text-align: justify;
	padding-top: 16px;
	padding-left: 24px;
	padding-right: 24px;
	width: 80%;
	overflow: auto;
	z-index: -1;
	color: rgba(0, 0, 63, 1);
}
/* END REGION */

<div id="menu">
  <a href="index.html" class="active hint">
    <img src="../media/home-icon.svg">
    <span class="hinttext"><p>Home page</p></span>
	</a>
  [other icons]
</div>

<div class="content">
  <h2>Hello World</h2>
  <p>Have a nice day</p>
</div>
&#13;
&#13;
&#13;

嗯,#menu a:hover ~ .content部分根本不起作用。 我在那里错过了什么?

2 个答案:

答案 0 :(得分:0)

添加#menu:hover代替#menu a:hover,因为.content前面只有div#menu ~仅代表兄弟姐妹DOM元素,而不是指代选择后的所有html(就像您想到#menu a:hover ~ .content)。

&#13;
&#13;
/* INIT REGION: sidebar menu customization */
#menu {
	position: fixed;
	top: 132px;
	height: 100%;
	width: 64px;
	background-color: rgb(25, 151, 156);
	/*padding-left: 10px;*/
	color: rgb(0, 0, 63);
	display: block;
}
#menu a {
	display: block;
	text-decoration: none;
	color: rgb(0, 0, 63);
	background-color: rgb(25, 151, 156);
	transition: background-color 0.5s ease, border-left 0.5s linear;
	border-left: 0px groove rgb(255, 140, 64);
	position: relative;
	display: block;
	z-index: -1;
}
#menu a:hover {
	border-left: 6px groove rgb(255, 140, 64);
	background-color: rgb(255, 140, 64);
	z-index: 0;
}
#menu a .hinttext {
	display: table;
	/*visibility: hidden;*/
	width: 120px;
	height: 52px;
	background-color: rgb(255, 140, 64);
	color: white;
	padding: 3px 7px;
	position: absolute;
	top: 0;
	z-index: -1;
	left: -200%;
	border-top-right-radius: 2px;
	border-bottom-right-radius: 2px;
	opacity: 0;
	transition: opacity 0.75s ease, left 1s ease;
}
#menu:hover ~ .content {
	z-index: -2;
	color: rgba(0, 0, 63, 0.3);
}
#menu:hover .hinttext {
	visibility: visible;
	background-color: rgb(255, 140, 64);
	z-index: 0;
	opacity: 1;
	left: 100%;
}
/* END REGION */

/* INIT REGION: main content customization */
.content {
	position: relative;
	margin-left: 64px;
	margin-top: 120px;
	text-align: justify;
	padding-top: 16px;
	padding-left: 24px;
	padding-right: 24px;
	width: 80%;
	overflow: auto;
	z-index: -1;
	color: rgba(0, 0, 63, 1);
}
/* END REGION */
&#13;
<div id="menu">
  <a href="index.html" class="active hint">
    <img src="../media/home-icon.svg">
    <span class="hinttext"><p>Home page</p></span>
	</a>
  [other icons]
</div>

<div class="content">
  <h2>Hello World</h2>
  <p>Have a nice day</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
/* INIT REGION: sidebar menu customization */
#menu {
	position: fixed;
	top: 132px;
	height: 100%;
	width: 64px;
	background-color: rgb(25, 151, 156);
	/*padding-left: 10px;*/
	color: rgb(0, 0, 63);
	display: block;
}
#menu a {
	display: block;
	text-decoration: none;
	color: rgb(0, 0, 63);
	background-color: rgb(25, 151, 156);
	transition: background-color 0.5s ease, border-left 0.5s linear;
	border-left: 0px groove rgb(255, 140, 64);
	position: relative;
	display: block;
	z-index: -1;
}
#menu a:hover {
	border-left: 6px groove rgb(255, 140, 64);
	background-color: rgb(255, 140, 64);
	z-index: 0;
}
#menu a .hinttext {
	display: table;
	/*visibility: hidden;*/
	width: 120px;
	height: 52px;
	background-color: rgb(255, 140, 64);
	color: white;
	padding: 3px 7px;
	position: absolute;
	top: 0;
	z-index: -1;
	left: -200%;
	border-top-right-radius: 2px;
	border-bottom-right-radius: 2px;
	opacity: 0;
	transition: opacity 0.75s ease, left 1s ease;
}

/*
#menu a:hover ~ .content {
	z-index: -2;
	color: rgba(0, 0, 63, 0.3);
}
*/

#menu:hover ~ .content {
  z-index: -2;
	color: rgba(0, 0, 63, 0.3);
  transition: all 10ms;
}

#menu a:hover .hinttext {
	visibility: visible;
	background-color: rgb(255, 140, 64);
	z-index: 0;
	opacity: 1;
	left: 100%;
}
/* END REGION */

/* INIT REGION: main content customization */
.content {
	position: relative;
	margin-left: 64px;
	margin-top: 120px;
	text-align: justify;
	padding-top: 16px;
	padding-left: 24px;
	padding-right: 24px;
	width: 80%;
	overflow: auto;
	z-index: -1;
	color: rgba(0, 0, 63, 1);
}
/* END REGION */
&#13;
<div id="menu">
  <a href="index.html" class="active hint">
    <img src="../media/home-icon.svg">
    <span class="hinttext"><p>Home page</p></span>
	</a>
  [other icons]
</div>

<div class="content">
  <h2>Hello World</h2>
  <p>Have a nice day</p>
</div>
&#13;
&#13;
&#13;