资讯专栏INFORMATION COLUMN

好看漂亮的html5网页特效学习笔记(3)_猜猜下一个颜色是什么?

YancyYe / 1473人阅读

摘要:如字体页面推荐的那样,把下面这段代码在添加到页面标签内,即可嵌入相应的字体。但这还不是我们要的效果。它相对的不是父节点或者页面的根节点。而是由视窗大小来决定的,单位,代表视窗的。具体请看属性用来设置如何处理元素中的空白。

效果:

根据给出的两个连续颜色,玩家需要猜出下一个是什么颜色

随机关卡

使用vw,vh,vmin,vmax来屏幕自适应

很难玩

html+css+javascript,但js很短并不难,上手难度:简单

欢迎来我的博客看此文章: https://clatterrr.com/archive...

源码:

演示地址:https://clatterrr.github.io/c...

源码文件:https://github.com/clatterrr/...

学习笔记 使用google字体

这段用来导入google一种名叫Pacifico的字体。google字体中文页面:http://www.googlefonts.net/,选择喜欢的字体并取得名字,即可引用。一共三种方式,注意字体名字自己改:

像上面这样在css使用@import。

如google字体页面推荐的那样,把下面这段代码在html添加到页面 标签内,即可嵌入相应的字体。

</>复制代码

使用@font-face。

然后就可以高兴地使用喜欢的字体了。详细请看:https://www.ibm.com/developer...

让元素正中心对齐网页正中心(自适应)

有时候,我们想元素的恰好在网页中间,像上图下方那样,即元素正中心恰好就网页正中心,并且还要主动适应屏幕大小,怎么办?

如果我们不设置它们的位置,一般是元素左上角和网页的左上角对齐,如上图左上。

为了不被其它元素影响到,我们加一句

</>复制代码

  1. position: absolute;

然后要它到网页正中间,设置top和left,为了自适应,不使用px而使用百分比。

</>复制代码

  1. top: 50%;
  2. left: 50%;

嗯,现在就变成上图右上那样,元素左上角对齐网页正中间了。但这还不是我们要的效果。于是再加一句

</>复制代码

  1. transform: translate(-50%, -50%);
使用vw、vh、vmin、vmax来响应式调整元素大小

是一种视窗单位,也是相对单位。它相对的不是父节点或者页面的根节点。而是由视窗(Viewport)大小来决定的,单位 1,代表视窗的 1%。具体请看:https://blog.csdn.net/ZNYSYS5...

css属性white-space

用来设置如何处理元素中的 空白。具体请看https://developer.mozilla.org...

javascript解释

直接把注释放源码中了,颜色渐变原理也很简单

