雪花特效---纯css实现

15 篇文章 0 订阅
6 篇文章 1 订阅

html代码

<div class="snow-container">
  <div class="snow foreground"></div>
  <div class="snow foreground layered"></div>
  <div class="snow middleground"></div>
  <div class="snow middleground layered"></div>
  <div class="snow background"></div>
  <div class="snow background layered"></div>
</div>

css代码:

.snow-container {
  position: absolute;
  height: 800px;
  width: 100%;
  max-width: 100%;
  top: 0;
  overflow: hidden;
  z-index: 2;
  pointer-events: none;
  background-color: red;
}

.snow {
  display: block;
  position: absolute;
  z-index: 2;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  pointer-events: none;
  -webkit-transform: translate3d(0, -100%, 0);
          transform: translate3d(0, -100%, 0);
  -webkit-animation: snow linear infinite;
          animation: snow linear infinite;
}
.snow.foreground {
  background-image: url("https://dl6rt3mwcjzxg.cloudfront.net/assets/snow/snow-large-075d267ecbc42e3564c8ed43516dd557.png");
  -webkit-animation-duration: 15s;
          animation-duration: 15s;
}
.snow.foreground.layered {
  -webkit-animation-delay: 7.5s;
          animation-delay: 7.5s;
}
.snow.middleground {
  background-image: url(https://dl6rt3mwcjzxg.cloudfront.net/assets/snow/snow-medium-0b8a5e0732315b68e1f54185be7a1ad9.png);
  -webkit-animation-duration: 20s;
          animation-duration: 20s;
}
.snow.middleground.layered {
  -webkit-animation-delay: 10s;
          animation-delay: 10s;
}
.snow.background {
  background-image: url(https://dl6rt3mwcjzxg.cloudfront.net/assets/snow/snow-small-1ecd03b1fce08c24e064ff8c0a72c519.png);
  -webkit-animation-duration: 30s;
          animation-duration: 30s;
}
.snow.background.layered {
  -webkit-animation-delay: 15s;
          animation-delay: 15s;
}

@-webkit-keyframes snow {
  0% {
    -webkit-transform: translate3d(0, -100%, 0);
            transform: translate3d(0, -100%, 0);
  }
  100% {
    -webkit-transform: translate3d(15%, 100%, 0);
            transform: translate3d(15%, 100%, 0);
  }
}

@keyframes snow {
  0% {
    -webkit-transform: translate3d(0, -100%, 0);
            transform: translate3d(0, -100%, 0);
  }
  100% {
    -webkit-transform: translate3d(15%, 100%, 0);
            transform: translate3d(15%, 100%, 0);
  }
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 这是一个可以在 Vue.js 中实现完整雪花特效的代码示例: ``` <template> <div id="app"> <div class="snowflakes" v-bind:style="snowflakesStyle"> <span v-for="i in snowflakesCount" v-bind:key="i" class="snowflake"></span> </div> </div> </template> <style> .snowflake { position: absolute; top: -50px; left: -50px; width: 50px; height: 50px; background: white; border-radius: 50%; box-shadow: 0 0 10px white; animation: falling 10s linear infinite; } @keyframes falling { from { transform: rotate(0deg) translateY(-50px); } to { transform: rotate(360deg) translateY(100vh); } } </style> <script> export default { name: "App", data() { return { snowflakesCount: 100, snowflakesStyle: { width: "100vw", height: "100vh", position: "fixed", top: 0, left: 0, overflow: "hidden" } }; } }; </script> ``` 在这段代码中,我们使用了 Vue.js 的 `v-for` 指令来循环渲染 `snowflake` 组件,并为每个雪花设置了绝对定位,使用了 CSS 动画来让雪花旋转并下落。 希望这个示例能帮助你。 ### 回答2: 以下是一个用Vue实现完整雪花特效的代码示例: ``` html <template> <div> <canvas ref="canvas" :width="width" :height="height"></canvas> </div> </template> <script> export default { data() { return { width: 800, height: 600, ctx: null, flakes: [], numFlakes: 100, }; }, mounted() { this.initCanvas(); this.generateFlakes(); this.animate(); }, methods: { initCanvas() { this.ctx = this.$refs.canvas.getContext('2d'); }, generateFlakes() { for (let i = 0; i < this.numFlakes; i++) { const x = Math.random() * this.width; const y = Math.random() * this.height; const size = Math.random() * 3 + 2; const speed = Math.random() * 1 + 0.5; const opacity = Math.random() * 0.5 + 0.3; const flake = { x, y, size, speed, opacity }; this.flakes.push(flake); } }, animate() { requestAnimationFrame(this.animate); this.ctx.clearRect(0, 0, this.width, this.height); for (let i = 0; i < this.numFlakes; i++) { const flake = this.flakes[i]; flake.y += flake.speed; if (flake.y > this.height) { flake.y = 0 - flake.size; } this.ctx.beginPath(); this.ctx.arc(flake.x, flake.y, flake.size, 0, Math.PI * 2); this.ctx.closePath(); this.ctx.fillStyle = `rgba(255, 255, 255, ${flake.opacity})`; this.ctx.fill(); } }, }, }; </script> ``` 上述代码实现了一个简单的雪花特效。首先,我们在模板中通过canvas元素创建了一个画布。在`mounted`生命周期钩子函数中,我们初始化了画布并生成了一定数量的雪花。然后,在`animate`方法中,我们使用循环遍历所有的雪花并更新它们的位置,根据需要循环播放雪花下落的动画效果。最后,我们使用`requestAnimationFrame`方法递归地调用`animate`方法,以实现平滑的动画效果。 这段代码的关键是使用`requestAnimationFrame`方法不断地重绘画布以更新雪花的位置和透明度。在循环中,我们使用`arc`方法绘制雪花的圆形,并使用`fillStyle`属性设置雪花的颜色和透明度,最后调用`fill`方法将雪花渲染到画布上。 你可以将以上代码复制到Vue项目中的组件中使用,然后引入这个组件并将其放置到你的网页中即可看到雪花特效的效果。 ### 回答3: Vue实现完整雪花特效的代码如下: ```vue <template> <div id="snowflakes"> <img v-for="(flake, index) in snowflakes" :key="index" class="snowflake" :src="flake.url" :style="flake.style" /> </div> </template> <script> export default { data() { return { snowflakes: [] }; }, methods: { createSnowflake() { const screenWidth = document.documentElement.clientWidth; const screenHeight = document.documentElement.clientHeight; const flakeSize = this.getRandomSize(); const flakeSpeed = this.getRandomSpeed(); return { url: 'snowflake.png', // 雪花图片的路径 style: { left: `${this.getRandomPosition(screenWidth)}px`, top: '-50px', width: `${flakeSize}px`, height: `${flakeSize}px`, animation: `fall ${flakeSpeed}s linear infinite` } }; }, getRandomPosition(max) { return Math.floor(Math.random() * max); }, getRandomSize() { return Math.floor(Math.random() * 20) + 10; }, getRandomSpeed() { return Math.floor(Math.random() * 5) + 3; }, startSnowfall() { setInterval(() => { this.snowflakes.push(this.createSnowflake()); }, 500); } }, mounted() { this.startSnowfall(); } }; </script> <style> #snowflakes { position: fixed; top: 0; left: 0; z-index: -1; pointer-events: none; } .snowflake { position: absolute; } @keyframes fall { 0% { transform: translateY(-50px) rotate(0deg); } 100% { transform: translateY(100vh) rotate(360deg); } } </style> ``` 上面的代码实现了一个简单的雪花特效。首先,在`mounted`生命周期钩子中调用`startSnowfall`方法来开始雪花的飘落。`startSnowfall`方法使用`setInterval`函数来定时添加雪花对象到`snowflakes`数组中,每500毫秒添加一个。 在`createSnowflake`方法中,根据屏幕的宽度和高度计算雪花的位置、尺寸和速度。生成的每个雪花对象都有一个`url`属性,以指定雪花图片的路径,并使用动态绑定的方式将雪花对象的样式应用到`img`标签上。 在样式中,为`img`标签设置`position: absolute`,使其脱离文档流,并在`#snowflakes`容器内创建一个层级低于其他元素的效果。使用`@keyframes`和`animation`属性来定义雪花下落的动画效果,其中`fall`为动画的名称,`transform`属性为雪花的位移和旋转效果。 每个雪花对象都会根据其定义的动画属性在页面中飘落。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值