将鼠标悬停在一个div上,它会影响另一个div

时间:2013-08-12 17:50:03

标签: css3 animation hover

所以我想将鼠标悬停在一个Box上并让它激活另一个具有缓动效果的div。您可以在下面看到.images{}有0s无限滚动,然后.box:hover > .images{}是我将0更改为10s以开始幻灯片放映。

HTML

<div class="slideshow">
     <div class="images"></div>
     <div class="box"></div>   
</div>

CSS:

.slideshow {
  position: relative;
  overflow: hidden;
  height: 220px;
  width: 100%;
}
.box {
width:100px;
height:100px;
position: absolute;
left: 0;
top: 0;
background-color: #333;
}

.images {
  background: url('http://jamesebeling.com/testing/jamesebeling/wp-content/uploads/2013/08/buildings.png');
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 300%;
 -webkit-transition: all 1s ease-out;
  -moz-transition: all 1s ease-out;
  -o-transition: all 1s ease-out;
  transition: all 1s ease-out;
  -webkit-animation: slideshow 0s linear infinite;
  -moz-animation:    slideshow 0s linear infinite;
}
@-webkit-keyframes slideshow {
  0%    { left: 0; }
  100%  { left: -200%; }
}
@moz-keyframes slideshow {
  0%    { left: 0; }
  100%  { left: -200%; }
}
/* Hey browser, use your GPU */
   -webkit-transform: translate3d(0, 0, 0);
}

@-webkit-keyframes moveSlideshow {
    0%   { 
        -webkit-transform: translateX(0);  
    }    
    100% { 
        -webkit-transform: translateX(-200%);  
    }
}
@-moz-keyframes moveSlideshow {
    0%   { 
        -moz-transform:    translateX(0); 
    }    
    100% { 
        -moz-transform:    translateX(-200%); 
    }
}

.box:hover > .images {
.images {
  background: url('http://jamesebeling.com/testing/jamesebeling/wp-content/uploads/2013/08/buildings.png');
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 300%;
 -webkit-transition: all 1s ease-out;
  -moz-transition: all 1s ease-out;
  -o-transition: all 1s ease-out;
  transition: all 1s ease-out;
  -webkit-animation: slideshow 10s linear infinite;
  -moz-animation:    slideshow 10s linear infinite;
}
@-webkit-keyframes slideshow {
  0%    { left: 0; }
  100%  { left: -200%; }
}
@moz-keyframes slideshow {
  0%    { left: 0; }
  100%  { left: -200%; }
}
/* Hey browser, use your GPU */
   -webkit-transform: translate3d(0, 0, 0);
}

@-webkit-keyframes moveSlideshow {
    0%   { 
        -webkit-transform: translateX(0);  
    }    
    100% { 
        -webkit-transform: translateX(-200%);  
    }
}
@-moz-keyframes moveSlideshow {
    0%   { 
        -moz-transform:    translateX(0); 
    }    
    100% { 
        -moz-transform:    translateX(-200%); 
    }
}

1 个答案:

答案 0 :(得分:1)

如果您更改HTML以在box课程之前加入images课程,则可以使用adjacent selector选择.images,前面是.box:hover }}:

.box:hover + .images { ... }

Working demo

我还将z-index: 1添加到.box,因此它位于图片元素的顶部。