简体   繁体   中英

hover in one div that trigger the effect hover in another

I have a problem: I have an effect hover in one div, but I would like to trigger the effect when the cursor is hover another div. Something like:

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

I see some solutions here but they did not work. In particular I tried to change the css property of the inner div with the jquery .css method but I did not succed in giving the following option:

-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;

The hover I want to assing is from the library hover.css, I simply want that a bounce effect of an arrow is triggered when the whole div is hover, not just the little arrow. There is a solution to my problem?

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;
}

For html

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

you can have css like

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

see a working fiddle here http://jsfiddle.net/jdksbg51/

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

You could achieve this by using CSS descendant selectors to select #inner-div when you hover on #outside-box div.

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;
}

Using jQuery

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

DEMO FIDDLE

Here you are based on your fiddle http://jsfiddle.net/a0xx6kqa/4/

When you hover over the parent element it will search for the child with class .animated-div and will animate that div no matter how many other elements you have in the parent :)

.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;
}

Just add #outside-box:hover .animated-div where you call the animations instead of the rest of the classes

.animated-div:hover, .animated-div:focus, .animated-div:active {} should just be #outside-box:hover .animated-div {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM