用前端开发css把三张图片横过来并且有一定的间隔
时间: 2024-03-23 15:37:20 浏览: 16
可以使用以下 CSS 代码实现三张图片横过来并且有一定的间隔:
```css
.container {
display: flex;
justify-content: center;
}
.image {
margin: 0 20px; /* 设置图片之间的间隔 */
}
```
然后在 HTML 中创建一个包含三张图片的容器,并为每个图片添加 `image` 类:
```html
<div class="container">
<img src="image1.jpg" alt="" class="image">
<img src="image2.jpg" alt="" class="image">
<img src="image3.jpg" alt="" class="image">
</div>
```
这样就可以实现三张图片横过来并且有一定的间隔的效果。
相关问题
请用前端开发css把两个悬浮动画排到一排并且有一定间隔
可以使用以下 CSS 代码实现两个悬浮动画排到一排并且有一定间隔的效果:
```css
.container {
display: flex;
justify-content: center;
}
.animation {
margin: 0 20px; /* 设置动画之间的间隔 */
animation: floating 2s ease-in-out infinite;
}
@keyframes floating {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
100% {
transform: translateY(0);
}
}
```
然后在 HTML 中创建一个包含两个悬浮动画的容器,并为每个动画添加 `animation` 类:
```html
<div class="container">
<div class="animation">悬浮动画1</div>
<div class="animation">悬浮动画2</div>
</div>
```
这样就可以实现两个悬浮动画排到一排并且有一定间隔的效果。
web前端开发打地鼠小游戏HTML+CSS+JS
好的,下面我来给你讲解一下如何用 HTML、CSS 和 JS 开发一个打地鼠小游戏。
首先,在 HTML 中创建游戏界面,可以使用以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打地鼠小游戏</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
width: 400px;
height: 400px;
margin: auto;
}
.hole {
width: 100px;
height: 100px;
background-color: #ccc;
border: 1px solid #000;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
}
.hole:hover {
background-color: #999;
}
</style>
</head>
<body>
<div class="container">
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
<div class="hole"></div>
</div>
<script src="script.js"></script>
</body>
</html>
```
在上面的代码中,我们创建了一个包含 9 个洞口的容器,每个洞口都是一个 div 元素,样式为灰色背景、黑色边框、30px 的字号、光标为手型,并且当鼠标悬浮在洞口上时会变为深灰色背景。其中容器的样式使用了 flex 布局,可以使洞口自动排列。
接下来,我们使用 JS 来实现地鼠的随机出现和消失,代码如下:
```js
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const moles = document.querySelectorAll('.mole');
let lastHole;
let timeUp = false;
let score = 0;
function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function randomHole(holes) {
const idx = Math.floor(Math.random() * holes.length);
const hole = holes[idx];
if (hole === lastHole) {
return randomHole(holes);
}
lastHole = hole;
return hole;
}
function peep() {
const time = randomTime(200, 1000);
const hole = randomHole(holes);
hole.classList.add('up');
setTimeout(() => {
hole.classList.remove('up');
if (!timeUp) peep();
}, time);
}
function startGame() {
scoreBoard.textContent = 0;
timeUp = false;
score = 0;
peep();
setTimeout(() => {
timeUp = true;
}, 10000);
}
function bonk(e) {
if (!e.isTrusted) return;
score++;
this.classList.remove('up');
scoreBoard.textContent = score;
}
moles.forEach(mole => mole.addEventListener('click', bonk));
```
在上面的代码中,我们首先获取了 HTML 中所有的洞口和地鼠元素,并定义了一些变量用来存储上一个出现的洞口、游戏是否结束和得分等信息。接着,我们实现了三个函数:
1. `randomTime`:用于生成一个随机时间,表示地鼠出现和消失的时间间隔。
2. `randomHole`:用于随机选择一个洞口,保证地鼠每次出现的位置不同。
3. `peep`:用于控制地鼠的随机出现和消失,每次随机生成一个时间和一个洞口,将该洞口的样式修改为出现状态并在一定时间后恢复为原来的样式,然后在一定时间后再次调用该函数。
最后,我们实现了开始游戏和打地鼠的函数,并将打地鼠的函数绑定到每个地鼠元素的点击事件上。在打地鼠函数中,我们判断是否由用户点击触发,如果不是则直接返回,否则将得分加一并将该地鼠的样式修改为消失状态。
完成以上代码后,你就可以在浏览器中打开该 HTML 文件,开始你的打地鼠小游戏了!