简体   繁体   English

Javascript:无法使div正确对齐

[英]Javascript : Cant get divs to align properly

I am trying to create the charts displayed in 我正在尝试创建显示在

https://gist.github.com/maxkfranz/eb861f83fb741628342f https://gist.github.com/maxkfranz/eb861f83fb741628342f

so basically, i have a bootstrap container and 所以基本上,我有一个引导容器,

<div class="container">

    /*Other divs here*/

    <div id="cy" style=" height: 50%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background-color: #FAEDEF;"></div>

                <div id="cy2" style=" height: 50%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 50%;
  background-color: #EDF1FA;
  border-top: 1px solid #ccc;"></div>
</div>

and here are my head elements 这是我的头

  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="http://code.highcharts.com/highcharts.js"></script>
  <script src="http://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min.js"></script>

and here is the javascript 这是javascript

$(document).ready(function() {
var elesJson = {
  nodes: [
    { data: { id: 'a', foo: 3, bar: 5, baz: 7 } },
    { data: { id: 'b', foo: 7, bar: 1, baz: 3 } },
    { data: { id: 'c', foo: 2, bar: 7, baz: 6 } },
    { data: { id: 'd', foo: 9, bar: 5, baz: 2 } },
    { data: { id: 'e', foo: 2, bar: 4, baz: 5 } }
  ], 

  edges: [
    { data: { id: 'ae', weight: 1, source: 'a', target: 'e' } },
    { data: { id: 'ab', weight: 3, source: 'a', target: 'b' } },
    { data: { id: 'be', weight: 4, source: 'b', target: 'e' } },
    { data: { id: 'bc', weight: 5, source: 'b', target: 'c' } },
    { data: { id: 'ce', weight: 6, source: 'c', target: 'e' } },
    { data: { id: 'cd', weight: 2, source: 'c', target: 'd' } },
    { data: { id: 'de', weight: 7, source: 'd', target: 'e' } }
  ]
};

$('#cy').cytoscape({
  style: cytoscape.stylesheet()
    .selector('node')
      .css({
        'background-color': '#B3767E',
        'width': 'mapData(baz, 0, 10, 10, 40)',
        'height': 'mapData(baz, 0, 10, 10, 40)',
        'content': 'data(id)'
      })
    .selector('edge')
      .css({
        'line-color': '#F2B1BA',
        'target-arrow-color': '#F2B1BA',
        'width': 2,
        'target-arrow-shape': 'circle',
        'opacity': 0.8
      })
    .selector(':selected')
      .css({
        'background-color': 'black',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black',
        'opacity': 1
      })
    .selector('.faded')
      .css({
        'opacity': 0.25,
        'text-opacity': 0
      }),

  elements: elesJson,

  layout: {
    name: 'circle',
    padding: 10
  },

  ready: function(){

  }
});

$('#cy2').cytoscape({
  style: cytoscape.stylesheet()
    .selector('node')
      .css({
        'background-color': '#6272A3',
        'shape': 'rectangle',
        'width': 'mapData(foo, 0, 10, 10, 30)',
        'height': 'mapData(bar, 0, 10, 10, 50)',
        'content': 'data(id)'
      })
    .selector('edge')
      .css({
        'width': 'mapData(weight, 0, 10, 3, 9)',
        'line-color': '#B1C1F2',
        'target-arrow-color': '#B1C1F2',
        'target-arrow-shape': 'triangle',
        'opacity': 0.8
      })
    .selector(':selected')
      .css({
        'background-color': 'black',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black',
        'opacity': 1
      }),

  elements: elesJson,

  layout: {
    name: 'breadthfirst',
    directed: true,
    padding: 10
  },

  ready: function(){
    // ready 2
  }
});
  });

the problem is that when i open up the page, the two divs cy and cy2 cover up the entire page! 问题是当我打开页面时,两个div cycy2覆盖整个页面! meaning all the other divs are overwritten. 表示所有其他div被覆盖。

Does this have something to do with posiiton:absolute ? 这与posiiton:absolute吗? i tried to change it to relative, but if i do that then cy and cy2 just appear as very thin lines! 我试图将其更改为相对,但是如果我这样做,则cycy2只会显示为细线!

I am new to css and i dont know how to configure the style so that the charts appear properly. 我是CSS的新手,我不知道如何配置样式,以便图表正确显示。

Any help would be much appreciated. 任何帮助将非常感激。

The problem is that you're using height: 50% , but there's nothing for it to be 50% of. 问题是您使用的是height: 50% ,但是没有什么是50%的高度。 When you set the divs to position: absolute , they're getting 50% of the window height. 当您将div设置为position: absolute ,它们将获得窗口高度的50%。 If you can set a fixed height for them, you should be all set. 如果可以为它们设置一个固定的高度,则应该全部设置好。

 <div id="cy" style="height: 400px;
   width: 100%;
   background-color: #FAEDEF;"></div>

 <div id="cy2" style=" height: 400px;
   background-color: #EDF1FA;
   border-top: 1px solid #ccc;"></div>

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

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