发布时间:2025-12-10 13:03:30 浏览次数:7
以下是CSS背景音乐代码大全,帮助您添加宜人的音乐,增加网页的商业价值。代码使用pre标签方便快速复制粘贴使用。
1. 使用背景音乐HTML5标签和CSS样式
<audio autoplay="autoplay" loop="loop"><source src="your_music_file.mp3" type="audio/mpeg" /></audio>body { background: url('your_background_image_url') no-repeat center center fixed; -webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover;}2. 使用CSS选择器和伪元素添加背景音乐
body::before {content: '';display: block;position: fixed;z-index: -1;top: 0;left: 0;right: 0;bottom: 0;background-image: url('your_background_image_url');background-repeat: no-repeat;background-position: center;background-size: cover;filter: blur(5px);}body::after {content: '';display: block;position: fixed;z-index: -2;top: 0;left: 0;right: 0;bottom: 0;background-image: url('your_music_icon_url');background-repeat: no-repeat;background-position: center;background-size: 50px;opacity: 0.5;}body {background-color: rgba(0,0,0,0.5);}@media (max-width: 768px) {body::before {filter: none;}body::after {opacity: 0;}}@media (min-width: 769px) {body::before {animation: moveBackground 20s ease infinite;}@keyframes moveBackground {0% {background-position: center 0;}50% {background-position: center 100%;}100% {background-position: center 0;}}}<audio autoplay="autoplay" loop="loop"><source src="your_music_file.mp3" type="audio/mpeg" /></audio>3. 创建全屏背景音乐
<audio autoplay="autoplay" loop="loop"><source src="your_music_file.mp3" type="audio/mpeg" /></audio>#full-screen-background-music {position: fixed;left: 0;top: 0;z-index: -1;width: 100%;height: 100%;overflow: hidden;}#full-screen-background-music video {position: absolute;z-index: -1;top: 0;left: 0;width: 100%;height: 100%;object-fit: cover;}4. 使用jQuery添加背景音乐
<script src="jquery.min.js"></script><script>$(document).ready(function() {$('body').append('<audio autoplay="autoplay" loop="loop"><source src="your_music_file.mp3" type="audio/mpeg" /></audio>');});</script>无论是哪种方式,使用CSS背景音乐代码,可以让您的网站更加生动,增加访问者对您网站的留恋和情感共鸣。