资讯专栏INFORMATION COLUMN

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

robin / 1799人阅读

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

效果:

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

随机关卡

使用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...

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

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

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

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

position: absolute;

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

 top: 50%;
 left: 50%;

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

transform: translate(-50%, -50%);


使用vw、vh、vmin、vmax来响应式调整元素大小

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

css属性white-space

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

javascript解释

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

const game = {
  color: {
    red: 0,
    green: 0,
    blue: 0
  },
  variation: {
    red: 0,
    green: 0,
    blue: 0
  },
  right: 0,
  total: 0,
  //错误选项中的颜色变换
  possibilities: [
    [0, 0, 16], [0, 16, 0], [0, 16, 16], [16, 0, 0], [16, 0, 16], [16, 16, 0], [16, 16, 16],
    [0, 0, -16], [0, -16, 0], [0, -16, -16], [-16, 0, 0], [-16, 0, -16], [-16, -16, 0], [-16, -16, -16],
    [0, 16, -16], [0, -16, 16], [16, 0, -16], [-16, 0, 16], [16, -16, 0], [-16, 16, 0],
    [16, 16, -16], [16, -16, 16], [16, -16, -16], [-16, 16, 16], [-16, 16, -16], [-16, -16, 16]
  ],
  min: 50,
  correct: 0,
  initialize: function () {
    // 获取答案选项元素
    const boxes = document.querySelectorAll(".boxes.mini .color-box");
    for (let x = 0; x < boxes.length; x++) {
      //为每个选项元素添加点击事件
      boxes[x].addEventListener("click", function () {
        //如果点击的是正确的选项,那么就让结果面板添加correct类,以便让结果面板显示出来
        //点击的正确添加right类,给正确数量加上1
        if (this.dataset.value == game.correct) {
          document.querySelector("#scrim").classList.add("correct");
          this.classList.add("right");
          game.right++;
        } else {
            //如果点击的是错误的选项,那么就让结果面板添加incorrect类,以便让结果面板显示出来
            //点击的错误的选项添加wrong类,再让正确的选项添加上right类
          document.querySelector("#scrim").classList.add("incorrect");
          this.classList.add("wrong");
          document.querySelector(`[data-value="${game.correct}"]`).classList.add("right");
        }
        //更新游戏信息(网页右上角)
        game.total++;
        document.querySelector("#total").textContent = game.total;
        document.querySelector("#guessed").textContent = game.right;
        //最终结果显示,让第三个大正方形上方显示正确的颜色,下方显示玩家选择的颜色
        document.querySelector("#correct-color").style.backgroundColor = document.querySelector(`[data-value="${game.correct}"]`).style.backgroundColor;
        document.querySelector("#picked-color").style.backgroundColor = this.style.backgroundColor;
      });
    }
 
    //为结果面板的按钮添加点击事件,点击后开始新游戏
    document.querySelector("#scrim button").addEventListener("click", function () {
      const scrim = document.querySelector("#scrim");
      scrim.classList.remove("correct");
      scrim.classList.remove("incorrect");
      game.generateGame();
    });
    this.generateGame();
  },
  generateGame: function () {
 
    //移除选项中的正确和错误类
    var dright = document.querySelector(".right");
    if (dright) dright.classList.remove("right");
    var dwrong = document.querySelector(".wrong");
    if (dwrong) dwrong.classList.remove("wrong");
 
    //第三个大正方形重新回归未知状态
 
    document.querySelector("#correct-color").style.backgroundColor = "rgba(0,0,0,0)";
    document.querySelector("#picked-color").style.backgroundColor = "rgba(0,0,0,0)";
 
    //产生随机颜色
    this.color.red = this.min + Math.floor(Math.random() * (255 - this.min * 2));
    this.color.green = this.min + Math.floor(Math.random() * (255 - this.min * 2));
    this.color.blue = this.min + Math.floor(Math.random() * (255 - this.min * 2));
    
    //产生随机颜色变量
    this.variation.red = Math.floor((Math.random() * this.min) / 2);
    this.variation.green = Math.floor((Math.random() * this.min) / 2);
    this.variation.blue = Math.floor((Math.random() * this.min) / 2);
   //给前两个大正方形涂上颜色
    document.querySelector("#box-1").style.backgroundColor = `rgb(${this.color.red},${this.color.green},${this.color.blue})`;
    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})`;
 
    //随机选择正确的选项,并为它涂上正确的颜色
    this.correct = Math.floor(Math.random() * 4);
    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})`;
 
    //让其它错误的选项涂上错误的元素,具体办法是第二个大正方的颜色加上一个随机小变量
    for (let x = 0; x < 4; x++) {
      if (x != this.correct) {
        var change = Math.floor(Math.random() * this.possibilities.length);
        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]})`;
      }
    }
  }
}
 
game.initialize()
其它源码

html



 

  
  下一个颜色是什么?
 
 
 
  
 
 

 

 
  
0 / 0

Color Sequence Scheme

Which color comes next?

You picked the right color!

Oh no! That was not the right color!

css

@import url("https://fonts.googleapis.com/css?family=Pacifico");
 
html, body {
  background: #9cf;
  margin: 0;
  padding: 0;
}
 
h1, h2 {
  text-align: center;
  color: white;
  font-size: 5vmin;
  text-shadow: 0 1px 3px rgba(0,0,0,0.25);
  font-family: Pacifico, arial, serif;
  font-weight: normal;
}
 
h2 {
  font-size: 3.5vmin;
  margin-top: 5vmin;
}
 
#points {
  font-family: Pacifico, Verdana, sans-serif;
  color: white;
  font-size: 5vmin;
  text-shadow: 0 1px 3px rgba(0,0,0,0.25);
  position: absolute;
  top: 1vmin;
  right: 2vmin;
}
 
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.boxes {
  margin: auto auto;
  text-align: center;
  white-space: nowrap;
}
 
.color-box {
  display: inline-block;
  background: red;
  box-sizing: border-box;
  border: 1.25vmin solid white;
  border-radius: 2px;
  width: 20vmin;
  height: 20vmin;
  margin-right: 5vmin;
  box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25);
  position: relative;
}
 
.boxes.mini .color-box {
  width: 15vmin;
  height: 15vmin;
  margin-right: 3vmin;
  cursor: pointer;
}
 
.color-box.right {
  border-color: green;
}
 
.color-box.wrong {
  border-color: #e81222;
}
 
#box-3 {
  margin-right: 0;
  background: #ccc;
  overflow: hidden;
}
 
#color-3 {
  margin-right: 0;
}
 
#box-3::before {
  content: "?";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: Pacifico, Verdana, sans-serif;
  font-size: 6vmin;
  color: rgba(0,0,0,0.5);
}
 
#scrim {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0);
  display: none;
}
 
#scrim.correct,
#scrim.incorrect {
  display: block;
}
 
#scrim > div {
  padding: 3vmin;
  border-radius: 3px;
  background: white;
  box-shadow: 0 0.5rem 1.5rem -0rem rgba(0,0,0,0.25);
}
 
#scrim h2 {
  color: #444;
  margin-top: 0;
  display: none;
}
 
#scrim.correct #correct,
#scrim.incorrect #incorrect {
  display: block;
}
 
#scrim button {
  width: 100%;
  text-align: center;
  font-size: 2vmin;
  padding: 1.5vmin;
  border-radius: 3px;
  border: 0;
  background: #396;
  color: white;
  box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25);
  cursor: pointer;
}
 
#correct-color,
#picked-color {
  position: absolute;
  width: 100%;
  height: 60%;
  z-index: 2;
}
 
#picked-color {
  top: 50%;
}

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

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

相关文章

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

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

    YancyYe 评论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条评论

阅读需要支付1元查看
<