gpt4 book ai didi

Javascript:如何覆盖整个区域?

转载 作者:行者123 更新时间:2023-11-29 10:29:18 24 4
gpt4 key购买 nike

我是第一次使用 SVG 编写代码。我用 javascript 创建了一个小程序。该矩形不是从该区域的底部完美开始的,仍然是一条浅蓝色带。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#graphicArea {
width: 1400px;
background: #a7def2;
}
</style>
</head>
<body>
<div id="outer-wrapper">
<div id="graphicArea"> </div>
</div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var width = 1400;
var height = 600;
var graphic;
var gocceAlSec = 7;
graphic = d3.select("#graphicArea").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "graphic")
.attr("overflow", "hidden");

var dataset = [0];

graphic.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", 0)
.attr("y", 600)
.attr("width", 1400)
.attr("height", 0)
.style("fill", "blue")
.transition()
.duration(50000)
.attr("height", 600)
.attr("y", 0);

</script>
</body>

最佳答案

您正在将背景颜色设置为 <div> ,因此你必须处理默认边距、填充、计算高度等......

一种更简单的方法是将背景颜色设置为 SVG:

graphic = d3.select("#graphicArea").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "graphic")
.style("background", "#a7def2")

这里是你的代码有那个变化:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="outer-wrapper">
<div id="graphicArea"> </div>
</div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var width = 1400;
var height = 600;
var graphic;
var gocceAlSec = 7;
graphic = d3.select("#graphicArea").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "graphic")
.style("background", "#a7def2")
.attr("overflow", "hidden");

var dataset = [0];

graphic.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", 0)
.attr("y", 600)
.attr("width", 1400)
.attr("height", 0)
.style("fill", "blue")
.transition()
.duration(50000)
.attr("height", 600)
.attr("y", 0);

function makeRain() {

for (var i = 0; i < gocceAlSec; i++) {
startX = Math.random() * width,
startY = Math.random() * 100 - 100,
endX = startX;
endY = height + 200;
graphic.insert("circle")
.attr("cx", startX)
.attr("cy", startY)
.attr("r", 2)
.style("fill", "blue")
.transition()
.duration(2000)
.attr("cx", endX + 100)
.attr("cy", endY)
.remove();
};
}

d3.timer(makeRain, 100);
</script>
</body>

如果你想坚持使用 <div>风格你可以尝试一些改变,比如max-heigh: 600px; .


PS:由于这是您的第一个 D3/SVG 代码(顺便说一句,荣誉),这里有一个提示:您不需要为矩形输入选择,不仅因为它只有一个,而且主要是因为数据是没有意义的。只需将元素附加到容器即可。

关于Javascript:如何覆盖整个区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423960/

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