jQuery函数在点击时翻转图像

jQuery函数在点击时翻转图像

本文介绍了jQuery函数在点击时翻转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些CSS来翻转图片。它工作正常,但我想把它转换为一个函数,使我可以调用该函数在 onclick 事件。



到目前为止,我可以在这里找到一个演示:



以下是图片的HTML:

 < div class =f1_container> 
< div class =shadow f1_card>
< div class =front face>
< img src =http://media-cdn.tripadvisor.com/media/photo-s/03/48/0b/14/dolphin-view-chalets.jpgstyle =height:281px ; width:450px; />
< / div>
< div class =back face center>这里有一些文字< / div>
< / div>
< / div>

这是CSS:

  .f1_container {
position:relative;
margin:10px;
width:450px;
height:281px;
z-index:1;
float:left;
}
.f1_container {
-webkit-透视:1000;
透视:1000;
}
.f1_card {
width:100%;
height:100%;
-webkit-transform-style:preserve-3d;
-webkit-transition:all 1.0s linear;
transform-style:preserve-3d;
transition:all 1.0s linear;
}
.f1_container:hover .f1_card {
-webkit-transform:rotateY(180deg);
transform:rotateY(180deg);
box-shadow:-5px 5px 5px #aaa;
}
.face {
position:absolute;
width:100%;
height:100%;
backface-visibility:hidden;
-webkit-backface-visibility:hidden;
}
.face.back {
display:block;
-webkit-transform:rotateY(180deg);
transform:rotateY(180deg);
box-sizing:border-box;
color:white;
text-align:center;
background-color:#aaa;
}
body {width:2000px}


解决方案>

将此添加到您当前的解决方案很简单:



CSS



使用以下类别替换:hover 状态:

 。 f1_container:hover .f1_card {} 
/ *变成* /
.f1_container.active .f1_card {}

JavaScript



添加此JavaScript部分,以便在点击时切换新类:

  $('。f1_container')click(function(){
$(this).toggleClass('active');
});

演示




I've written some css to flip an image. It's working fine, but I want to convert it to a function so that I can call that function in an onclick event.

A demo of what I have so far can be found here: http://jsfiddle.net/Spokey/ggUue/1/

Here's the HTML for the image:

<div class="f1_container">
    <div class="shadow f1_card">
        <div class="front face">
            <img src="http://media-cdn.tripadvisor.com/media/photo-s/03/48/0b/14/dolphin-view-chalets.jpg" style="height: 281px; width: 450px;" />
        </div>
        <div class="back face center">Some text inside here</div>
    </div>
</div>

This is the CSS:

.f1_container {
    position: relative;
    margin:10px;
    width: 450px;
    height: 281px;
    z-index : 1;
    float:left;
}
.f1_container {
    -webkit-perspective: 1000;
    perspective: 1000;
}
.f1_card {
    width: 100%;
    height: 100%;
    -webkit-transform-style: preserve-3d;
    -webkit-transition: all 1.0s linear;
    transform-style: preserve-3d;
    transition: all 1.0s linear;
}
.f1_container:hover .f1_card {
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
    box-shadow: -5px 5px 5px #aaa;
}
.face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.face.back {
    display: block;
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
    box-sizing: border-box;
    color: white;
    text-align: center;
    background-color: #aaa;
}
body{width:2000px}
解决方案

It's pretty simple to add this to your current solution:

CSS

Replace the :hover state with a class:

.f1_container:hover .f1_card {}
/* becomes */
.f1_container.active .f1_card {}

JavaScript

Add this JavaScript part that toggles the new class on click:

$('.f1_container').click(function() {
    $(this).toggleClass('active');
});

Demo

Try before buy

这篇关于jQuery函数在点击时翻转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:58