amp-image-slider
简介
amp-image-slider
组件允许通过移动垂直滑块来比较 2 张图像。用户可以单击/触摸并拖动滑块来比较图像。
设置
在头部导入 amp-image-slider
组件。
<script async custom-element="amp-image-slider" src="https://cdn.ampproject.org/v0/amp-image-slider-0.1.js"></script>
在本示例中,我们将创建一些类来定位标签。(我们将在下一节中重新访问)
<style amp-custom>
.label {
color: white;
background-color: rgba(0, 0, 0, 0.4);
width: 5rem;
padding: 1rem 0;
text-align: center;
font-weight: bold;
}
.label-left-center {
top: 50%;
left: 1rem;
transform: translateY(-50%);
}
.label-right-center {
top: 50%;
right: 1rem;
transform: translateY(-50%);
}
.triangle-hint .amp-image-slider-hint-left {
width: 10px;
height: 20px;
background-size: 10px 20px;
margin-right: 10px;
background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='10' height='20' viewBox='0 0 10 20'%3e%3cpolygon points='10,0 0,10 10,20' style='fill:white' /%3e%3c/svg%3e");
}
.triangle-hint .amp-image-slider-hint-right {
width: 10px;
height: 20px;
background-size: 10px 20px;
margin-left: 10px;
background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='10' height='20' viewBox='0 0 10 20'%3e%3cpolygon points='0,0 10,10 0,20' style='fill:white' /%3e%3c/svg%3e");
}
.slider-no-display .amp-image-slider-hint-left, .slider-no-display .amp-image-slider-hint-right {
display: none;
}
.seek-button-container {
display: flex;
justify-content: space-around;
padding: 1rem;
}
</style>
基本用法
通过在内部放置 2 个 amp-img
来构建基本图像滑块。第一个 amp-img
将是左侧图像,第二个将是右侧图像。
<amp-image-slider width="300" height="200" layout="responsive">
<amp-img src="/static/samples/img/canoe_900x600_blur.jpg" alt="A blurry image about canoeing" layout="fill"></amp-img>
<amp-img src="/static/samples/img/canoe_900x600.jpg" alt="An image about canoeing" layout="fill"></amp-img>
</amp-image-slider>
自定义文本标签
您可以通过提供额外的 div
来为您的图像添加可自定义的文本标签,这些 div
分别使用 first
和 second
属性标记,用于左侧和右侧图像。您可以使用类自定义标签的样式和位置。例如,要垂直居中图像上的标签,您可以使用 top/bottom
和 transform
规则。有关详细信息,请查看“设置”部分。
<amp-image-slider width="300" height="200" layout="responsive">
<amp-img src="/static/samples/img/red_apple_1_1024x682.jpg" alt="A red apple" layout="fill"></amp-img>
<amp-img src="/static/samples/img/green_apple_1_1024x682.jpg" alt="A green apple" layout="fill"></amp-img>
<div first class="label label-left-center">Red</div>
<div second class="label label-right-center">Green</div>
</amp-image-slider>
箭头提示
默认情况下,关于移动图像滑块的箭头提示将始终显示。一旦用户与滑块交互(例如单击和触摸),提示将淡出。如果滑块离开视口并在稍后再次返回,则提示将重新出现。要禁用此默认行为,请向滑块添加 disable-hint-reappear
属性。
<amp-image-slider width="300" height="200" layout="responsive" disable-hint-reappear>
<amp-img src="/static/samples/img/canoe_900x600_blur.jpg" alt="A blurry image about canoeing" layout="fill"></amp-img>
<amp-img src="/static/samples/img/canoe_900x600.jpg" alt="An image about canoeing" layout="fill"></amp-img>
</amp-image-slider>
自定义提示
通过覆盖 .amp-image-slider-hint-left
和 .amp-image-slider-hint-right
类,您可以更改左右提示图标的样式。特别是,您可以覆盖 background-image
以提供您自己的图标。
<amp-image-slider class="triangle-hint" width="300" height="200" layout="responsive">
<amp-img src="/static/samples/img/canoe_900x600_blur.jpg" alt="A blurry image about canoeing" layout="fill"></amp-img>
<amp-img src="/static/samples/img/canoe_900x600.jpg" alt="An image about canoeing" layout="fill"></amp-img>
</amp-image-slider>
隐藏提示
要隐藏提示,您可以简单地为这些类设置 display: none;
。
<amp-image-slider class="slider-no-display" width="300" height="200" layout="responsive">
<amp-img src="/static/samples/img/red_apple_1_1024x682.jpg" alt="A red apple" layout="fill"></amp-img>
<amp-img src="/static/samples/img/green_apple_1_1024x682.jpg" alt="A green apple" layout="fill"></amp-img>
</amp-image-slider>
动作
amp-image-slider
提供了一个动作 .seekTo(percent=[0-1])
,该动作将滑块移动到从左侧开始的相应百分比。例如,您可以使用 slider-id.seekTo(percent=0)
和 slider-id.seekTo(percent=1)
将滑块移动到左侧和右侧边框。
<amp-image-slider id="seekable" width="300" height="200" layout="responsive">
<amp-img src="/static/samples/img/canoe_900x600_blur.jpg" alt="A blurry image about canoeing" layout="fill"></amp-img>
<amp-img src="/static/samples/img/canoe_900x600.jpg" alt="An image about canoeing" layout="fill"></amp-img>
</amp-image-slider>
<div class="seek-button-container">
<button on="tap:seekable.seekTo(percent=1)">SHOW LEFT</button>
<button on="tap:seekable.seekTo(percent=0)">SHOW RIGHT</button>
</div>
不同的初始位置
默认情况下,滑块最初将放置在滑块的中心。您可以使用 initial-slider-position
属性更改其初始位置。
<amp-image-slider width="300" height="200" layout="responsive" initial-slider-position="0.3">
<amp-img src="/static/samples/img/red_apple_1_1024x682.jpg" alt="A red apple" layout="fill"></amp-img>
<amp-img src="/static/samples/img/green_apple_1_1024x682.jpg" alt="A green apple" layout="fill"></amp-img>
</amp-image-slider>
不同的步长
通过将 step-size
设置为 0 到 1 之间的值,您可以在滑块获得焦点时,每次按左箭头键和右箭头键时,以自定义百分比移动滑块。
<amp-image-slider tabindex="0" width="300" height="200" layout="responsive" step-size="0.2">
<amp-img src="/static/samples/img/red_apple_1_1024x682.jpg" alt="A red apple" layout="fill"></amp-img>
<amp-img src="/static/samples/img/green_apple_1_1024x682.jpg" alt="A green apple" layout="fill"></amp-img>
</amp-image-slider>
如果此页面上的解释没有涵盖您的所有问题,请随时与其他 AMP 用户联系,讨论您的确切使用案例。
转到 Stack Overflow 未解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的持续参与者,但我们也欢迎您针对您特别感兴趣的问题进行一次性贡献。
在 GitHub 上编辑示例-
作者: @kevinkassimo