生日到了,赠送一个特别的生日蛋糕给她!

583 阅读4分钟

我正在参加「码上掘金挑战赛」详情请看:码上掘金挑战赛来了!

前言

今天是家中长姐的生日,简单实现一个线上的生日蛋糕给她许愿,祝福她生日快乐!

码上掘金

使用css实现一个插上蜡烛的生日蛋糕,点击‘关灯许愿’按钮,关灯许愿,许愿时间设定为7秒钟,之后蜡烛自动熄灭。具体展示效果: code.juejin.cn/pen/7142850…

实现思路

  1. 整体页面使用flex布局,让蛋糕元素垂直居中显示
body {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  background-color: #f07167;
  overflow: hidden;
}
  1. 用css画一个装蛋糕的盘子。使用蛋糕元素的伪元素实现, 并加上内阴影,实现盘子的立体效果
.birthday-cake:before {
  content: "";
  position: absolute;
  background-color: #ede0d4;
  width: 400px;
  height: 140px;
  border-radius: 50%;
  left: 50%;
  bottom: -140px;
  transform: translateX(-50%);
  box-shadow: inset -2px -5px #e6ccb2;
}
  1. 蛋糕由主体部分、中间部分和顶部组成。主体部分主要设置蛋糕的宽高,以及使用伪元素:before实现蛋糕底部的圆柱形状;中间部分设置蛋糕的夹层,比如巧克力、喜欢吃的水果;顶部设置蛋糕周边的小花边装饰,使用给同一个元素添加多个阴影效果实现,以及表面的水果涂层或者巧克力涂层。
.cake {
  position: absolute;
  top: 50%;
  left: 50%;
  background-color: #fff;
  width: 350px;
  height: 130px;
  transform: translate(-50%, -50%);
}

.cake:before,
.cake:after {
  content: "";
  position: absolute;
}

.cake:before,
.middle,
.middle:before {
  border-radius: 50% 50% 50% 50% / 0% 0% 100% 100%;
  width: 350px;
  height: 50px;
}

.cake:before {
  background-color: #fff;
  top: 130px;
}

.cake:after {
  background-color: #7f5539;
  width: 350px;
  height: 30px;
  top: 50px;
}

.middle {
  position: absolute;
  background-color: #7f5539;
  top: 80px;
  z-index: 1;
}

.middle:before {
  content: "";
  position: absolute;
  background-color: #fff;
  top: -35px;
}

.top {
  position: absolute;
  background-color: #eea08c;
  width: 350px;
  height: 90px;
  border-radius: 50%;
  z-index: 2;
  top: -45px;
  box-shadow: inset -5px -1px #fff, inset -70px 2px rgba(255, 255, 255, .1);
}

.chocs {
  position: absolute;
  width: 55px;
  height: 50px;
  background-color: #eea08c;
  top: 0;
  z-index: 1;
  border-radius: 50% 50% 50% 50% / 0% 0% 100% 100%;
  box-shadow: 49px 20px #eea08c, 98px 25px #eea08c, 147px 30px #eea08c, 196px 25px #eea08c, 245px 20px #eea08c, 295px 0 #eea08c, 0px 4px #fff, 49px 24px #fff, 98px 29px #fff, 147px 34px #fff, 196px 29px #fff, 245px 24px #fff, 295px 4px #fff;
}
  1. 画一个蜡烛定位在蛋糕顶部,并加上蜡烛的火焰元素,实现蜡烛点燃的效果,同时给火焰元素加上动画属性,增加火焰跳动的效果
.velas {
  background: #ffffff;
  position: absolute;
  top: -115px;
  left: 50%;
  margin-left: -5px;
  width: 10px;
  height: 50px;
}

.velas:after,
.velas:before {
  background: rgba(255, 0, 0, 0.4);
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
}

.velas:after {
  top: 15%;
  left: 0;
}

.velas:before {
  top: 25%;
  left: 0;
}


.fuego {
  border-radius: 100%;
  box-shadow: 0 0 40px 10px rgba(248, 233, 209, 0.2);
  position: absolute;
  top: -12px;
  left: 50%;
  margin-left: -3px;
  width: 6px;
  height: 12.5px;
}

.fuego:nth-child(1) {
  animation: fuego 2s infinite;
}

.fuego:nth-child(2) {
  animation: fuego 1.5s infinite;
}

.fuego:nth-child(3) {
  animation: fuego 1s infinite;
}

.fuego:nth-child(4) {
  animation: fuego 0.5s infinite;
}

.fuego:nth-child(5) {
  animation: fuego 0.2s infinite;
}
@keyframes fuego {
  0% {
    background: rgba(254, 248, 97, 0.5);
    transform: translateY(0) scale(1);
  }

  50% {
    background: rgba(255, 50, 0, 0.1);
    transform: translateY(-20px) scale(0);
  }

  100% {
    background: rgba(254, 248, 97, 0.5);
    transform: translateY(0) scale(1);
  }
}
  1. 点击‘关灯许愿’文字按钮,页面屏幕增加黑暗效果,实现许愿的简单环境,许愿时间为7秒钟,时间到了之后,蜡烛熄灭,屏幕变亮
.text {
  position: fixed;
  color: #fff;
  width: 35px;
  right: 40px;
  bottom: 20px;
  cursor: pointer;
  font-size: 20px;
  text-shadow: 
      4px 4px 0px rgba(0, 0, 0, .15),
      1px 1px 0 rgba(255, 200, 200, 255),
      2px 2px 0 rgba(255, 150, 150, 255),
      3px 3px 0 rgba(255, 125, 125, 255);
}
  1. 增加背景音乐生日快乐歌(ps: 这首听起来挺happy的,jym猜猜是谁唱的?)

后记:这篇文章本来该昨天完成的,被我硬生生拖到今天才写完,算是给家姐迟来的小小祝福吧!