vue.js - Dynamically assign styles to DOM Element in Polymer 3.0
问题描述
I'm building a component in Polymer 3.0 for the first time, and I was wondering how to dynamically assign styles to a DOM element. I am building a horizontal timeline of events, and I would like to place vertical markers at every year in set range. In Vue.js
, I would do something like this:
<year-marker v-for="(year, index) in years" :style="getPercentFromLeft(year)" :key="index"></year-marker>
In this setup, I would have a data structure with all the years that I want to display, and the v-for would loop through that data structure and render a markers for every year.
The getPercentFromLeft
function would calculate the percent from the left side of the screen that the marker should be positioned. The return value is in the format of {left: 10%}
. The year-marker
has additional styles, but they are specified in the style section.
What is the equivalent way to do this in Polymer 3.0
? If there isn't one, what is a better way to do this?
解决方案
在 polymer3 中,您可以使用以下代码来实现。迭代您的数据结构并计算每个项目的样式。
<dom-repeat items="{{years}}">
<template>
<paper-item class$={{getPercentFromLeft(year)}}>{{item}}</paper-item>
</template>
</dom-repeat>
将为每个项目(年份)调用 getPercentFromLeft,您可以返回所需的类。
推荐阅读
- php - Laravel 相对临时签名路由签名始终无效
- android - 无法通过 Intent 从子活动(谷歌地图)传递价值到父活动
- reactjs - 如何使用 useState 钩子解决随机数组问题(React useState)?
- vba - 如何在添加记录之前检查表中的现有列?
- boolean - 如何设置字段私有布尔值设置为真?
- c# - 是否可以将 HttpResponseMessage json 转换为对象?
- android - 我在 recyclerview 中的适配器更改了 2 个项目而不是 1 个项目的背景颜色
- sql - 将外部数据添加到 sql 表
- javascript - 在表格中拖动图像 - 修复大小和每个图像
- machine-learning - 为什么我的变分自编码器无法学习