Hiệu ứng tuyết rơi ngẫu nhiên trang trí Noel cho blog/website

Noel không chỉ là dịp kỷ niệm ngày Thiên Chúa giáng thế theo đạo Thiên Chúa mà đây còn là một ngày lễ của mỗi gia đình. Hôm nay, hãy cùng ChuHung.Net trang trí đón Noel cho blog/website bằng hiệu ứng tuyết rơi tuyệt đẹp nhé!.

→ Chuyển blog sang chế độ tối để nhìn rõ hơn nhé!

Các bước thực hiện

Bước 1: Copy toàn bộ code dưới đây và tuỳ chỉnh:

<style>
	#snowflakeContainer{position:absolute;left:0px;top:0px;}
	.snowflake{padding-left:15px;font-size:14px;line-height:24px;position:fixed;color:#ebebeb;user-select:none;z-index:1000;-moz-user-select:none;-ms-user-select:none;-khtml-user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;}
	.snowflake:hover {cursor:default}
</style>
<div id='snowflakeContainer'>
<p class='snowflake'></p>
</div>
<script style='text/javascript'>
	//<![CDATA[
	var requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame;var transforms=["transform","msTransform","webkitTransform","mozTransform","oTransform"];var transformProperty=getSupportedPropertyName(transforms);var snowflakes=[];var browserWidth;var browserHeight;var numberOfSnowflakes=50;var resetPosition=false;function setup(){window.addEventListener("DOMContentLoaded",generateSnowflakes,false);window.addEventListener("resize",setResetFlag,false)}setup();function getSupportedPropertyName(b){for(var a=0;a<b.length;a++){if(typeof document.body.style[b[a]]!="undefined"){return b[a]}}return null}function Snowflake(b,a,d,e,c){this.element=b;this.radius=a;this.speed=d;this.xPos=e;this.yPos=c;this.counter=0;this.sign=Math.random()<0.5?1:-1;this.element.style.opacity=0.5+Math.random();this.element.style.fontSize=4+Math.random()*30+"px"}Snowflake.prototype.update=function(){this.counter+=this.speed/5000;this.xPos+=this.sign*this.speed*Math.cos(this.counter)/40;this.yPos+=Math.sin(this.counter)/40+this.speed/30;setTranslate3DTransform(this.element,Math.round(this.xPos),Math.round(this.yPos));if(this.yPos>browserHeight){this.yPos=-50}};function setTranslate3DTransform(a,c,b){var d="translate3d("+c+"px, "+b+"px, 0)";a.style[transformProperty]=d}function generateSnowflakes(){var b=document.querySelector(".snowflake");var h=b.parentNode;browserWidth=document.documentElement.clientWidth;browserHeight=document.documentElement.clientHeight;for(var d=0;d<numberOfSnowflakes;d++){var j=b.cloneNode(true);h.appendChild(j);var e=getPosition(50,browserWidth);var a=getPosition(50,browserHeight);var c=5+Math.random()*40;var g=4+Math.random()*10;var f=new Snowflake(j,g,c,e,a);snowflakes.push(f)}h.removeChild(b);moveSnowflakes()}function moveSnowflakes(){for(var b=0;b<snowflakes.length;b++){var a=snowflakes[b];a.update()}if(resetPosition){browserWidth=document.documentElement.clientWidth;browserHeight=document.documentElement.clientHeight;for(var b=0;b<snowflakes.length;b++){var a=snowflakes[b];a.xPos=getPosition(50,browserWidth);a.yPos=getPosition(50,browserHeight)}resetPosition=false}requestAnimationFrame(moveSnowflakes)}function getPosition(b,a){return Math.round(-1*b+Math.random()*(a+2*b))}function setResetFlag(a){resetPosition=true};
	//]]>
</script>

Tuỳ chỉnh mặc định:

  • Thay đổi biểu tượng: <p class='snowflake'>❄</p>
  • Thay đổi màu tuyết: color:#ebebeb
  • Thay đổi số lượng: numberOfSnowflakes=50
  • Thay đổi kích thước: 30+"px"

Bước 2: Chèn toàn bộ code vào trước thẻ đóng </body>

Bước 3: Lưu lại và tận hưởng thành quả!

Lời kết

Trên đây là toàn bộ hướng dẫn và code hiệu ứng tuyết rơi ngẫu nhiên trang trí Noel cho blog/website. Chúc các bạn có một ngày học tập và làm việc thật hiệu quả. Xin chào và hẹn gặp lại!

Tôi là một lập trình viên IOS. Code chính là IOS nhưng thỉnnh thoảng vẫn đá sang Android hoặc web. Mặc dù không quá thông thạo nhưng tôi sẽ chia sẻ những kiến thức mà mình đã tìm hiểu, áp dụng qua.

Bài viết liên quan

Sơ đồ workflow migrate và sqlc trong CI/CD pipeline

Database migration an toàn với Golang golang-migrate sqlc Atlas so sánh

Tại sao cần database migration tool trong Golang Khi xây dựng ứng dụng production với Golang, việc quản lý thay đổi schema database là bài toán bắt buộc. Ba công…

Xem thêm

TypeScript 5.5: type inference cải tiến, decorators chuẩn hóa, isolated declarations

Giới thiệu TypeScript 5.5 mang đến những cải tiến đáng kể cho hệ thống kiểu dữ liệu, khiến mã nguồn an toàn hơn và dễ bảo trì hơn. Bài viết…

Xem thêm

So sánh các web framework Python phổ biến

Giới thiệu So sánh các web framework Python phổ biến là một trong những chủ đề nóng nhất trong thời gian qua. Bài viết này sẽ cung cấp một cái…

Xem thêm
5 1 đánh giá
Article Rating
Theo dõi
Thông báo của
guest
10 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất