简体   繁体   English

Bootstrap获取div列的宽度(以像素为单位)

[英]Bootstrap get width of div column in pixels

I'm setting up a page with bootstrap. 我正在设置一个带引导程序的页面。 I have the layout working perfectly but one of the elements is a zoomable map of the US (using d3). 我的布局工作得很完美,但其中一个元素是美国的可缩放地图(使用d3)。 The zoom function I am using requires the width and height of the div in pixels in order to calculate how to translate and scale the map. 我正在使用的缩放功能需要div的宽度和高度(以像素为单位),以便计算如何平移和缩放地图。 I have tried using percentages but I can't get anything going that way. 我已经尝试过使用百分比,但我无法做到这一点。 Is there any way to dynamically get the height and width of the div. 有没有办法动态获取div的高度和宽度。 I have searched all over but the search terms are too generic (or I'm not clever enough to phrase it correctly). 我已经搜索了所有搜索条件过于通用(或者我不够聪明,无法正确地说出来)。

Alternatively, how else might I get the necessary values. 或者,如何获得必要的值。

Here is my implementation using hard coded width and height (which won't work if the page resizes). 这是我使用硬编码宽度和高度的实现(如果页面调整大小,则不起作用)。

//make the map element
    var width = 1000;
    var height = 1000;
    var svg = d3.select("#Map")
    .append("svg")
    .attr("id", "chosvg")
    .attr("height", height)
    //.attr("viewBox", "0 0 600 600")
    .attr("width", width)
    .style("preserveAspectRatio", "true");

    cbsa = svg.append("g");
d3.json("data/us-cbsa.json", function(json) {
    cbsa.selectAll("path")
        .attr("id", "cbsa")
        .data(json.features)
        .enter()
        .append("path") 
        .attr("class", data ? quantize : null) //data ? value_if_true : value_if_false -- ternary operator
        .attr("d", path)
        .on("click", clicked);
});

in the clicked() function, I have the zoom like this which works, but only with a certain window width 在clicked()函数中,我有这样的缩放工作,但只有一定的窗口宽度

        cbsa.transition()
        .duration(750)
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
        .style("stroke-width", 1.5 / k + "px");

For clarity, I am ideally loooking for something like: var width = column.width() //or something using percentages 为清楚起见,我理所当然地喜欢以下内容:var width = column.width()//或使用百分比的东西

I can include my html as well if it helps. 如果它有帮助,我也可以包含我的HTML。

You can get the width of the column by calling: 您可以通过调用以获取列的宽度:

var bb = document.querySelector ('#Map')
                    .getBoundingClientRect(),
       width = bb.right - bb.left;

Depending on the browser, the bb might already have an width property. 根据浏览器的不同, bb可能已经有了width属性。 Keep in mind that the column might appear wider because the initial size of the svg is too big, so its parent column might bee, too. 请记住,列可能看起来更宽,因为svg的初始大小太大,因此其父列也可能是蜜蜂。

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM