javascript - 如何让“内联”图像全部排成一行?
问题描述
我试图让图像占用尽可能少的空间。他们占用空间的原因似乎是因为我进行了 50% 的转换,但我希望剩余的空白不占用页面上的空间。
在第一行,如果不是全部 4 张,至少应该有 3 张钞票(5,50,100)的空间。
我怎样才能让它们都适合?
这是我的代码:
应用程序.css
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-float infinite 3s ease-in-out;
}
}
.half-container {
display: flex;
flex-flow: row;
}
.left {
display: flex;
flex-flow: row wrap;
width: 60%;
}
.right {
width: 40%;
}
.App-header {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
}
.App-link {
color: rgb(112, 76, 182);
}
@keyframes App-logo-float {
0% {
transform: translateY(0);
}
50% {
transform: translateY(10px)
}
100% {
transform: translateY(0px)
}
}
应用程序.js
import React from 'react';
import { useState } from 'react';
import logo from './logo.svg';
import { Counter } from './features/counter/Counter';
import './App.css';
import fifty from './img/fifty.png'
import five from './img/five.png'
import hundred from './img/hundred.png'
import loonie from './img/loonie.png'
import ten from './img/ten.png'
import toonie from './img/toonie.png'
import twenty from './img/twenty.png'
const randomInt = ( lo, hi ) => {
return Math.floor( Math.random() * hi )
}
const getRandomCash = () => {
return {
loonie: randomInt(0, 3),
toonie: randomInt(0,3),
five: randomInt(0,3),
ten: randomInt(0,3),
twenty: randomInt(0,3),
fifty: randomInt(0,3),
hundred: randomInt(0,3)
}
}
const values = {
loonie: 1,
toonie: 2,
five: 5,
ten: 10,
twenty: 20,
fifty: 50,
hundred: 100
}
const images = { loonie, toonie, five, ten, twenty, fifty, hundred }
function App() {
const [cash, setCash] = useState(getRandomCash());
console.log(cash);
let items = Object.keys(cash).map(
key => Array(cash[key]).fill(key)
).flat()
console.log(items);
let imageDivs = items.map( item => {
return (
<div style={{width:'auto', height:'auto'}}>
<img style={{transform:'scale(0.5)'}} src={images[item]} alt="" >
</img>
</div>
)
})
//console.log(imageDivs)
return (
<div className="App">
<div class="half-container">
<div class="left">
{imageDivs}
</div>
<div class="right">
<div style={{margin:'30px'}}>
<h3>How much money?</h3>
<input type="text"/>
<div style={{marginTop:'20px'}}>
<button onClick={() => setCash(getRandomCash())}>
New Wallet
</button>
<button>
Submit
</button>
</div>
</div>
</div>
</div>
</div>
)
}
export default App;
解决方案
您可以使用引导网格行列,而不是像这样转换它
let imageDivs = items.map( item => {
return (
<div class="col-md-3">
<img src={images[item]} alt="" >
</img>
</div>
)
})
并且不要忘记添加行类
<div class="row left">
{imageDivs}
</div>
推荐阅读
- python - 替换 Python 列表中元素的值
- oracle - 将 Oracle 自治数据库连接到 .net 实体框架
- angular - 为什么在对接 Angular 项目时显示“找不到 Angular 项目”
- python-3.x - 如何编写for循环以使其在第N次出现条件时停止?
- c++ - 从基类实例调用派生类方法而不进行强制转换
- python - 如何在 pandas 和 python 的 grouby 数据图中显示线条的标签
- html - 浏览器因嵌入网站而停止工作,但仅在显示图标时
- ffmpeg - Raspberry Pi Zero W 上的 FFmpeg 安装:对 __atomic 的未定义引用
- sql - 在 Postgres 中搜索部分存在于字符串中的多个单词
- linux - 如何检查我的交通控制规则的运行情况?