请使用js实现一丛会动的白玫瑰花,我需要源码
时间: 2024-03-06 20:49:45 浏览: 13
好的,以下是一个使用 JavaScript 实现的会动的白玫瑰花的代码。这里使用了 CSS3 的动画效果。
HTML 代码:
```html
<div class="flower-container">
<div class="flower">
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="center"></div>
</div>
</div>
```
CSS 代码:
```css
.flower-container {
position: relative;
width: 100px;
height: 100px;
animation: rotate 10s linear infinite;
}
.flower {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: 100%;
transform-origin: center;
}
.petals {
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 50px;
border-radius: 10px;
transform-origin: center bottom;
animation: petal 4s ease-in-out infinite;
}
.petals:nth-child(1) {
transform: translate(-25px, -50%) rotate(0deg);
}
.petals:nth-child(2) {
transform: translate(-25px, -50%) rotate(72deg);
}
.petals:nth-child(3) {
transform: translate(-25px, -50%) rotate(144deg);
}
.petals:nth-child(4) {
transform: translate(-25px, -50%) rotate(216deg);
}
.petals:nth-child(5) {
transform: translate(-25px, -50%) rotate(288deg);
}
@keyframes petal {
0% {
transform: translate(-25px, -50%) rotate(0deg);
}
50% {
transform: translate(-25px, -50%) rotate(30deg);
}
100% {
transform: translate(-25px, -50%) rotate(0deg);
}
}
.center {
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
background-color: white;
border-radius: 50%;
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
```
JavaScript 代码:
```javascript
// 获取花朵元素
const flower = document.querySelector('.flower');
// 当鼠标移动到花朵上时,停止花朵旋转
flower.addEventListener('mouseenter', () => {
flower.style.animationPlayState = 'paused';
});
// 当鼠标移出花朵时,恢复花朵旋转
flower.addEventListener('mouseleave', () => {
flower.style.animationPlayState = 'running';
});
```
完整的 HTML 文件代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>会动的白玫瑰花</title>
<style>
.flower-container {
position: relative;
width: 100px;
height: 100px;
animation: rotate 10s linear infinite;
}
.flower {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: 100%;
transform-origin: center;
}
.petals {
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 50px;
border-radius: 10px;
transform-origin: center bottom;
animation: petal 4s ease-in-out infinite;
}
.petals:nth-child(1) {
transform: translate(-25px, -50%) rotate(0deg);
}
.petals:nth-child(2) {
transform: translate(-25px, -50%) rotate(72deg);
}
.petals:nth-child(3) {
transform: translate(-25px, -50%) rotate(144deg);
}
.petals:nth-child(4) {
transform: translate(-25px, -50%) rotate(216deg);
}
.petals:nth-child(5) {
transform: translate(-25px, -50%) rotate(288deg);
}
@keyframes petal {
0% {
transform: translate(-25px, -50%) rotate(0deg);
}
50% {
transform: translate(-25px, -50%) rotate(30deg);
}
100% {
transform: translate(-25px, -50%) rotate(0deg);
}
}
.center {
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
background-color: white;
border-radius: 50%;
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="flower-container">
<div class="flower">
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="petals"></div>
<div class="center"></div>
</div>
</div>
<script>
// 获取花朵元素
const flower = document.querySelector('.flower');
// 当鼠标移动到花朵上时,停止花朵旋转
flower.addEventListener('mouseenter', () => {
flower.style.animationPlayState = 'paused';
});
// 当鼠标移出花朵时,恢复花朵旋转
flower.addEventListener('mouseleave', () => {
flower.style.animationPlayState = 'running';
});
</script>
</body>
</html>
```
你可以将这些代码复制到一个 HTML 文件中,然后在浏览器中打开这个文件,就可以看到一个会动的白玫瑰花了。当鼠标移动到花朵上时,花朵会暂停旋转,移出时则恢复旋转。