返回信息流RT,本人js基本小白,最近做的一个项目需要把一段js代码打包成一个xx.js文件从而可以使用“src=xx.js”的方式进行引用,但是不太会,求大神解答一下如何封装,以下是我的代码,这段代码是用d3.js画一个柱状图的:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: orange;
}
.bar:hover {
fill: orangered ;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.min.js"></script>
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 480 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Percent:</strong> <span style='color:red'>" + d.percent + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
d3.csv("bar.csv", type, function(error, data) {
x.domain(data.map(function(d) { return d.attribute; }));
y.domain([0, d3.max(data, function(d) { return d.percent; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Percent");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.attribute); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.percent); })
.attr("height", function(d) { return height - y(d.percent); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
function type(d) {
d.percent = +d.percent;
return d;
}
</script>
</body>
</html>
thksthkshtkshtksthks!
这是一条镜像帖。来源:北邮人论坛 / www-technology / #22127同步于 2013/11/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖
求问js封装的问题
yyfs
2013/11/21镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
简单说就是把从63行开始到134行结束的代码单独放在xx.js里面,然后像59和60行一样引用这个文件。
另外自己的文件使用相对或者绝对路径,不用uri
那对于<style>那段呢?是不是可以写入到一个css文件里?
【 在 rhj1122 的大作中提到: 】
: 简单说就是把从63行开始到134行结束的代码单独放在xx.js里面,然后像59和60行一样引用这个文件。
: 另外自己的文件使用相对或者绝对路径,不用uri
你nodejs搞得咋样了
【 在 rhj1122 的大作中提到: 】
: 简单说就是把从63行开始到134行结束的代码单独放在xx.js里面,然后像59和60行一样引用这个文件。
: 另外自己的文件使用相对或者绝对路径,不用uri
把js部分找个xx.js文件放进去,然后在原html中加<script src="xx.js"></script>加入位置可以在</head>或</body>前面 ,然后就可以了吧
【 在 yyfs 的大作中提到: 】
: 那对于<style>那段呢?是不是可以写入到一个css文件里?
发自「贵邮」