简体   繁体   English

使用css和javascript将div翻转180度

[英]Flip a div 180 degrees using css and javascript

I want to flip a div 180 degrees using a javascript function which triggers a css animation. 我想使用触发css动画的javascript函数将div翻转180度。 My div has got the following declaration: 我的div有以下声明:

.start-photo-thumb
{
    position: relative;
    float: left;
    background: #444444;
    height: 192px;
    width: 192px;
    margin-right: 10px;
    margin-bottom: 10px;
    perspective: 1000px;
    animation: rotating 0.6s linear infinite;
    animation-play-state: paused;
}

@keyframes rotating 
{
    from
    {
        transform: rotateY(0deg);
    }
    to
    {
        transform: rotateY(180deg);
    }
}

I think I'm taking the wrong way with my attempt. 我想我的尝试是错误的。

function flipPhoto(box)
{
    $('#' + box.id).css('animation-play-state', 'running');

    setTimeout(function(){
        $('#' + box.id).css('animation-play-state', 'paused');
    }, 600);
}

In addition I want to change the background-image at the moment you can't see the div (at 90 degrees or at 300ms). 另外我想在你看不到div时(90度或300ms)改变背景图像。

Is there a better and easier way to solve that problem? 有没有更好更简单的方法来解决这个问题? Thanks in advance! 提前致谢!

CSS3 Rotate transformation can be used for flipping any element across x or y axis. CSS3旋转变换可用于在x轴或y轴上翻转任何元素。

CSS: CSS:

#container_2 {
  -webkit-transform: rotate(180deg);
  -moz-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}

to rotate a div on y-axis use negative values 在y轴上旋转div使用负值

  transform: rotateY(-180deg);
  -ms-transform: rotateY(-180deg); /* IE 9 */
  -webkit-transform: rotateY(-180deg); /* Safari and Chrome */

Above Transformation can also be timed to run after predefined interval using CSS3 animation and Keyframes: 还可以使用CSS3动画和关键帧在预定义间隔后运行以上转换:

@keyframes rotating 
{
    from
    {
        transform: rotateY(0deg);
    }
    to
    {
        transform: rotateY(180deg);
    }
 }

 #timed-animation{
    -webkit-animation: rotating 5s infinite; /* Safari 4+ */
    -moz-animation:    rotating 5s infinite; /* Fx 5+ */
    -o-animation:      rotating 5s infinite; /* Opera 12+ */
    animation:         rotating 5s infinite; /* IE 10+, Fx 29+ */
 }

Triggering Rotate transformation on click event using jquery 触发使用jquery在单击事件上旋转转换

jQuery: jQuery的:

$('#foo').click(function() {
  $(this).css('-webkit-transform', 'rotateY(-180deg)');
  $(this).css('-moz-transform', 'rotateY(-180deg)');
  $(this).css('transform', 'rotateY(-180deg)');
});

LIVE DEMO: 现场演示:

Css Way : http://jsfiddle.net/dreamweiver/x6v60t2f/ Css方式: http//jsfiddle.net/dreamweiver/x6v60t2f/

Jquery way : http://jsfiddle.net/dreamweiver/LTNPs/2108/ Jquery方式: http//jsfiddle.net/dreamweiver/LTNPs/2108/

Reference for CSS3 "transform" article: https://css-tricks.com/snippets/css/keyframe-animation-syntax/ 参考CSS3“转换”文章: https//css-tricks.com/snippets/css/keyframe-animation-syntax/

You can do it using only CSS3: 你只能使用CSS3来做到这一点:

div#div-id {
    transform: rotate(180deg);
}

if you want to animate it you can use CSS3 animations 如果你想动画它,你可以使用CSS3 动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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