首页 > 解决方案 > 如果我悬停父级,如何触发子级?

问题描述

当悬停在父级上时,我想触发子级 div。搜索了许多主题,但没有发现真正有用,除了:class.parent:hover class.child {}我的主题不重复:Css hover child to trigger effect on parent

提供的来源:https ://jsfiddle.net/Arty_Prof/81e4uy3w/

html {
  background: black;
}

#buttonStart {
  position: absolute;
  width: 15%;
  height: 10%;
  bottom: 15%;
  left: 5%;
}

#imageStart {
  width: 100%;
  height: 100%;
}

#buttonStart:hover .hovicon.effect-3.sub-b {
  cursor: pointer;
}
.hovicon {
    display: inline-block;
    font-size: 45px;
    line-height: 90px;
    cursor: pointer;
    width: 90px;
    height: 90px;
    border-radius: 50%;
    text-align: center;
    position: relative;
    text-decoration: none;
    z-index: 1;
    color: #fff;
}
.hovicon.auto-width {
    width: auto;
    height: auto;
}
.hovicon:after {
    pointer-events: none;
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    content:'';
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}
.hovicon:before {
    speak: none;
    font-size: 48px;
    line-height: 90px;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    display: block;
    -webkit-font-smoothing: antialiased;
}
.hovicon.effect-3 {
	position: absolute;
	bottom:-13%;
	left: 62%;
	font-size: 3.8em;
	-webkit-transition: color 0.3s;
    -moz-transition: color 0.3s;
    transition: color 0.3s;
}
.hovicon.effect-3:after {
    top: -2px;
    left: -2px;
    padding: 2px;
    z-index: -1;
    background: #fff;
    -webkit-transition: -webkit-transform 0.2s, opacity 0.3s;
    -moz-transition: -moz-transform 0.2s, opacity 0.3s;
    transition: transform 0.2s, opacity 0.3s;
}
/* Effect 3b */
.hovicon.effect-3.sub-b, .hovicon.effect-3.sub-b i {
    color: #fff;
}
.hovicon.effect-3.sub-b:hover, .hovicon.effect-3.sub-b:hover i {
	color: #696969;
}
.hovicon.effect-3.sub-b:after {
    -webkit-transform: scale(1.3);
    -moz-transform: scale(1.3);
    -ms-transform: scale(1.3);
    transform: scale(1.3);
    opacity: 0;
}
.hovicon.effect-3.sub-b:hover:after {
    -webkit-transform: scale(1);
    -moz-transform: scale(1);
    -ms-transform: scale(1);
    transform: scale(1);
    opacity: 1;
}
<div id="buttonStart">
  <div><input type="image" id="imageStart" src="https://cdn.pixabay.com/photo/2017/10/10/07/48/beach-2836300_960_720.jpg">
    <div class="hovicon effect-3 sub-b" id="buttonStartEffect">&nbsp &nbsp</div>
  </div>
</div>

小提琴中的其他代码。

我希望在悬停父 div 时看到白色圆圈(在我的例子中是一张图片)。

标签: htmlcsscss-transitions

解决方案


你的小提琴几乎是正确的。我刚刚更改了最后一个选择器,如下所示,它在父 div 悬停时显示圆形动画。请尝试下面的css

html {
  background: black;
}

#buttonStart {
  position: absolute;
  width: 15%;
  height: 10%;
  bottom: 15%;
  left: 5%;
}

#imageStart {
  width: 100%;
  height: 100%;
}

#buttonStart:hover .hovicon.effect-3.sub-b {
  cursor: pointer;
}

/* Effect 3 */

.hovicon {
  display: inline-block;
  font-size: 45px;
  line-height: 90px;
  cursor: pointer;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  text-align: center;
  position: relative;
  text-decoration: none;
  z-index: 1;
  color: #fff;
}

.hovicon.auto-width {
  width: auto;
  height: auto;
}

.hovicon:after {
  pointer-events: none;
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  content: '';
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
}

.hovicon:before {
  speak: none;
  font-size: 48px;
  line-height: 90px;
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-transform: none;
  display: block;
  -webkit-font-smoothing: antialiased;
}

.hovicon.effect-3 {
  position: absolute;
  bottom: -13%;
  left: 62%;
  font-size: 3.8em;
  -webkit-transition: color 0.3s;
  -moz-transition: color 0.3s;
  transition: color 0.3s;
}

.hovicon.effect-3:after {
  top: -2px;
  left: -2px;
  padding: 2px;
  z-index: -1;
  background: #fff;
  -webkit-transition: -webkit-transform 0.2s, opacity 0.3s;
  -moz-transition: -moz-transform 0.2s, opacity 0.3s;
  transition: transform 0.2s, opacity 0.3s;
}

/* Effect 3b */

.hovicon.effect-3.sub-b,
.hovicon.effect-3.sub-b i {
  color: #fff;
}

.hovicon.effect-3.sub-b:hover,
.hovicon.effect-3.sub-b:hover i {
  color: #696969;
}

.hovicon.effect-3.sub-b:after {
  -webkit-transform: scale(1.3);
  -moz-transform: scale(1.3);
  -ms-transform: scale(1.3);
  transform: scale(1.3);
  opacity: 0;
}

#buttonStart:hover .hovicon.effect-3.sub-b:after {
  -webkit-transform: scale(1);
  -moz-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  opacity: 1;
}

推荐阅读