我正在努力创建一个具有不同尺寸的图像的AMP-Carousel。我想将旋转木马中的图像缩放到固定高度和自动宽度。
文档中提供的示例都具有相同的宽度/高度。
我已经尝试省略了amp-img元素的宽度并使用layout =“fixed-height”,但这根本不起作用。文档非常令人困惑。
<amp-carousel width=500 height=300>
<amp-img src="http://placehold.it/200x600" width=200 height=600></amp-img>
<amp-img src="http://placehold.it/700x550" width=700 height=550></amp-img>
<amp-img src="http://placehold.it/340x410" width=340 height=410></amp-img>
</amp-carousel>
我创造了一个js-fiddle来向你展示我拥有的和我想要的东西
https://jsfiddle.net/ag38afa7/
编辑:
样式与文档不一致或者我没有得到它们?
在amp-carousel页面上它说:Layout not supported for: responsive
但在演示页面上amp-img元素有layout="responsive"
https://ampbyexample.com/components/amp-carousel/
您必须使用fixed-height
布局并为旋转木马和所有图像提供相同的高度。需要相应地更新宽度以保持纵横比。例:
<amp-carousel height=300 layout="fixed-height">
<amp-img src="http://placehold.it/200x600" width=100 height=300></amp-img>
<amp-img src="http://placehold.it/700x550" width=381 height=300></amp-img>
<amp-img src="http://placehold.it/340x410" width=248 height=300></amp-img>
</amp-carousel>
目前不支持自动调整宽度。
现在有一个全面的教程,对ampbyexample.com进行了解释,并且不再需要具有相同高度的图像。
这是一个略有修改(我使用figure
而不是div
为captions)最小的工作示例:
<style amp-custom>
figure {
position: relative;
width: 100%;
height: 300px;
}
amp-img img {
object-fit: contain;
}
</style>
<amp-carousel
height="300"
layout="fixed-height"
type="slides">
<figure>
<amp-img
layout="fill"
src="https://unsplash.it/500/400"></amp-img>
</figure>
<figure>
<amp-img
layout="fill"
src="https://unsplash.it/500/500"></amp-img>
</figure>
<figure>
<amp-img
layout="fill"
src="https://unsplash.it/300/500"></amp-img>
</figure>
</amp-carousel>