jQuery 响应式动态测验插件 Quiz.js

  • 源码大小:57.85KB
  • 所需积分:1积分
  • 源码编号:19JP-3564
  • 浏览次数:589次
  • 最后更新:2023年06月27日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

Javascript测验在网站和博客上正成为一种流行趋势。它们为您提供了一种吸引访问者与您的网站互动的方式,并展示您对HTML、CSS和Javascript的了解。

在本教程中,我将指导您使用问答jQuery插件创建一个响应迅速、动态、设计精美、多语言的问答。答案保存在cookie中,问答可以通过url中的哈希完全导航。

如何使用它:

1.在文档中加载所需的jQuery库和Bootstrap 4框架。

  1. <link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <script src="/path/to/cdn/bootstrap.bundle.min.js"></script>

2.下载并加载jQuery quiz.js插件的文件。

  1. <link rel="stylesheet" href="./dist/jquery.quiz.css" />
  2. <script src="./dist/jquery.quiz.js"></script>

3.创建一个空的DIV容器来容纳测验。

  1. <div id="example"></div>

4.用JSON定义您自己的问答问题和答案,如下所示。

  1. // quiz.json
  2. [{
  3. "questions": [{
  4. "question": "What's my favorite color?",
  5. "description": "Optional Description",
  6. "answers": [{
  7. "answer": "Red",
  8. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  9. "true": 0
  10. },
  11. {
  12. "answer": "Black",
  13. "alert": "<strong>Exactly!</strong> See <a href=\"#\"> for details!</a>",
  14. "true": 1
  15. },
  16. {
  17. "answer": "Blue",
  18. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  19. "true": 0
  20. }]
  21. },
  22. {
  23. "question": "What is my favorite programming language?",
  24. "description": null,
  25. "answers": [{
  26. "answer": "PHP",
  27. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  28. "true": 0
  29. },
  30. {
  31. "answer": "C++",
  32. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  33. "true": 0
  34. },
  35. {
  36. "answer": "JavaScript",
  37. "alert": "<strong>Exactly!</strong> See <a href=\"#\"> for details!</a>",
  38. "true": 1
  39. }]
  40. },
  41. {
  42. "question": "What is my favorite country?",
  43. "description": null,
  44. "answers": [{
  45. "answer": "UK",
  46. "alert": "<strong>Exactly!</strong> See <a href=\"#\"> for details!</a>",
  47. "true": 1
  48. },
  49. {
  50. "answer": "US",
  51. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  52. "true": 0
  53. },
  54. {
  55. "answer": "France",
  56. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  57. "true": 0
  58. }]
  59. },
  60. {
  61. "question": "Who is my favorite celebrity?",
  62. "description": null,
  63. "answers": [{
  64. "answer": "Johnny Depp",
  65. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  66. "true": 0
  67. },
  68. {
  69. "answer": "Emma Watson",
  70. "alert": "<strong>The answer is incorrect.</strong> See <a href=\"#\"> for details!</a>",
  71. "true": 0
  72. },
  73. {
  74. "answer": "Arnold Schwarzenegger",
  75. "alert": "<strong>Exactly!</strong> See <a href=\"#\"> for details!</a>",
  76. "true": 1
  77. }]
  78. }],
  79. "params": {
  80. "intro": 1,
  81. "intromessage": "A jQuery plugin that lets you create responsive and beautiful quizzes on the page. "
  82. }
  83. }]

