gpt4 book ai didi

javascript - 点击时更改颜色的铁图标

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:57 24 4
gpt4 key购买 nike

我有一个 very basic example我想在点击时更改铁图标的颜色。

我使用铁选择器进行攻丝:

<template>

<style>

:host {
display: block;
padding: 10px;
--iron-icon-fill-color: blue;
};

.iron-selected iron-icon {
--iron-icon-fill-color: red;
};

.iron-selected iron-icon {
fill: purple;
}

</style>

<iron-meta id=meta key="info" value="some"></iron-meta>

<iron-meta id=meta key="sub">
<a href="some"></a>
</iron-meta>

<iron-selector id="one" selected="0">
<div><iron-icon icon="android" class="ico"></iron-icon></div>
<div><iron-icon icon="menu" class="ico"></iron-icon></div>
</iron-selector>


</template>

我之前被告知这是 shim 的一个限制,其中每个元素范围只能保留一个自定义值。

编写一个“直接”选择器(参见“粉红色”的变化)确实可行。

现在...如果我想使用组件提供的变量 (--iron-icon-fill-color) 而不是直接操作 CSS,我需要写什么? (或者,这实际上是不可能的吗?)

最佳答案

更新:新发布的 Polymer 1.6.0支持 native CSS 属性(无垫片),因此您的代码可以在没有 updateStyles() 的情况下工作.

要启用 native CSS 属性,请初始化 Polymer导入前的变量 polymer.html :

<script>Polymer = {lazyRegister: true, useNativeCSSProperties: true};</script>

HTMLImports.whenReady(_ => {
"use strict";

Polymer({
is: 'x-foo'
});
});
<head>
<base href="https://polygit.org/polymer+1.6.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>

<!-- Enable native CSS properties (instead of shim) -->
<script>Polymer = {lazyRegister: true, useNativeCSSProperties: true};</script>
<link rel="import" href="polymer/polymer.html">

<link rel="import" href="iron-selector/iron-selector.html">
<link rel="import" href="iron-icons/iron-icons.html">
</head>

<body>
<x-foo></x-foo>

<dom-module id="x-foo">
<template>
<style>
:host{
--iron-icon-fill-color: blue;
}
.iron-selected {
background: #eee;
}
.iron-selected iron-icon {
--iron-icon-fill-color: pink;
}
</style>

<iron-selector selected="0">
<div>
<iron-icon icon="android" class="ico"></iron-icon>
</div>
<div>
<iron-icon icon="menu" class="ico"></iron-icon>
</div>
</iron-selector>
</template>
</dom-module>
</body>

codepen


来自 Polymer 1.5.0 docs (强调我的):

Polymer's custom property shim evaluates and applies custom property values once at element creation time.

由于您正在使用自定义属性动态更新样式(即,在创建元素之后),因此必须通过调用 updateStyles() 重新应用自定义属性。 .你可以听<iron-selector> iron-select 触发此更新的事件。

// template
<iron-selector on-iron-select="_updateStyles">

// script
Polymer({
...
_updateStyles: function() {
this.updateStyles();
}
});

HTMLImports.whenReady(_ => {
"use strict";

Polymer({
is: 'x-foo',
_updateStyles: function() {
this.updateStyles();
}
});
});
<head>
<base href="https://polygit.org/polymer+1.5.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-selector/iron-selector.html">
<link rel="import" href="iron-icons/iron-icons.html">
</head>

<body>
<x-foo></x-foo>

<dom-module id="x-foo">
<template>
<style>
:host{
--iron-icon-fill-color: blue;
}
.iron-selected {
background: #eee;
}
.iron-selected iron-icon {
--iron-icon-fill-color: pink;
}
</style>

<iron-selector selected="0" on-iron-select="_updateStyles">
<div>
<iron-icon icon="android" class="ico"></iron-icon>
</div>
<div>
<iron-icon icon="menu" class="ico"></iron-icon>
</div>
</iron-selector>
</template>
</dom-module>
</body>

codepen

关于javascript - 点击时更改颜色的铁图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38158428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com