如何使用带有 Django 模板标签的 amp CSS 星级评分?

Posted

技术标签:

【中文标题】如何使用带有 Django 模板标签的 amp CSS 星级评分?【英文标题】:How to use Star rating for amp CSS with Django template Tag? 【发布时间】:2019-01-12 15:46:09 【问题描述】:

我只想使用 django 模板标签根据数据库中可用的值在 amp 页面上显示星级。我该怎么做,因为我的评分是浮点类型,而且该代码也可以用于非放大器页面。

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>



<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://***.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>

该网页由几个产品组成,其星级评分需要显示在其卡片 div 中。

【问题讨论】:

【参考方案1】:

您不需要使用上面的代码来显示星级,因为它用于接受星级。由于 amp 页面不支持 js,所以可以借助 css 来实现。您需要做的是放置一个空白星级图像,然后将其与完全相同大小的填充图像重叠,并使用带有 django 模板标签的剪辑来显示它。

<img style="position: absolute;clip: rect(0px,i.ratingpx,0px,0px);top: 0px;left: 0px" src="https://filled-up-star.png"/>

【讨论】:

以上是关于如何使用带有 Django 模板标签的 amp CSS 星级评分?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Django 模板中的“带有模板标签”中设置 Django 渲染块值?

Django 模板未正确格式化 <pre> 标签

在视图而不是模板中生成带有 Django 静态 url 的图像标签

使用带有特殊字符的 Django 的 url 模板标签

如何将javascript变量传递给django模板标签

带有“带有模板标签”方法的 django 静态文件