</>复制代码

  1. const game = {
  2. color: {
  3. red: 0,
  4. green: 0,
  5. blue: 0
  6. },
  7. variation: {
  8. red: 0,
  9. green: 0,
  10. blue: 0
  11. },
  12. right: 0,
  13. total: 0,
  14. //错误选项中的颜色变换
  15. possibilities: [
  16. [0, 0, 16], [0, 16, 0], [0, 16, 16], [16, 0, 0], [16, 0, 16], [16, 16, 0], [16, 16, 16],
  17. [0, 0, -16], [0, -16, 0], [0, -16, -16], [-16, 0, 0], [-16, 0, -16], [-16, -16, 0], [-16, -16, -16],
  18. [0, 16, -16], [0, -16, 16], [16, 0, -16], [-16, 0, 16], [16, -16, 0], [-16, 16, 0],
  19. [16, 16, -16], [16, -16, 16], [16, -16, -16], [-16, 16, 16], [-16, 16, -16], [-16, -16, 16]
  20. ],
  21. min: 50,
  22. correct: 0,
  23. initialize: function () {
  24. // 获取答案选项元素
  25. const boxes = document.querySelectorAll(".boxes.mini .color-box");
  26. for (let x = 0; x < boxes.length; x++) {
  27. //为每个选项元素添加点击事件
  28. boxes[x].addEventListener("click", function () {
  29. //如果点击的是正确的选项,那么就让结果面板添加correct类,以便让结果面板显示出来
  30. //点击的正确添加right类,给正确数量加上1
  31. if (this.dataset.value == game.correct) {
  32. document.querySelector("#scrim").classList.add("correct");
  33. this.classList.add("right");
  34. game.right++;
  35. } else {
  36. //如果点击的是错误的选项,那么就让结果面板添加incorrect类,以便让结果面板显示出来
  37. //点击的错误的选项添加wrong类,再让正确的选项添加上right类
  38. document.querySelector("#scrim").classList.add("incorrect");
  39. this.classList.add("wrong");
  40. document.querySelector(`[data-value="${game.correct}"]`).classList.add("right");
  41. }
  42. //更新游戏信息(网页右上角)
  43. game.total++;
  44. document.querySelector("#total").textContent = game.total;
  45. document.querySelector("#guessed").textContent = game.right;
  46. //最终结果显示,让第三个大正方形上方显示正确的颜色,下方显示玩家选择的颜色
  47. document.querySelector("#correct-color").style.backgroundColor = document.querySelector(`[data-value="${game.correct}"]`).style.backgroundColor;
  48. document.querySelector("#picked-color").style.backgroundColor = this.style.backgroundColor;
  49. });
  50. }
  51. //为结果面板的按钮添加点击事件,点击后开始新游戏
  52. document.querySelector("#scrim button").addEventListener("click", function () {
  53. const scrim = document.querySelector("#scrim");
  54. scrim.classList.remove("correct");
  55. scrim.classList.remove("incorrect");
  56. game.generateGame();
  57. });
  58. this.generateGame();
  59. },
  60. generateGame: function () {
  61. //移除选项中的正确和错误类
  62. var dright = document.querySelector(".right");
  63. if (dright) dright.classList.remove("right");
  64. var dwrong = document.querySelector(".wrong");
  65. if (dwrong) dwrong.classList.remove("wrong");
  66. //第三个大正方形重新回归未知状态
  67. document.querySelector("#correct-color").style.backgroundColor = "rgba(0,0,0,0)";
  68. document.querySelector("#picked-color").style.backgroundColor = "rgba(0,0,0,0)";
  69. //产生随机颜色
  70. this.color.red = this.min + Math.floor(Math.random() * (255 - this.min * 2));
  71. this.color.green = this.min + Math.floor(Math.random() * (255 - this.min * 2));
  72. this.color.blue = this.min + Math.floor(Math.random() * (255 - this.min * 2));
  73. //产生随机颜色变量
  74. this.variation.red = Math.floor((Math.random() * this.min) / 2);
  75. this.variation.green = Math.floor((Math.random() * this.min) / 2);
  76. this.variation.blue = Math.floor((Math.random() * this.min) / 2);
  77. //给前两个大正方形涂上颜色
  78. document.querySelector("#box-1").style.backgroundColor = `rgb(${this.color.red},${this.color.green},${this.color.blue})`;
  79. document.querySelector("#box-2").style.backgroundColor = `rgb(${this.color.red + this.variation.red},${this.color.green + this.variation.green},${this.color.blue + this.variation.blue})`;
  80. //随机选择正确的选项,并为它涂上正确的颜色
  81. this.correct = Math.floor(Math.random() * 4);
  82. document.querySelector("#color-" + this.correct).style.backgroundColor = `rgb(${this.color.red + this.variation.red * 2},${this.color.green + this.variation.green * 2},${this.color.blue + this.variation.blue * 2})`;
  83. //让其它错误的选项涂上错误的元素,具体办法是第二个大正方的颜色加上一个随机小变量
  84. for (let x = 0; x < 4; x++) {
  85. if (x != this.correct) {
  86. var change = Math.floor(Math.random() * this.possibilities.length);
  87. document.querySelector("#color-" + x).style.backgroundColor = `rgb(${this.color.red + this.variation.red + this.possibilities[change][0]},${this.color.green + this.variation.green + this.possibilities[change][1]},${this.color.blue + this.variation.blue + this.possibilities[change][2]})`;
  88. }
  89. }
  90. }
  91. }
  92. game.initialize()
其它源码

html

</>复制代码

  1. 下一个颜色是什么?
  2. 0 / 0
  3. Color Sequence Scheme

  4. Which color comes next?

  5. You picked the right color!

  6. Oh no! That was not the right color!

css

