Particles.js là một thư viện nhỏ gọn cung cấp hiệu ứng tạo ra các vật thể nhỏ trôi nổi trên trang web của bạn, cùng tham khảo nhé.

Hướng dẫn sử dụng hiệu ứng Particles.js

Xem demo Đến Particles.js

Sử dụng Particles.js

Để sử dụng Particles.js, bạn có thể tải về hoặc thêm trực tiếp thư viện sau từ CDN nhé. Lưu là nên dùng phiên bản 2.0.0 để cho hiệu quả tốt nhất.

<script src="http://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>

Hiệu ứng 1: Mặc định

HTML

<div class="particles-default-container"><div id="particles-default-js"></div></div>

CSS

.particles-default-container {
    height: 450px;
    margin-bottom: 30px;
    position: relative;
}
#particles-default-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: #b61924;
}

JS

particlesJS("particles-default-js", {
	"particles": {
		"number": {
			"value": 80,
			"density": {
				"enable": true,
				"value_area": 800
			}
		},
		"color": {
			"value": "#ffffff"
		},
		"shape": {
			"type": "circle",
			"stroke": {
				"width": 0,
				"color": "#000000"
			},
			"polygon": {
				"nb_sides": 5
			},
			"image": {
				"src": "img/github.svg",
				"width": 100,
				"height": 100
			}
		},
		"opacity": {
			"value": 0.5,
			"random": false,
			"anim": {
				"enable": false,
				"speed": 1,
				"opacity_min": 0.1,
				"sync": false
			}
		},
		"size": {
			"value": 3,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 40,
				"size_min": 0.1,
				"sync": false
			}
		},
		"line_linked": {
			"enable": true,
			"distance": 150,
			"color": "#ffffff",
			"opacity": 0.4,
			"width": 1
		},
		"move": {
			"enable": true,
			"speed": 6,
			"direction": "none",
			"random": false,
			"straight": false,
			"out_mode": "out",
			"bounce": false,
			"attract": {
				"enable": false,
				"rotateX": 600,
				"rotateY": 1200
			}
		}
	},
	"interactivity": {
		"detect_on": "canvas",
		"events": {
			"onhover": {
				"enable": true,
				"mode": "repulse"
			},
			"onclick": {
				"enable": true,
				"mode": "push"
			},
			"resize": true
		},
		"modes": {
			"grab": {
				"distance": 400,
				"line_linked": {
					"opacity": 1
				}
			},
			"bubble": {
				"distance": 400,
				"size": 40,
				"duration": 2,
				"opacity": 8,
				"speed": 3
			},
			"repulse": {
				"distance": 200,
				"duration": 0.4
			},
			"push": {
				"particles_nb": 4
			},
			"remove": {
				"particles_nb": 2
			}
		}
	},
	"retina_detect": true
});

Ta được kết quả.


Hiệu ứng 2: Tuyết

HTML

<div class="particles-snow-container"><div id="particles-snow-js"></div></div>

CSS

.particles-snow-container {
    height: 450px;
    margin-bottom: 30px;
    position: relative;
}
#particles-snow-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: #b61924;
}

JS

particlesJS("particles-snow-js", {
	"particles": {
		"number": {
			"value": 400,
			"density": {
				"enable": true,
				"value_area": 800
			}
		},
		"color": {
			"value": "#fff"
		},
		"shape": {
			"type": "circle",
			"stroke": {
				"width": 0,
				"color": "#000000"
			},
			"polygon": {
				"nb_sides": 5
			},
			"image": {
				"src": "img/github.svg",
				"width": 100,
				"height": 100
			}
		},
		"opacity": {
			"value": 0.5,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 1,
				"opacity_min": 0.1,
				"sync": false
			}
		},
		"size": {
			"value": 10,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 40,
				"size_min": 0.1,
				"sync": false
			}
		},
		"line_linked": {
			"enable": false,
			"distance": 500,
			"color": "#ffffff",
			"opacity": 0.4,
			"width": 2
		},
		"move": {
			"enable": true,
			"speed": 6,
			"direction": "bottom",
			"random": false,
			"straight": false,
			"out_mode": "out",
			"bounce": false,
			"attract": {
				"enable": false,
				"rotateX": 600,
				"rotateY": 1200
			}
		}
	},
	"interactivity": {
		"detect_on": "canvas",
		"events": {
			"onhover": {
				"enable": true,
				"mode": "bubble"
			},
			"onclick": {
				"enable": true,
				"mode": "repulse"
			},
			"resize": true
		},
		"modes": {
			"grab": {
				"distance": 400,
				"line_linked": {
					"opacity": 0.5
				}
			},
			"bubble": {
				"distance": 400,
				"size": 4,
				"duration": 0.3,
				"opacity": 1,
				"speed": 3
			},
			"repulse": {
				"distance": 200,
				"duration": 0.4
			},
			"push": {
				"particles_nb": 4
			},
			"remove": {
				"particles_nb": 2
			}
		}
	},
	"retina_detect": true
});

