使用D3.js从元素获取宽度 [英] Get width from element using D3.js

查看:807
本文介绍了使用D3.js从元素获取宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html:

<div class="bar no" style="width: 25.0916%;"></div>

我想以数字或百分比的形式访问宽度尺寸。

I'd like to access the width size as a number or percentage.

我尝试了以下一些,但第一个没有返回任何内容,第二个返回数字。

I've tried some the following but the first returns nothing and the second returns the number in pixels.

width = this.getBBox().width

width = d3.select(this).style("width")

我可以用什么来返回500或25.0%?

What can I use to return 500 or 25.0%?

推荐答案

比较以下内容:

console.log('Computed style width as pixels: ');
console.log(d3.select('.bar').style('width'));
console.log('Computed style width as a number: ');
console.log(parseFloat(d3.select('.bar').style('width')));
console.log('As a percent: ');
console.log(d3.select('.bar').node().style.width);
console.log('Actual width as number: ');
console.log(d3.select('.bar').node().offsetWidth);

<script src="https://d3js.org/d3.v4.js"></script>
<div class="bar no" style="width: 25.0916%;"></div>

这篇关于使用D3.js从元素获取宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