【问题标题】:Canvas dimensions based on outer div measurements (js?)基于外部 div 测量值的画布尺寸(js?)
【发布时间】:2013-03-28 02:13:59
【问题描述】:

我的布局有点噩梦。几天来我一直在尝试自己修复它,但现在它变成了这个......

http://jsfiddle.net/Osceana/yQ3As/2/

我只是想让画布跨越该时间线的整个长度,并且我希望它在“所有站点”div 上方结束,无论它移动到哪个窗口。我似乎无法做到这一点。我在javascript中尝试过,但无济于事。

$("#chart").height(
canvasHeight);

$("#chart").width(
timelineWidth);

附:时间线应该有一个我在 js 中实现的左边距:

$("#timeline").css(
    "margin-left", namesWidth);

^ 这出现在我的项目中,但由于某种原因没有出现在 jsfiddle 上。但是在我的项目中,左边距似乎导致水平溢出。我只想让时间轴移动到足以容纳名称的位置,然后它跨越屏幕水平长度的整个其余部分。我将 CSS 的宽度设置为 100%、92% 等,但结果总是很奇怪。是因为左边距百分比不起作用吗?

时间线 CSS:

    #timeline {
    font-size:15px;
    color:black;
    font-family:Calibri;
    text-align:center;
    margin-top:60px;
    width:100%;
}
    #timeline td {
    width: 4%;
}

时间线js:

var namesWidth = $("#names").width()

$("#timeline").css(
    "margin-left", namesWidth);

*感谢您非常提供任何和所有的见解。我希望我没有要求太多。

【问题讨论】:

    标签: html layout canvas html5-canvas


    【解决方案1】:

    Live Demo

    Editor

    你没有设置你的小提琴来使用 jQuery。下面应该可以让你接近你需要的东西。

    var $chart = $("#chart"),
        spacing = 40,
        canvasHeight = $("#company").position().top - $chart.position().top - spacing,
        windowWidth = $(window).width(),
        namesWidth = $("#names").width();
    
    $chart.width($("#timeline").width());
    $chart.height(canvasHeight);
    
    $("#timeline").css("margin-left", namesWidth);
    $chart.css("margin-left", namesWidth);
    
    // change on resize
    $(window).resize(function () {
        canvasHeight = $("#company").position().top - $chart.position().top - spacing;
        $chart.height(canvasHeight);
    });
    

    【讨论】:

    • 您好,感谢您的回复!愚弄我,忘记打开 jQuery,谢谢。这些更改看起来很简单,但是我注意到画布宽度仍然低于时间线 div。如果您一直展开 Result 窗口,它会在晚上 9:00 左右停止,它似乎也不完全从第一个 (上午 12:00)开始。此外,画布在页面加载时显示为 3 或 4 像素粗线。如果我调整窗口大小并重新最大化它,它会再次伸展。有任何想法吗?再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多