如何使用 Angular 2 动画实现翻转效果?

2024-05-14

我一直在我的项目中使用纯CSS翻转卡片,但这个解决方案不是合适的。有人可以通过点击按钮来呈现角度 2 的翻转吗?我在 angularjs 中找到了一个https://codepen.io/Zbeyer/pen/oXQrZg https://codepen.io/Zbeyer/pen/oXQrZg

    <div ng-app="cardFlipper" ng-controller="AppController">    
    <h1>Card Flipping AngularJS</h1>
    <div class="flip-container">
        <div class="flipper" ng-click="flipCard()" ng-class="{'flipToFront':isCardRevealed, 'flipToBack':!isCardRevealed}">
            <div class="back" ng-class="{'face-hidden':hideBackFace}">
            </div>
            <div class="front" ng-class="{'face-hidden':!hideBackFace}">
                <h1>{{currentCard.title | uppercase}}</h1>
                <p ng-if="currentCard.icon">{{currentCard.icon}}</p>
                <br ng-if="currentCard.icon" />
                <img ng-if="currentCard.imageUrl" src="{{currentCard.imageUrl}}" alt="currentCard.imageAlt" />
                <p>{{currentCard.description}}</p>
            </div>
        </div>
    </div>

    <footer>
        <div ng-if="currentCard && currentCard.source">
            <a ng-href="{{currentCard.source}}" target="_blank">Source</a>
        </div>
    </footer>
    <br />
    <br />


</div>
<style>
    /* CARD DIMENSIONS */
@width: 19em;
@height: 27em;
@shadow:1em 1em 2em #111111;
/* MIXINS */
.box-shadow (@string:@shadow) {
    -webkit-box-shadow: @string;
    -moz-box-shadow:    @string;
    box-shadow:         @string;
}


.box-sizing(@sizing:border-box) {
    -webkit-box-sizing: @sizing;
    -moz-box-sizing: @sizing;
    box-sizing: @sizing;
}

.border-radius (@radius: 0.5em) {
    -webkit-border-radius: @radius;
    -moz-border-radius: @radius;
    border-radius: @radius;
}

.top-perspective() {
        /*perspective*/
    -webkit-perspective:1000;
       -moz-perspective:1000;
        -ms-perspective:1000;
         -o-perspective:1000;
            perspective:1000;


}

 .flipped-transform-back () {
    /*transform*/
    -webkit-transform:rotateY(180deg);
       -moz-transform:rotateY(180deg);
        -ms-transform:rotateY(180deg);
         -o-transform:rotateY(180deg);
            transform:rotateY(180deg);
}

.flipped-transform-front {
     -webkit-transform: rotateY(0deg);
        -moz-transform: rotateY(0deg);
          -o-transform: rotateY(0deg);
         -ms-transform: rotateY(0deg);
             transform: rotateY(0deg);
}

.flipper-transform(@transition: 0.4s) {
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    -o-transform-style: preserve-3d;
    -ms-transform-style: preserve-3d;
    transform-style: preserve-3d;

    -webkit-transition: @transition;
    -moz-transition: @transition;
    -o-transition: @transition;
    -ms-transition: @transition;
    transition: @transition;
}

