我有一个带有layout=“响应式”的amp-img,我需要把一些文本放在里面,或者放在上面,如果你喜欢这样说的话。amp-img将填充屏幕的宽度,高度将由amp-img确定,这样整个图像都是可见的,并且宽高比保持不变。
有没有办法做到这一点?
我可以将图像放在背景中,但会丢失amp-img提供的响应式大小。我试过使用背景大小的封面或容器,但我发现图像总是被裁剪,要么是在右边,要么是在底部。
我还尝试使用position:absolute来放置文本,但无法将文本放在图像的顶部。下面是一次尝试,结果是图像下面的文本:
<div style="position:relative">
<amp-img src="/images/@Model.ImageUrl" layout="responsive" width="1920" height="1080" alt=""></amp-img> @* 16 x 9 *@
<div class="clearfix" style="padding-top:25%; padding-bottom:10%; position:absolute; z-index:1">
<div class="mx-auto md-col-9">
<h2 class="tx-g2 tx-center ml-1 mr-1 shadow mb-0" style="{text-transform:uppercase;}">
<amp-fit-text width="400" height="20" layout="responsive" max-font-size="75">
@Html.Raw(Model.Title)
</amp-fit-text>
</h2>
</div>
</div>
</div>有没有一种方法可以让图片的大小正确,并将文本放在上面?
PS。有人会为amp-fit-text创建一个标签吗?
发布于 2019-02-08 14:09:25
您的容器缺少宽度。下面是一个更简单的例子。
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title> Hello World</title>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-custom>
</style>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<div style="position: relative;">
<amp-img src="https://omoiwords.com/stories-poster.jpeg-2048.jpeg"
width="1228" height="819" layout="responsive"></amp-img>
<div style="background-color: rgba(0,0,0,0.7);
color: white; width: 80%; position:absolute; top:10%; left: 10%;">
<amp-fit-text
width="200" height="50" layout="responsive">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
</amp-fit-text>
</div>
</div>
</body>
</html>
发布于 2020-04-08 19:10:24
<div style="position: relative;">
<amp-img src="https://omoiwords.com/stories-poster.jpeg-2048.jpeg"
width="1228" height="819" layout="responsive"></amp-img>
<div style="background-color: rgba(0,0,0,0.7);
color: white; width: 80%; position:absolute; top:10%; left: 10%;">
<amp-fit-text
width="200" height="50" layout="responsive">
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
</amp-fit-text>
</div>
</div>
<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>发布于 2019-02-08 23:48:14
我发现了一种使用amp-carousel和单张幻灯片的解决方法:
<amp-carousel layout="responsive" height="1080" width="1920" type="slides" style="position:relative;">
<div style="background:linear-gradient(rgba(0,0,0,0), rgba(0,0,0,0)),
url(/images/@Model.ImageUrl);
background-size:contain; background-repeat:no-repeat; width:100%; height:100%;">
<div class="clearfix" style="padding-top:25%; padding-bottom:10%;">
<div class="mx-auto md-col-9">
<h2 class="tx-g2 tx-center ml-1 mr-1 shadow mb-0" style="{text-transform:uppercase;}">
<amp-fit-text width="400" height="20" layout="responsive" max-font-size="75">
@Html.Raw(Model.Title)
</amp-fit-text>
</h2>
</div>
</div>
</div>
https://stackoverflow.com/questions/54466477
复制相似问题