【发布时间】:2021-11-03 13:19:06
【问题描述】:
我正在尝试转换组 svg 元素的 transform 属性。 (我可以对圆本身应用过渡,它会起作用,但在实际项目中,我有一个带路径的组,所以我必须对组元素应用变换)。
它在 Chrome 中运行良好,但在 Firefox 或 Safari 中无法运行。
我已经阅读了我能找到的所有内容,并认为这是一个解决方案:
-webkit-transition: -webkit-transform 1s linear;
-moz-transition: -moz-transform 1s linear;
-o-transition: -o-transform 1s linear;
transition: transform 1s linear;
但它对我不起作用。
完整代码示例here也。
<script>
import { scaleTime, scaleLinear, extent, max, timeFormat,scaleBand} from 'd3';
import { fade, fly } from 'svelte/transition';
let points = []
const height = 500;
const xTicks = [1990, 1995, 2000, 2005, 2010, 2015];
const yTicks = [0, 5, 10, 15, 20];
const padding = { top: 20, right: 15, bottom: 20, left: 25 };
$: years = points.map(d => d.year)
let selectedY = 'birthrate';
$: xScale = scaleBand()
.domain(years)
.range([padding.left, 500 - padding.right])
.padding(0.2);
$: yScale = scaleLinear()
.domain([0, 20])
.range([500 - padding.bottom, padding.top]);
$: innerWidth = 500 - (padding.left + padding.right);
$: barWidth = innerWidth / xTicks.length;
$: points = points.map(d => ({ ...d, birthrateNew: 15 }))
function updateData() {
points = [
{ id: 1, year: 1990, birthrate: 16.7 },
{ id: 2, year: 1995, birthrate: 14.6 },
{ id: 3, year: 2000, birthrate: 14.4 },
{ id: 4, year: 2005, birthrate: 14 },
{ id: 5, year: 2010, birthrate: 13 },
{ id: 6, year: 2015, birthrate: 12.4 }
];
}
function updateData2() {
selectedY = 'birthrateNew'
}
$: calcData = points.map(d => {
return {
x: xScale(d.year),
y: yScale(d[selectedY]),
};
});
</script>
<svg width="500" height="500">
{#each calcData as d}
<g in:fade="{{delay: d.x*2}}" transform="translate({d.x}, {d.y})">
<circle
cx={8}
cy=23
r= {3}
stroke='#000'
fill= '#fff'
stroke-width='2'
></circle>
</g>
{/each}
</svg>
<button on:click={updateData}>
click me
</button>
<button on:click={updateData2}>
click me
</button>
<style>
g {
-webkit-transition: -webkit-transform 1s linear;
-moz-transition: -moz-transform 1s linear;
-o-transition: -o-transform 1s linear;
transition: transform 1s linear;
}
</style>
谢谢!
【问题讨论】:
标签: css animation svg d3.js svelte