.back-face-should-be-hidden() {

    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -ms-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* STYLE SHEET */

body {
    font-family: "myriad-pro", sans-serif;
    font-size:100%;
    text-align:center;

    color:#D1D1D1;

    padding: 0;
    background-color:#333333;

    margin:0 auto;
    padding: 0;
}

footer {
    text-align:center;
    padding:1em;
    a {
        font-size:1em;
        color:RGB(255, 208, 128);
    }
}


.flip-container {
    display:block;
    margin:0 auto;
    width: @width * 1.0;
    height: @height *1.0;
}   

.flipToFront {
    .flipped-transform-front();
}

.flipToBack {
    .flipped-transform-back();
}

/* flip speed goes here */
.flipper {
    .top-perspective();
    .flipper-transform();
    width:100%;
    height:100%;
    position:relative;
}

.flip-container, .front, .back {
    .back-face-should-be-hidden();

    text-align:center;
    color:#333333;        
    padding: 0.25em;

    h1, h2, h3, a {
        font-size: 1.25em;
    }
}

.face-hidden {

    /*
        .face-should-be-hidden();
    */
}

.front, .back {
    /* hide back of pane during swap */
    overflow:scroll;

    display:inline-block;

    /* Card overlay eachother */
    position: absolute;
    top: 0;
    left: 0;

    /* Make Pretty */

    .box-sizing();
    .border-radius();    
    .box-shadow();
    width:100%;
    height:100%;
}

/* front pane, placed above back */
.front {
    text-align:center;
    z-index: 2;
    background-color:#FEFEFE;
    .flipped-transform-front ();    
    .box-sizing();
     border:0.5em solid #FEFEFE;
    img {
        width:100%;
        margin: 0 auto;
        height:auto;
        .border-radius();
    }
}

/* back, initially hidden pane */
.back {
    background-color:#EBEBEB;
    .flipped-transform-back ();

    background-image: url('http://subtlepatterns.com/patterns/upfeathers.png');
    background-position: center;
    background-repeat: repeat;
    .box-sizing();
     border:1em solid #FEFEFE;


}

/* Media Queries */

/*
@highdensity: ~"only screen and (-webkit-min-device-pixel-ratio: 1.5)",
              ~"only screen and (min--moz-device-pixel-ratio: 1.5)",
              ~"only screen and (-o-min-device-pixel-ratio: 3/2)",
              ~"only screen and (min-device-pixel-ratio: 1.5)";
*/

@mobile:      ~"only screen and (max-width: 34em)";
@tablet:      ~"only screen and (min-width: 34em) and (max-width: 55em)";
@desktop:     ~"only screen and (min-width: 55em)";


@media @mobile {
    h1, h2, h3 {
        font-size: 1.25em;
    }

    .flip-container, .front, .back {
        width: @width * 1.0;
        height: @height *1.0;
    }
}
/*
@media @tablet {
        .flip-container, .front, .back {
        width: @width * 1.25;
        height: @height *1.25;

        h1, h2, h3 {
            font-size: 1.75em;
        }
    }
}

@media @desktop {
    .flip-container, .front, .back {
        width: @width * 1.5;
        height: @height *1.5;

    }
}*/

<script>
    angular.module('cardFlipper', [])
.controller('AppController', ['$scope', '$interval', function($scope, $interval) {

    $scope.cards = [
    {
        title: "escheresque-dark",
        icon:"",
        imageUrl:"http://subtlepatterns.com/patterns/escheresque_ste.png",
        description:"Sublte Pattern Source image below...",
        source: "http://subtlepatterns.com/escheresque-dark/"
    },
    {
        title: "dark sharp edges",
        icon:"",
        imageUrl:"http://subtlepatterns.com/patterns/footer_lodyas.png",
        description:"Sublte Pattern Source image below...",
        source: "http://subtlepatterns.com/dark-sharp-edges/"
    },
    {
        title: "Grey Washed Wall",
        icon:"",
        imageUrl:"http://subtlepatterns.com/patterns/grey_wash_wall.png",
        description:"Sublte Pattern Source image below...",
        source: "http://subtlepatterns.com/grey-washed-wall/"
    }
];
    $scope.currentCard = {};

    $scope.isCardRevealed = false;
    $scope.flipCard = function() {
        $scope.isCardRevealed = !$scope.isCardRevealed;
        if($scope.isCardRevealed) {
            $scope.generateCard();
        } else {

            $scope.currentCard = {};
            /*            setTimeout(function() {
//                $scope.isBackHidden = !$scope.isCardRevealed;
            }, 0.1 * 1000);
*/            
        }
        /*



        */
    }

    $scope.generateCard = function() {
        $scope.currentCard = {};
        var index = Math.floor((Math.random() * $scope.cards.length) + 0);
         $scope.currentCard = $scope.cards[index];
    }

}]);
</script>

有人可以把它变成 angular2 或实现不同的东西吗?


这个演示使用 Angular 中的动画

import { Component, OnInit, trigger, state, style, transition, animate } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <div class="tp-wrapper">
      <div class="tp-box" (click)="toggleFlip()" [@flipState]="flip">
        <div class="tp-box__side tp-box__front">Front
        </div>
        <div class="tp-box__side tp-box__back">Back
        </div>
      </div>
    </div>
  `,
  styles: [
    `
    .tp-wrapper {
      perspective: 800px;
    }

    .tp-box {
      position: relative;
      width: 200px;
      height: 100px;
      margin: 3rem auto;
      transform-style: preserve-3d;
      transition: transform 1s;
    }
    .tp-box__side {
      width: 100%;
      height: 100%;
      position: absolute;
      backface-visibility: hidden;
      color: #fff;
      text-align: center;
      line-height: 100px;
      font-size: 24px;
      font-weight: 700;
      cursor: pointer;
      user-select: none;
    }
    .tp-box__front {
      background: #f30d36;
    }
    .tp-box__back {
      background: #23262d;
      transform: rotateY(179.9deg);
    }

    `
  ],
  animations: [
    trigger('flipState', [
      state('active', style({
        transform: 'rotateY(179.9deg)'
      })),
      state('inactive', style({
        transform: 'rotateY(0)'
      })),
      transition('active => inactive', animate('500ms ease-out')),
      transition('inactive => active', animate('500ms ease-in'))
    ])  
  ]
})
export class AppComponent {

  flip: string = 'inactive';
  constructor() {}

  toggleFlip() {
    this.flip = (this.flip == 'inactive') ? 'active' : 'inactive';
  }

}

在线演示:https://plnkr.co/edit/RZ1v9M?p=preview https://plnkr.co/edit/RZ1v9M?p=preview

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Angular 2 动画实现翻转效果? 的相关文章

随机推荐

  • C++ 类成员函数和来自 C API 的回调

    我正在尝试学习如何称呼它write data 函数从funmain 类中的函数如下面的代码所示 我知道如果我只列出这两个函数而不将其放入类中 该程序就可以工作 curl easy setopt curl CURLOPT WRITEFUNCT
  • Excel 工作表到 iPhone 数据 -- A 点到 B 点

    尽可能简单 我有一个非常简单的 Excel 电子表格 只有 1000 多条记录 我想将其用作 iPhone 应用程序的静态数据源 最好的进攻计划是什么 我心中的可能性 1 直接读取XLS作为数据源 是否有Obj C库用于此 2 将XLS 转
  • 如何从 Javascript/Typescript 中的数组对象计算运行总计并使用 HTML 在每个实例上显示输出?

    我正在开发一个 MEAN 堆栈项目 并且有一个如下所示的数组 savings any 300 450 350 500 我还有一个名为 saving bf 的变量 它是从数据库中检索的结转储蓄 其值如下 savings bf 15000 我想
  • 本地化水晶报表

    在 Crystal Reports 上本地化标签 列标题 字段标签 页眉 页脚等 的好方法是什么 我们目前使用的是 XI R2 SP4 但希望迁移到 2008 年 看起来 2008 年提供了更好的查看器 UI 本地化 它有内容本地化的故事吗
  • 如何使用内联 SQL 参数化 IN 语句的集合? [复制]

    这个问题在这里已经有答案了 可能的重复 参数化 SQL IN 子句 https stackoverflow com questions 337704 parameterizing a sql in clause 你好 我有一个查询 如下所示
  • 如何使用 Excel.UriLink.16 更改 Excel URL 的文件关联?

    我正在尝试更改文件关联 以便在另一个浏览器中打开 Excel 单元格中的 URL 根据使用 CMD ftype命令与Excel UriLink 16 我应该能够使用以下命令从 powershell 通过 cmd 执行此操作 To chang
  • Vista幻影目录

    我们有一个程序 安装程序会检查配置文件是否存在 如果存在 它不会复制该文件 它假设用户已修改其配置文件并希望保留这些修改 不幸的是 这是一个 Vista 之前的应用程序 它将配置文件保存在 Program Files 中 问题是 如果你在重
  • 如何使 Redis 缓存中数据层次结构(树)的部分内容无效

    我有一些产品数据 需要在 Redis 缓存中存储多个版本 数据由 JSON 序列化对象组成 获取普通 基本 数据的过程很昂贵 将其定制为不同版本的过程也很昂贵 因此我想缓存所有版本以尽可能进行优化 数据结构看起来像这样 BaseProduc
  • AngularJS:选择非 2 路绑定到模型

    我正在使用选择来显示客户名称 用户应该能够选择现有客户端 然后更新范围属性 控制器 初始化 首选 if scope clients length gt 0 scope existingClient scope clients 0 View
  • 将变量声明为全局变量,然后声明为局部变量 -Shadowing-

    将变量声明为全局变量然后将其重新声明为局部变量意味着什么 int a 0 int main int a 7 return 0 我在参考文献中看到了这个例子 但我不明白 请考虑到我是 C 编程的初学者 这意味着在你的main方法 如果你只使用
  • 如何将类型传递给方法?

    我怎样才能调用这个构造函数 public class DataField public String Name public Type TheType public DataField string name Type T Name nam
  • 将 Node.js(用于实时通知)添加到现有 PHP 应用程序

    我有一个现有的 PHP 应用程序 我需要向其中添加实时通知 为了实现这一点 我安装了node js 打算添加socket io以实现所有实时功能 然而 尽管在过去的三个小时里研究并试图弄清楚如何将两者结合起来 但我发现自己并没有更接近于获得
  • Windows平台下C语言控制串口DTR和RTS引脚

    如何在windows平台上控制串口的DTR和RTS引脚 我希望通过升高或降低电压来对其进行位敲击或操作 您需要使用EscapeComm 函数 https learn microsoft com en us windows desktop a
  • Ruby on Rails - 设计注册链接不起作用 - 未定义方法“user_registration_path”

    这个问题之前已经报告过 但我仍然无法找到解决方案 我已将插件 Devise 安装到我的新 RoR 项目中 当我点击注册链接时 我被重定向到以下路线 http localhost 3000 users registration sign up
  • 如何在css3中制作曲线风格的菜单?

    是否可以用css3制作曲线 圆弧样式的菜单 我可以使用canvas或HTML5中的其他东西来实现这一点吗 预先感谢 洛根 不幸的是 我不知道有什么优雅的解决方案 特别是当涉及到菜单项时 但弧线本身应该可以在纯 css 和几个 html 元素
  • 2^31 次方的 Java 指数错误 [重复]

    这个问题在这里已经有答案了 我正在编写一个java程序来输出2的指数幂 顺便说一句 我不能使用Math pow 但是在 2 31 和 2 32 处我得到了其他东西 另外 我不打算接受负整数 My code class PrintPowers
  • React Native TypeError:无法读取未定义的属性“createClient”

    我是 React 本机框架的新手 我使用 create react native app AwesomeProject 创建了应用程序 我想在我的项目中使用 BLE 因此我安装了 react native ble plx 模块 但是当我创建
  • 无法验证 GitHub 中的虚假电子邮件

    我已经创建了一个 GitHub 帐户 并且我不喜欢公开分享我的电子邮件地址 我厌倦了垃圾邮件 所以我关注了 GitHub保密您的电子邮件地址 https help github com articles keeping your email
  • 向 ReduxReducer 添加回调

    是否有任何错误 反模式 就 React Redux 中的思考 中添加了一个回调action data转化为行动 reducer ACTION FOR REDUCER var x 123 if action data callback act
  • 如何使用 Angular 2 动画实现翻转效果?

    我一直在我的项目中使用纯CSS翻转卡片 但这个解决方案不是合适的 有人可以通过点击按钮来呈现角度 2 的翻转吗 我在 angularjs 中找到了一个https codepen io Zbeyer pen oXQrZg https code