Ta được kết quả.


Hiệu ứng 3: Bóng

HTML

<div class="particles-bubble-container"><div id="particles-bubble-js"></div></div>

CSS

.particles-bubble-container {
    height: 450px;
    margin-bottom: 30px;
    position: relative;
}
#particles-bubble-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgb(44, 46, 67);
}

JS

particlesJS("particles-bubble-js", {
	"particles": {
		"number": {
			"value": 6,
			"density": {
				"enable": true,
				"value_area": 800
			}
		},
		"color": {
			"value": "#1b1e34"
		},
		"shape": {
			"type": "polygon",
			"stroke": {
				"width": 0,
				"color": "#000"
			},
			"polygon": {
				"nb_sides": 6
			},
			"image": {
				"src": "img/github.svg",
				"width": 100,
				"height": 100
			}
		},
		"opacity": {
			"value": 0.3,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 1,
				"opacity_min": 0.1,
				"sync": false
			}
		},
		"size": {
			"value": 160,
			"random": false,
			"anim": {
				"enable": true,
				"speed": 10,
				"size_min": 40,
				"sync": false
			}
		},
		"line_linked": {
			"enable": false,
			"distance": 200,
			"color": "#ffffff",
			"opacity": 1,
			"width": 2
		},
		"move": {
			"enable": true,
			"speed": 8,
			"direction": "none",
			"random": false,
			"straight": false,
			"out_mode": "out",
			"bounce": false,
			"attract": {
				"enable": false,
				"rotateX": 600,
				"rotateY": 1200
			}
		}
	},
	"interactivity": {
		"detect_on": "canvas",
		"events": {
			"onhover": {
				"enable": false,
				"mode": "grab"
			},
			"onclick": {
				"enable": false,
				"mode": "push"
			},
			"resize": true
		},
		"modes": {
			"grab": {
				"distance": 400,
				"line_linked": {
					"opacity": 1
				}
			},
			"bubble": {
				"distance": 400,
				"size": 40,
				"duration": 2,
				"opacity": 8,
				"speed": 3
			},
			"repulse": {
				"distance": 200,
				"duration": 0.4
			},
			"push": {
				"particles_nb": 4
			},
			"remove": {
				"particles_nb": 2
			}
		}
	},
	"retina_detect": true
});

Ta được kết quả.


Hiệu ứng 4: NASA

HTML

<div class="particles-nasa-container"><div id="particles-nasa-js"></div></div>

CSS

.particles-nasa-container {
    height: 450px;
    margin-bottom: 30px;
    position: relative;
    border-radius: var(--main-radius);
    overflow: hidden;
}
 #particles-nasa-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgb(35, 39, 65);
    background-image: url(https://inithtml.com/wp-content/uploads/2024/07/NASA.png);
    background-size: 20%;
    background-repeat: no-repeat;
    background-position: 50% 50%;
}

JS

