悬停在一个div中,触发效果悬停在另一个中

时间:2014-10-04 10:37:31

标签: javascript jquery html css hover

我有一个问题:我有一个效果悬停在一个div中,但我想在光标悬停另一个div时触发效果。类似的东西:

<div id='mouse-hover-in-this-div'>
    blablabla
    <div id='should-trigger-mouse-hover-in-this-div'></div>
</div>

我在这里看到一些解决方案,但它们没有用。特别是我试图用jquery .css方法改变内部div的css属性,但是我没有提供以下选项:

-webkit-transform: translateY(16px);
transform: translateY(16px);
-webkit-animation-name: hang;
animation-name: hang;
-webkit-animation-duration: 1.5s;
animation-duration: 1.5s;
-webkit-animation-delay: 0.3s;
animation-delay: 0.3s;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
animation-direction: alternate;

我想要的悬停是来自库hover.css,我只是想在整个div悬停时触发箭头的反弹效果,而不仅仅是小箭头。 我的问题有一个解决方案吗?

6 个答案:

答案 0 :(得分:1)

Demo

#outside-box:hover .animated-div {
  -webkit-transform: translateY(6px);
  transform: translateY(6px);
  -webkit-animation-name: hang;
  animation-name: hang;
  -webkit-animation-duration: 1.5s;
  animation-duration: 1.5s;
  -webkit-animation-delay: 0.3s;
  animation-delay: 0.3s;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  -webkit-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
  -webkit-animation-direction: alternate;
  animation-direction: alternate;
}

答案 1 :(得分:0)

对于html

  <div id='mouse-hover-in-this-div'>
        blablabla
        <div id='should-trigger-mouse-hover-in-this-div'></div>
    </div>

你可以拥有像

这样的CSS
#mouse-hover-in-this-div:hover #should-trigger-mouse-hover-in-this-div{
 //HOver Css here 
}

在这里看到一个工作小提琴http://jsfiddle.net/jdksbg51/

答案 2 :(得分:0)

如果你想要一个保镖效果,你需要添加jQueryUI效果以及jQuery库

答案 3 :(得分:0)

当您将鼠标悬停在#inner-div div上时,可以使用CSS descendant selectors选择#outside-box来实现此目的。

JSFiddle - DEMO

<强> HTML:

<div id='outside-box'>this is some text
    <div id="another-div">
        <div id='inner-div' class='animated-div'>-></div>
    </div>
</div>

<强> CSS:

#another-div {
    display: inline-block;
}
.animated-div {
    display: inline-block;
    -webkit-transition-duration: 0.5s;
    transition-duration: 0.5s;
    -webkit-transition-property: transform;
    transition-property: transform;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
}
#outside-box:hover .animated-div, #outside-box:focus .animated-div, #outside-box:active .animated-div {
    -webkit-transform: translateY(6px);
    transform: translateY(6px);
    -webkit-animation-name: hang;
    animation-name: hang;
    -webkit-animation-duration: 1.5s;
    animation-duration: 1.5s;
    -webkit-animation-delay: 0.3s;
    animation-delay: 0.3s;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
    animation-direction: alternate;
}

答案 4 :(得分:0)

使用jQuery

$('#mouse-hover-in-this-div').hover(function(e) {
$('#should-trigger-mouse-hover-in-this-div').trigger(e.type);
});

DEMO FIDDLE

答案 5 :(得分:0)

这里基于你的小提琴http://jsfiddle.net/a0xx6kqa/4/

当您将鼠标悬停在父元素上时,它将搜索具有.animated-div类的子节点,并且无论您在父节点中有多少其他元素,它都会为该div设置动画:)

.animated-div {
    display: inline-block;
    -webkit-transition-duration: 0.5s;
    transition-duration: 0.5s;
    -webkit-transition-property: transform;
    transition-property: transform;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
}
#outside-box:hover .animated-div {
    -webkit-transform: translateY(6px);
    transform: translateY(6px);
    -webkit-animation-name: hang;
    animation-name: hang;
    -webkit-animation-duration: 1.5s;
    animation-duration: 1.5s;
    -webkit-animation-delay: 0.3s;
    animation-delay: 0.3s;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
    animation-direction: alternate;
}

只需添加#outout-box:hover .animated-div,您可以在其中调用动画而不是其他类

.animated-div:hover,.animated-div:focus,.animated-div:active {}应该只是 #outout-box:hover .animated-div {}