Javascript按钮切换多个css动画

时间:2023-01-28 10:51:30

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.

我目前有一系列带有翻转动画的容器,可以在悬停时激活。我希望他们不仅可以在悬停时激活,还可以在javascript按钮上单击,这将使用单个按钮切换所有。

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:

这些对于我使用的对象重复,并依赖于以下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/

编辑:请求的jsfiddle:https://jsfiddle.net/futbyyef/

1 个解决方案

#1


0  

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

假设它们都有.flip-container类,你可以添加这样的函数。

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

这是一个有效的小提琴。确保在您的页面上包含jQuery。

#1


0  

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

假设它们都有.flip-container类,你可以添加这样的函数。

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

这是一个有效的小提琴。确保在您的页面上包含jQuery。