使用AngularJS为quizz应用设置动画

时间:2017-02-24 10:48:31

标签: css angularjs animation

我做了一个测验申请,但我想添加一些动画 喜欢fadein / fade-out,点击上一个/下一个按钮。可以任何一个 帮我做同样的事。什么东西需要改变css的东西需要改变CSS的东西需要改变css的东西需要改变css吗?

* {}

body {}

.question {
  width: 70%;
  margin: 0 auto;
  height: auto;
  display: block;
  background: #eeeeee;
}

.question h1 {
  text-align: center;
  padding-top: 30px;
  color: #666666;
}

.question h2 {
  width: 100%;
  font-size: 22px;
  color: #0c1e5c;
  padding: 1% 3% 0% 3%;
}

.question ul:nth-child(odd) {
  background: #d0dff6;
  width: 30%;
  padding: 8px;
  margin: 1% 9%;
  display: inline-block;
  color: #0c1e5c;
}

.question ul:nth-child(even) {
  background: #d0dff6;
  width: 30%;
  padding: 8px;
  margin: 1% 9%;
  display: inline-block;
  color: #0c1e5c;
}

.button {
  text-align: center;
  margin: 1% 0;
}

.btn {
  background: #8bf8a7;
  padding: 5px;
}
<html ng-app="quiz">

<head>
  <meta charset="utf-8">
  <title>Basic Quiz</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
  <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body ng-controller="quizCtrl">
  <div class="question">
    <h1>QUIZ APPLICATION</h1>
    <h2>{{questions.question}}</h2>
    <ul ng-repeat="option in questions.options">
      <li style="list-style:none">
        <input type="{{buttonType}}">{{option.text}}</li>
    </ul>
  </div>
  <div class="button">
    <input type="button" value="previous" class="btn" ng-show="isPrevious" ng-click="previousQuestion()">
    <input type="button" value="next" class="btn" ng-show="isNext" ng-click="nextQuestion()">
  </div>
</body>
<script>
  var app = angular.module("quiz", [])
  app.controller("quizCtrl", function($scope) {
    $scope.data = [{
        question: "1)Which of the following selector matches a element based on its id?",
        type: "single",
        options: [{
            text: "The Id Selector"
          },
          {
            text: "The Universal Selector"
          },
          {
            text: "The Descendant Selector"
          },
          {
            text: "The Class Selector"
          }
        ]
      },
      {
        question: "2)Which of the following defines a measurement as a percentage relative to another value, typically an enclosing element?",
        type: "multiple",
        options: [{
            text: "%"
          },
          {
            text: "cm"
          },
          {
            text: "percentage"
          },
          {
            text: "ex"
          }
        ]
      },
      {
        question: "3)Which of the following property is used to set the background color of an element?",
        type: "single",
        options: [{
            text: "background-color"
          },
          {
            text: "background-image"
          },
          {
            text: "background-repeat"
          },
          {
            text: "background-position"
          }
        ]
      },
      {
        question: "4)Which of the following is a true about CSS style overriding?",
        type: "multiple",
        options: [{
            text: "Any inline style sheet takes highest priority. So, it will override any rule defined in tags or rules defined in any external style sheet file."
          },
          {
            text: "Any rule defined in tags will override rules defined in any external style sheet file."
          },
          {
            text: "Any rule defined in external style sheet file takes lowest priority, and rules defined in this file will be applied only when above two rules are not applicable."
          }
        ]
      }
    ];
    $scope.index = 0;
    $scope.questions = $scope.data[$scope.index];
    $scope.buttonType = $scope.questions.type == 'single' ? 'radio' : 'checkbox';
    $scope.isPrevious = false;
    $scope.isNext = true;
    $scope.nextQuestion = function() {
      if ($scope.index < 3) {
        $scope.index = $scope.index + 1;
        $scope.questions = $scope.data[$scope.index];
        $scope.buttonType = $scope.questions.type == 'single' ? 'radio' : 'checkbox';
        $scope.isPrevious = true;
        if ($scope.index == 3) {
          $scope.isNext = false;
        }
      } else {
        // disble next botton logic
        $scope.isNext = false;
      }
    }

    $scope.previousQuestion = function() {
      if ($scope.index > 0) {
        $scope.index = $scope.index - 1;
        $scope.questions = $scope.data[$scope.index];
        $scope.buttonType = $scope.questions.type == 'single' ? 'radio' : 'checkbox';
        $scope.isNext = true;
        if ($scope.index == 0) {
          $scope.isPrevious = false;
        }
      } else {
        // disble next botton logic
        $scope.isPrevious = false;
      }
    }

  });
</script>

</html>

1 个答案:

答案 0 :(得分:0)

查看ng-animate,基本上它的作用是添加类,你可以相应地显示dom和隐藏dom,如下所示:

/* The starting CSS styles for the enter animation */
.fade.ng-enter {
  transition:0.5s linear all;
  opacity:0;
}

/* The finishing CSS styles for the enter animation */
.fade.ng-enter.ng-enter-active {
  opacity:1;
}

要使用该功能,您必须在html中使用ng-repeat,如下所示:

<div ng-repeat="item in data" ng-if="index === $index">
    //Your question html here
</div>

数据和索引是$ scope.data和$ scope.index。

这将是有条不紊的做事方式。

但是我看到你使用相同的div,只更改范围数据,这需要你设置

transition: 1s all ease;

在问题类上,然后在javascript中执行类似的操作:

angular.element('.question').css('opacity', 0);
$timeout(function() {
    // change question..
    angular.element('.question').css('opacity', 1);
}, 1000);