简体   繁体   中英

Javascript button to toggle multiple css animations

I currently have a series of containers with a flip animation that activates on hover. I would like for them to not only activate on hover, but also on a javascript button click that would use a single button to toggle flip all.

HTML:

<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
            <div class="flipper">
                <div class="front">
                    <a href="link1.html">
                        <img src="images/frontimg1.jpg"/>
                    </a>
                </div>
                <div class="back">
                    <div class="fliplinks">
                        <a href="link1.html">
                            <p>text1</p>
                        </a>
                    </div>
                </div>
            </div>
        </div>

These are repeated for as many objects as I use and rely on the following CSS:

.flip-container {
    float:left;
    margin: 7.5px;
}
/* entire container, keeps perspective */
.flip-container {
    perspective: 1000px;
}
/* flip the pane when hovered */
.flip-container:hover .flipper, .flip-container.hover .flipper {
    transform: rotateY(180deg);
}

.flip-container, .front, .back {
    width: 210px;
    height: 210px;
}

/* flip speed goes here */
.flipper {
    transition: 0.6s;
    transform-style: preserve-3d;

    position: relative;
}

/* hide back of pane during swap */
.front, .back {
    backface-visibility: hidden;

    position: absolute;
    top: 0;
    left: 0;
}

/* front pane, placed above back */
.front {
    z-index: 2;
    /* for firefox 31 */
    transform: rotateY(0deg);
}

/* back, initially hidden pane */
.back {
    transform: rotateY(180deg);
}

Edit: A jsfiddle as requested: https://jsfiddle.net/futbyyef/

Assuming that they all have the .flip-container class, you could add a function like this.

<script>
    $('.toggle-button').click(function(){
        $('.flip-container').toggleClass('hover');
    }
</script>

Then you just need to include a button to toggle it. Something like

<button class="toggle-button">Test Button</button>

Here is a fiddle that works. Make sure to include jQuery on your page.

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