【发布时间】:2017-04-17 00:00:22
【问题描述】:
我正在构建一个小型网络应用程序,该应用程序通过 Spotify API 查询热门曲目和艺术家,以将其返回并显示给用户。
我使用 Bootstrap 面板显示每个曲目或艺术家,标题中显示曲目或艺术家的名称,正文中显示相关图像。
对于轨道,这很有效,因为 API 响应中返回的图像似乎都是相同的尺寸。
不过,对于艺术家来说,返回的图像似乎都在其可能的尺寸上有所不同,这与曲目不同。
我试图通过仅使用图像数组中返回的图像之一并将图像的height 和width 设置为 100% 来解决此问题 - 但是,如果它们不是,这不适用于所有图像'不是正方形或图像分辨率低并且被样式炸毁了。
这是我的面板代码:
<div class="col-md-4" v-if="type == 'artists'" v-for="track in tracks">
<div class="panel panel-default">
<div class="panel-heading">@{{ track.name }}</div>
<div class="panel-body"><img :src="track.images[0].url" :alt="track.name" class="img-responsive center-block" style="height: 100%; width: 100%"/></div>
</div>
</div>
下面是一些可以在image 数组中返回的示例:
"images":[
{
"height":640,
"url":"https://i.scdn.co/image/64758843803a4cbda8c0413cb06dc896d74a0964",
"width":640
},
{
"height":320,
"url":"https://i.scdn.co/image/0f784b6a392e65e1ac637c487b27437ba7861198",
"width":320
},
{
"height":160,
"url":"https://i.scdn.co/image/80117df47ffed7d7b0cf490edc950cb285a226e7",
"width":160
}
]
"images":[
{
"height":640,
"url":"https://i.scdn.co/image/6a299fe9d66f3036bb0b22a458d38f662655b559",
"width":640
},
{
"height":300,
"url":"https://i.scdn.co/image/c9b1fe49b9139cc42090a8b045678ae9a323c400",
"width":300
},
{
"height":64,
"url":"https://i.scdn.co/image/476d0a6971a8e46a411dc2a5382b20176e0c1a23",
"width":64
}
]
"images":[
{
"height":666,
"url":"https://i.scdn.co/image/dd1ea0b4e68b25e2a82de61b03ee3933be266475",
"width":1000
},
{
"height":426,
"url":"https://i.scdn.co/image/b013b96bbe25ba13619f3d18f29f8dc999cdea7f",
"width":640
},
{
"height":133,
"url":"https://i.scdn.co/image/67636ab4283dc8783b60dd0ada9ade16300e3417",
"width":200
},
{
"height":43,
"url":"https://i.scdn.co/image/971cc938303fbebd8908ebfc30a9f759679efb14",
"width":64
}
]
我在我的应用程序中使用 Vue.js 来查询 API。
当我不知道将返回的分辨率时,我将如何正确且一致地渲染图像? 是否有一些 CSS 魔法或 JavaScript 可以帮助做到这一点?
【问题讨论】:
标签: javascript html css twitter-bootstrap vue.js