particlesJS("particles-nasa-js", {
	"particles": {
		"number": {
			"value": 160,
			"density": {
				"enable": true,
				"value_area": 800
			}
		},
		"color": {
			"value": "#ffffff"
		},
		"shape": {
			"type": "circle",
			"stroke": {
				"width": 0,
				"color": "#000000"
			},
			"polygon": {
				"nb_sides": 5
			},
			"image": {
				"src": "img/github.svg",
				"width": 100,
				"height": 100
			}
		},
		"opacity": {
			"value": 1,
			"random": true,
			"anim": {
				"enable": true,
				"speed": 1,
				"opacity_min": 0,
				"sync": false
			}
		},
		"size": {
			"value": 3,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 4,
				"size_min": 0.3,
				"sync": false
			}
		},
		"line_linked": {
			"enable": false,
			"distance": 150,
			"color": "#ffffff",
			"opacity": 0.4,
			"width": 1
		},
		"move": {
			"enable": true,
			"speed": 1,
			"direction": "none",
			"random": true,
			"straight": false,
			"out_mode": "out",
			"bounce": false,
			"attract": {
				"enable": false,
				"rotateX": 600,
				"rotateY": 600
			}
		}
	},
	"interactivity": {
		"detect_on": "canvas",
		"events": {
			"onhover": {
				"enable": true,
				"mode": "bubble"
			},
			"onclick": {
				"enable": true,
				"mode": "repulse"
			},
			"resize": true
		},
		"modes": {
			"grab": {
				"distance": 400,
				"line_linked": {
					"opacity": 1
				}
			},
			"bubble": {
				"distance": 250,
				"size": 0,
				"duration": 2,
				"opacity": 0,
				"speed": 3
			},
			"repulse": {
				"distance": 400,
				"duration": 0.4
			},
			"push": {
				"particles_nb": 4
			},
			"remove": {
				"particles_nb": 2
			}
		}
	},
	"retina_detect": true
});

Ta được kết quả.


Hiệu ứng 5: Nyan Cat

HTML

<div class="particles-nyan-cat-container"><div id="particles-nyan-cat-js"></div></div>

CSS

.particles-nyan-cat-container {
    height: 450px;
    margin-bottom: 30px;
    position: relative;
}
#particles-nyan-cat-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgb(4, 53, 100);
    background-image: url(https://inithtml.com/wp-content/uploads/2024/07/nyan-cat.gif);
    background-size: 60%;
    background-repeat: no-repeat;
    background-position: 0px 50%;
}

JS

particlesJS("particles-nyan-cat-js", {
	"particles": {
		"number": {
			"value": 100,
			"density": {
				"enable": false,
				"value_area": 800
			}
		},
		"color": {
			"value": "#ffffff"
		},
		"shape": {
			"type": "star",
			"stroke": {
				"width": 0,
				"color": "#000000"
			},
			"polygon": {
				"nb_sides": 5
			},
			"image": {
				"src": "img/github.svg",
				"width": 100,
				"height": 100
			}
		},
		"opacity": {
			"value": 0.5,
			"random": false,
			"anim": {
				"enable": false,
				"speed": 1,
				"opacity_min": 0.1,
				"sync": false
			}
		},
		"size": {
			"value": 4,
			"random": true,
			"anim": {
				"enable": false,
				"speed": 40,
				"size_min": 0.1,
				"sync": false
			}
		},
		"line_linked": {
			"enable": false,
			"distance": 150,
			"color": "#ffffff",
			"opacity": 0.4,
			"width": 1
		},
		"move": {
			"enable": true,
			"speed": 14,
			"direction": "left",
			"random": false,
			"straight": true,
			"out_mode": "out",
			"bounce": false,
			"attract": {
				"enable": false,
				"rotateX": 600,
				"rotateY": 1200
			}
		}
	},
	"interactivity": {
		"detect_on": "canvas",
		"events": {
			"onhover": {
				"enable": false,
				"mode": "grab"
			},
			"onclick": {
				"enable": true,
				"mode": "repulse"
			},
			"resize": true
		},
		"modes": {
			"grab": {
				"distance": 200,
				"line_linked": {
					"opacity": 1
				}
			},
			"bubble": {
				"distance": 400,
				"size": 40,
				"duration": 2,
				"opacity": 8,
				"speed": 3
			},
			"repulse": {
				"distance": 200,
				"duration": 0.4
			},
			"push": {
				"particles_nb": 4
			},
			"remove": {
				"particles_nb": 2
			}
		}
	},
	"retina_detect": true
});

Ta được kết quả.

Chúc các bạn thành công!

5/5 (7 bình chọn)