</>复制代码

  1. @import url("https://fonts.googleapis.com/css?family=Pacifico");
  2. html, body {
  3. background: #9cf;
  4. margin: 0;
  5. padding: 0;
  6. }
  7. h1, h2 {
  8. text-align: center;
  9. color: white;
  10. font-size: 5vmin;
  11. text-shadow: 0 1px 3px rgba(0,0,0,0.25);
  12. font-family: Pacifico, arial, serif;
  13. font-weight: normal;
  14. }
  15. h2 {
  16. font-size: 3.5vmin;
  17. margin-top: 5vmin;
  18. }
  19. #points {
  20. font-family: Pacifico, Verdana, sans-serif;
  21. color: white;
  22. font-size: 5vmin;
  23. text-shadow: 0 1px 3px rgba(0,0,0,0.25);
  24. position: absolute;
  25. top: 1vmin;
  26. right: 2vmin;
  27. }
  28. .center {
  29. position: absolute;
  30. top: 50%;
  31. left: 50%;
  32. transform: translate(-50%, -50%);
  33. }
  34. .boxes {
  35. margin: auto auto;
  36. text-align: center;
  37. white-space: nowrap;
  38. }
  39. .color-box {
  40. display: inline-block;
  41. background: red;
  42. box-sizing: border-box;
  43. border: 1.25vmin solid white;
  44. border-radius: 2px;
  45. width: 20vmin;
  46. height: 20vmin;
  47. margin-right: 5vmin;
  48. box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25);
  49. position: relative;
  50. }
  51. .boxes.mini .color-box {
  52. width: 15vmin;
  53. height: 15vmin;
  54. margin-right: 3vmin;
  55. cursor: pointer;
  56. }
  57. .color-box.right {
  58. border-color: green;
  59. }
  60. .color-box.wrong {
  61. border-color: #e81222;
  62. }
  63. #box-3 {
  64. margin-right: 0;
  65. background: #ccc;
  66. overflow: hidden;
  67. }
  68. #color-3 {
  69. margin-right: 0;
  70. }
  71. #box-3::before {
  72. content: "?";
  73. position: absolute;
  74. top: 50%;
  75. left: 50%;
  76. transform: translate(-50%, -50%);
  77. font-family: Pacifico, Verdana, sans-serif;
  78. font-size: 6vmin;
  79. color: rgba(0,0,0,0.5);
  80. }
  81. #scrim {
  82. position: fixed;
  83. top: 0;
  84. left: 0;
  85. width: 100%;
  86. height: 100%;
  87. background: rgba(0,0,0,0);
  88. display: none;
  89. }
  90. #scrim.correct,
  91. #scrim.incorrect {
  92. display: block;
  93. }
  94. #scrim > div {
  95. padding: 3vmin;
  96. border-radius: 3px;
  97. background: white;
  98. box-shadow: 0 0.5rem 1.5rem -0rem rgba(0,0,0,0.25);
  99. }
  100. #scrim h2 {
  101. color: #444;
  102. margin-top: 0;
  103. display: none;
  104. }
  105. #scrim.correct #correct,
  106. #scrim.incorrect #incorrect {
  107. display: block;
  108. }
  109. #scrim button {
  110. width: 100%;
  111. text-align: center;
  112. font-size: 2vmin;
  113. padding: 1.5vmin;
  114. border-radius: 3px;
  115. border: 0;
  116. background: #396;
  117. color: white;
  118. box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25);
  119. cursor: pointer;
  120. }
  121. #correct-color,
  122. #picked-color {
  123. position: absolute;
  124. width: 100%;
  125. height: 60%;
  126. z-index: 2;
  127. }
  128. #picked-color {
  129. top: 50%;
  130. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/106960.html

相关文章

  • 好看漂亮html5网页特效学习笔记(3)_猜猜一个颜色什么

    摘要:如字体页面推荐的那样,把下面这段代码在添加到页面标签内,即可嵌入相应的字体。但这还不是我们要的效果。它相对的不是父节点或者页面的根节点。而是由视窗大小来决定的,单位,代表视窗的。具体请看属性用来设置如何处理元素中的空白。 showImg(http://www.googlefonts.net/); 效果: 根据给出的两个连续颜色,玩家需要猜出下一个是什么颜色 随机关卡 使用vw,vh,...

    robin 评论0 收藏0
  • 好看漂亮html5网页特效学习笔记(4)_canvas实现火焰跟随鼠标

    摘要:好看漂亮的网页特效学习笔记猜猜下一个颜色是什么分步详细解释第一步很简单的初始化函数。绘制新的火焰红色的圆以及火花之前元素,即新的火焰红色的圆以及火花的生命周期未完的话,就继续更新它,否则就删除它第五步以火焰为粒子。 showImg(https://segmentfault.com/img/bVbwV8z?w=376&h=388); 效果: 逼真的火焰跟随鼠标,还冒出火花,照亮背景文字...

    SwordFly 评论0 收藏0
  • 好看漂亮html5网页特效学习笔记(1)_水墨动画

    摘要:对于来说,表示元素,除了优先级更高之外,与选择器相同。从父元素继承颜色渐变背景漂亮的深蓝浅蓝效果就是这个的作用。媒体查询,简单来说就是可以让网页自动适应不同的设备屏幕尺寸。具体请看贝塞尔曲线,用来生成水墨效果的关键。 showImg(https://segmentfault.com/img/bVbwNaj); 效果 鼠标触碰按钮,出现水墨风格动画 屏幕自适应 一份html文件,一份c...

    habren 评论0 收藏0
  • 好看漂亮html5网页特效学习笔记(1)_水墨动画

    摘要:对于来说,表示元素,除了优先级更高之外,与选择器相同。从父元素继承颜色渐变背景漂亮的深蓝浅蓝效果就是这个的作用。媒体查询,简单来说就是可以让网页自动适应不同的设备屏幕尺寸。具体请看贝塞尔曲线,用来生成水墨效果的关键。 showImg(https://segmentfault.com/img/bVbwNaj); 效果 鼠标触碰按钮,出现水墨风格动画 屏幕自适应 一份html文件,一份c...

    Null 评论0 收藏0
  • 好看漂亮html5网页特效学习笔记(2)_svg实现不同投票不同表情

    摘要:表情绘制使用纯代码绘制。其它表情请看源代码。当评分改变,这个高度很大的元素就向上移动,把需要的表情显示出来。源码不同投票不同表情 showImg(https://segmentfault.com/img/bVbwOQe?w=254&h=198); 特点: 根据不同评分显示不同表情,并且这些表情看起来像是在一个传送带上可以滚动 使用纯代码(svg)绘制表情以及用于评分的星星 html+...

    BaronZhang 评论0 收藏0

发表评论

0条评论

YancyYe

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<