gpt4 book ai didi

javascript - 在 svg 元素内移动

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

我正在尝试在 SVG 图形上叠加一个元素。为了简单起见,我将其重新创建为一个带有文本覆盖的矩形。我将对文本进行一些非常严格的格式设置和样式设置,但现在我想弄清楚为什么我不能将它放在页面的下方。我试过更改元素上的“x”和“y”,但它根本不动。谁能帮忙?我想将文本移到黄色 SVG 矩形的下方。

完整代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>Title Here</title>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<style type="text/css">

ul {
position:absolute;
}
</style>
</head>

<body>
<p>Click me</p>
<ul><text x="40" y="90">Test</text></ul>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
var padding = 30;


//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)

svg.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "yellow");

</script>
</body>

</html>

最佳答案

text 是一个 svg 元素,必须包含在 svg 标签中。

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>Title Here</title>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<style type="text/css">
ul {
position: absolute;
padding: 0;
}
</style>
</head>

<body>
<p>Click me</p>
<ul>
<svg width="500" height="300" viewBox="0 0 500 300">
<text x="40" y="270">Test</text>
</svg>
</ul>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
var padding = 30;


//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)

svg.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "yellow");
</script>
</body>

</html>

关于javascript - 在 svg 元素内移动 <text>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33487196/

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