星级评分
简介
此星级评分小部件仅使用 CSS 实现,因为 AMP 对自定义 JavaScript 有限制。它仍然具有星级评分组件的关键功能
-
触摸、鼠标和键盘可访问性
-
当用户将鼠标悬停在星星上时,星星会改变颜色
-
一旦做出选择,它就会“保持”
-
简洁的可缩放矢量图标
-
屏幕阅读器友好
设置
导入 amp-form
组件。
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
我们还需要 amp-mustache
组件。
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
星级评分的 CSS
此星级评分实现将“☆”Unicode 字符精确地覆盖在单选按钮上。下面的内联注释详细描述了规则。
<style amp-custom>
.rating {
--star-size: 3; /* use CSS variables to calculate dependent dimensions later */
padding: 0; /* to prevent flicker when mousing over padding */
border: none; /* to prevent flicker when mousing over border */
unicode-bidi: bidi-override; direction: rtl; /* for CSS-only style change on hover */
text-align: left; /* revert the RTL direction */
user-select: none; /* disable mouse/touch selection */
font-size: 3em; /* fallback - IE doesn't support CSS variables */
font-size: calc(var(--star-size) * 1em); /* because `var(--star-size)em` would be too good to be true */
cursor: pointer;
/* disable touch feedback on cursor: pointer - http://stackoverflow.com/q/25704650/1269037 */
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-tap-highlight-color: transparent;
margin-bottom: 1em;
}
/* the stars */
.rating > label {
display: inline-block;
position: relative;
width: 1.1em; /* magic number to overlap the radio buttons on top of the stars */
width: calc(var(--star-size) / 3 * 1.1em);
}
.rating > *:hover,
.rating > *:hover ~ label,
.rating:not(:hover) > input:checked ~ label {
color: transparent; /* reveal the contour/white star from the HTML markup */
cursor: inherit; /* avoid a cursor transition from arrow/pointer to text selection */
}
.rating > *:hover:before,
.rating > *:hover ~ label:before,
.rating:not(:hover) > input:checked ~ label:before {
content: "★";
position: absolute;
left: 0;
color: gold;
}
.rating > input {
position: relative;
transform: scale(3); /* make the radio buttons big; they don't inherit font-size */
transform: scale(var(--star-size));
/* the magic numbers below correlate with the font-size */
top: -0.5em; /* margin-top doesn't work */
top: calc(var(--star-size) / 6 * -1em);
margin-left: -2.5em; /* overlap the radio buttons exactly under the stars */
margin-left: calc(var(--star-size) / 6 * -5em);
z-index: 2; /* bring the button above the stars so it captures touches/clicks */
opacity: 0; /* comment to see where the radio buttons are */
font-size: initial; /* reset to default */
}
form.amp-form-submit-error [submit-error] {
color: red;
}
</style>
用法
虽然可以通过键盘访问此星级评分小部件,但并非没有怪癖
- 单击/点击小部件,然后按向上/向下/向左/向右箭头。不幸的是,在 Firefox 和 Safari 中,键盘箭头向后工作。这个问题似乎在不使用 JavaScript 的情况下,无法在非基于 Chrome 的浏览器上修复,因此请继续关注 amp-rating 组件。
- 将鼠标悬停在星星上以查看样式变化。
我们将使用一组单选按钮来获取用户对星级评分的输入,因为它们可以通过键盘访问。对于桌面,当用户将鼠标悬停在星星上时,更改先前星星的样式会很好。影响鼠标悬停时先前元素样式的唯一纯 CSS 方法是按相反的 DOM 顺序列出它们,这就是为什么下面的 <input>
元素从 5 到 1 运行的原因。
我们希望表单在用户做出选择后立即提交,而无需提交按钮。为此,我们将 input
的 on
属性设置为在 change
时提交表单。
初始评级由设置了 checked
属性的单选按钮确定。这是可选的。
<form id="rating" method="post" action-xhr="https://amp.org.cn/documentation/examples/interactivity-dynamic-content/star_rating/set" target="_blank">
<fieldset class="rating">
<input name="rating" type="radio" id="rating5" value="5" on="change:rating.submit">
<label for="rating5" title="5 stars">☆</label>
<input name="rating" type="radio" id="rating4" value="4" on="change:rating.submit">
<label for="rating4" title="4 stars">☆</label>
<input name="rating" type="radio" id="rating3" value="3" on="change:rating.submit">
<label for="rating3" title="3 stars">☆</label>
<input name="rating" type="radio" id="rating2" value="2" on="change:rating.submit" checked="checked">
<label for="rating2" title="2 stars">☆</label>
<input name="rating" type="radio" id="rating1" value="1" on="change:rating.submit">
<label for="rating1" title="1 stars">☆</label>
</fieldset>
<div submit-success>
<template type="amp-mustache">
<p>Thanks for rating {{rating}} star(s)!</p>
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Looks like something went wrong. Please try to rate again. {{error}}
</template>
</div>
</form>
需要进一步解释?
如果本页上的解释没有涵盖您所有的问题,请随时与其他 AMP 用户联系,讨论您的具体用例。
前往 Stack Overflow 未解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的持续参与者,但我们也欢迎您为特别热衷的问题做出一次性贡献。
在 GitHub 上编辑示例-
作者: @sebastianbenz