5.初始化插件并在页面上生成一个测试。

  1. $(document).ready(function(){
  2. $('#example').quiz({
  3.  
  4. // path to JSON
  5. quizJson: "quiz.json",
  6.  
  7. // handle results
  8. onResults: function(good, total){
  9. var perc = good / total;
  10. var alert = $('<div class="alert"></div>')
  11. .prependTo(this);
  12. if(perc == 0){
  13. alert.addClass('alert-danger')
  14. .html('All wrong! You didn't get an answer right.');
  15. } else if(perc > 0 && perc <= 0.25){
  16. alert.addClass('alert-danger')
  17. .html('Poor result! You just got it right ' + good + ' answers on ' + total + '. Try again.');
  18. } else if(perc > 0.25 && perc <= 0.5){
  19. alert.addClass('alert-danger')
  20. .html('Just sufficient! You got it right ' + good + ' answers on ' + total + '. You can do better.');
  21. } else if(perc > 0.5 && perc <= 0.75){
  22. alert.addClass('alert-success')
  23. .html('Discreet result! You got it right ' + good + ' answers on ' + total + '. Try again.');
  24. } else if(perc > 0.75 && perc < 1){
  25. alert.addClass('alert-success')
  26. .html('Good result! You got it right ' + good + ' answers on ' + total + '. We are almost there.');
  27. } else if(perc == 1){
  28. alert.addClass('alert-success')
  29. .html('Congratulations, you have answered all the questions!');
  30. }
  31. }
  32. });
  33. });

6.指定答案选择cookie将被设置为过期的天数。默认值:3600(以秒为单位)。

  1. $('#example').quiz({
  2. cookieExpire: 7200,
  3. });

7.确定是否隐藏下一个/上一个按钮。默认值:false。

  1. $('#example').quiz({
  2. hidePrevBtn: true,
  3. hideRestartBtn: true,
  4. });

8.启用/禁用渐变过渡。默认值:true。

  1. $('#example').quiz({
  2. fade: true,
  3. });

9.设置问题的数量。默认值:false(无限制)。

  1. $('#example').quiz({
  2. numQuestions: 5,
  3. });

10.自定义问题模板。

  1. $('#example').quiz({
  2. // Templates
  3. introTpl: '<div class="quiz_intro">'
  4. // Quiz title
  5. + '<h2><%this.title%></h2>'
  6. // Quiz description
  7. + '<%if(this.description){%>'
  8. + '<p><%this.description%></p>'
  9. + '<%}%>'
  10. // end if
  11. + '</div>',
  12. questionTpl: '<div class="' + FLEX_CLASS + '">'
  13. + '<div class="' + NUM_CLASS + '">'
  14. // Quiz num question
  15. + '<%this.question.num%>'
  16. + '.</div>'
  17. + '<div class="' + FLEXFILL_CLASS + '">'
  18. + '<h2>'
  19. // Quiz question
  20. + '<%this.question.question%>'
  21. + '</h2>'
  22. // Quiz question description
  23. + '<%if(this.question.description){%>'
  24. + '<p>'
  25. + '<%this.question.description%>'
  26. + '</p>'
  27. + '<%}%>'
  28. // end if
  29. // Cycle answers
  30. + '<%for(var index in this.answers){%>'
  31. + '<div class="quiz_radiogroup">'
  32. // Quiz radio
  33. + '<input type="radio" id="answer-<%this.answers[index].num%>" '
  34. + 'name="question<%this.question.id%>" '
  35. + 'value="<%this.answers[index].num%>" '
  36. // This data (quiz-name and quiz-value) are mandatory
  37. + 'data-quiz-name="question<%this.question.id%>" '
  38. + 'data-quiz-value="<%this.answers[index].num%>"'
  39. // § mandatory
  40. + '<%this.answers[index].checked%>>'
  41. + '<label for="answer-<%this.answers[index].num%>"><span></span> '
  42. // Answer label
  43. + '<%this.answers[index].answer%>'
  44. + '</label>'
  45. + '</div>'
  46. + '<%}%>'
  47. // end for
  48. + '</div>'
  49. + '</div>',
  50. resultsTpl: '<div class="' + FLEX_CLASS + '">'
  51. + '<div class="' + NUM_CLASS + '">'
  52. // Quiz num question
  53. + '<%this.question.num%>'
  54. + '.</div>'
  55. + '<div class="' + FLEXFILL_CLASS + '">'
  56. + '<h2>'
  57. // Quiz question
  58. + '<%this.question.question%>'
  59. + '</h2>'
  60. // Quiz question description
  61. + '<%if(this.question.description){%>'
  62. + '<p>'
  63. + '<%this.question.description%>'
  64. + '</p>'
  65. + '<%}%>'
  66. // end if
  67. // Answer
  68. + '<%if(this.answer){%>'
  69. + '<div class="' + RESPONSE_CLASS + '">'
  70. + '<strong>'
  71. // Answer num
  72. + '<%this.answer.num + 1%>'
  73. + '.</strong> '
  74. // Answer
  75. + '<%this.answer.answer%>'
  76. + '</div>'
  77. // Correct response
  78. + '<%if(this.answer.true == 1){%>'
  79. + '<div class="' + ALERT_CLASS + ' quiz_success">'
  80. + THUMBSUP_ICO
  81. // Answer alert
  82. + '<%this.answer.alert%>'
  83. + '</div>'
  84. // Else wrong response
  85. + '<%} else{%>'
  86. + '<div class="' + ALERT_CLASS + ' quiz_fail">'
  87. + THUMBSDOWN_ICO
  88. // Answer alert
  89. + '<%this.answer.alert%>'
  90. + '</div>'
  91. + '<%}%>'
  92. // end if
  93. + '<%}%>'
  94. // end if
  95. + '</div>'
  96. + '</div>',
  97. startBtnTpl: '<button class="' + BTN_CLASS + '">'
  98. // Button start
  99. + '<%this.messages.start%>'
  100. + PLAY_ICO
  101. + '</button>',
  102. prevBtnTpl: '<button class="' + BTN_CLASS + '">'
  103. + BACKWARD_ICO
  104. // Button previous
  105. + '<%this.messages.prev%>'
  106. + '</button>',
  107. nextBtnTpl: '<button class="' + BTN_CLASS + '">'
  108. // Button next
  109. + '<%this.messages.next%>'
  110. + FORWARD_ICO
  111. + '</button>',
  112. resultBtnTpl: '<button class="' + BTN_CLASS + '">'
  113. // Button go to results
  114. + '<%this.messages.results%>'
  115. + FORWARD_ICO
  116. + '</button>',
  117. restartBtnTpl: '<button class="' + BTN_CLASS + '">'
  118. + REPEAT_ICO
  119. // Button restart
  120. + '<%this.messages.restart%>'
  121. + '</button>',
  122. modalBtnTpl: '<button class="' + BTN_CLASS + '" data-dismiss="modal">'
  123. + REPEAT_ICO
  124. // Button restart (modal)
  125. + '<%this.messages.restart%>'
  126. + '</button>',
  127. progressTpl: false
  128. });

11.将测验信息本地化。

  1. $.quiz('localization', {
  2. start: 'Start',
  3. prev: 'Back',
  4. next: 'Forward',
  5. results: 'Go to results',
  6. restart: 'Back to the top',
  7. error: 'Error',
  8. errmsg: [
  9. 'Please choose an answer',
  10. 'Some questions have not been answered. Please, back to the top to answer all questions'
  11. ]
  12. });

12.或者在页面上包含您选择的语言JS。

  1. <script src="./dist/i18n/messages_de.js"></script>
  2. <script src="./dist/i18n/messages_es.js"></script>
  3. <script src="./dist/i18n/messages_fr.js"></script>
  4. <script src="./dist/i18n/messages_it.js"></script>
  5. <script src="./dist/i18n/messages_nl.js"></script>
  6. <script src="./dist/i18n/messages_pt.js"></script>

更新日志:

2022-05-22

  • 2.0版本

2022-02-26

  • 添加测验模板并随机化问题

预览截图