AMP-Carousel具有不同的图像尺寸

时间:2016-03-04 11:44:12

标签: amp-html

我正在努力创建一个具有不同尺寸图像的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小提琴,告诉你我有什么&amp;我想要的是什么

https://jsfiddle.net/ag38afa7/

编辑:

样式与文档不一致,或者我没有得到它们? 在amp-carousel页面上显示:Layout not supported for: responsive 但在演示页面上,amp-img元素有layout="responsive"
https://ampbyexample.com/components/amp-carousel/

2 个答案:

答案 0 :(得分:5)

您必须使用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>

目前不支持自动调整宽度。

答案 1 :(得分:0)

现在有一个全面的教程,其中有关于ampbyexample.com的说明,不再需要具有相同高度的图像。

这是一个略作修改的示例(我用figure代替了captionsdiv)最小工作示例:

<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>
相关问题