【问题标题】:How can I get two UL's in table-cell-formatted DIV's to be flush against the top?如何让表格单元格格式的 DIV 中的两个 UL 与顶部齐平?
【发布时间】:2023-03-13 01:35:01
【问题描述】:

jQuery(function() {
  jQuery(".collection").sortable({});

  jQuery('li', jQuery('.collection')).draggable({
    'connectToSortable': '.collection',
    'cancel': 'a.ui-icon',
    'revert': 'invalid',
    'containment': 'document',
    'cursor': 'move'
  });
});
body {
  background-color: silver;
  font-family: Verdana, Georgia;
}

div.main {
  margin-left: auto;
  margin-right: auto;
  width: 740px;
}

div.table {
  display: table;
  width: 740px;
}

div.td {
  display: table-cell;
  margin-top: 0;
  width: 50%;
}

div.tr {
  display: table-row;
}

ul.collection {
  background-color: white;
  list-style: none;
  padding-left: 0;
  min-height: 100px;
  padding-left: 0;
  width: 200px;
}

ul.collection>li {}

ul#selection {
  right: 0;
}

ul#source {}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" type="text/css" href="/stylesheets/page.css" />
</head>

<body>
  <div class="main">
    <div class="table">
      <div class="tr">
        <div class="td">
          <ul id="source" class="collection">
            <li class="draggable">Everything</li>
            <li class="draggable">Nonfiction</li>
            <li class="draggable">Fiction</li>
            <li class="draggable">Poetry</li>
          </ul>
        </div>
        <div class="td">
          <ul id="selection" class="collection">
          </ul>
        </div>
      </div>
    </div>
    <script src="/javascripts/jquery.js"></script>
    <script src="/javascripts/jquery-ui.js"></script>
    <script>
      jQuery(function() {
        jQuery(".collection").sortable({});

        jQuery('li', jQuery('.collection')).draggable({
          'connectToSortable': '.collection',
          'cancel': 'a.ui-icon',
          'revert': 'invalid',
          'containment': 'document',
          'cursor': 'move'
        });
      });
    </script>
  </div>
</body>

</html>

此脚本旨在使 LI 在两个 UL 之间可拖动。运行时,左侧表格单元格样式 DIV 中填充的 UL 不会与表格样式 DIV 的顶部齐平;它的顶部似乎与右侧的空表格单元格的底部松散(或精确)对齐。初始图片,背景不同,如下:

如果两个 UL 至少有一个 LI,则它们会根据需要对齐到顶部齐平。如果最后一个 LI 被拖过去,可能会出现闪烁效果。所需的齐平外观是:

我想设置一些东西,使两个 UL 都位于模拟表格单元格的顶部,无论它们是填充的还是空的。

【问题讨论】:

    标签: jquery html css vertical-alignment css-tables


    【解决方案1】:

    通过在div.td 元素上使用vertical-align: top,垂直对齐 table-cells。这将告诉table-cell 坚持到顶部。

    请看下面的演示:

    jQuery(function() {
      jQuery(".collection").sortable({});
    
      jQuery('li', jQuery('.collection')).draggable({
        'connectToSortable': '.collection',
        'cancel': 'a.ui-icon',
        'revert': 'invalid',
        'containment': 'document',
        'cursor': 'move'
      });
    });
    body {
      background-color: silver;
      font-family: Verdana, Georgia;
    }
    
    div.main {
      margin-left: auto;
      margin-right: auto;
      width: 740px;
    }
    
    div.table {
      display: table;
      width: 740px;
    }
    
    div.td {
      display: table-cell;
      margin-top: 0;
      width: 50%;
      vertical-align: top;
    }
    
    div.tr {
      display: table-row;
    }
    
    ul.collection {
      background-color: white;
      list-style: none;
      padding-left: 0;
      min-height: 100px;
      padding-left: 0;
      width: 200px;
    }
    
    ul.collection>li {}
    
    ul#selection {
      right: 0;
    }
    
    ul#source {}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <!DOCTYPE html>
    <html>
    
    <head>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" type="text/css" href="/stylesheets/page.css" />
    </head>
    
    <body>
      <div class="main">
        <div class="table">
          <div class="tr">
            <div class="td">
              <ul id="source" class="collection">
                <li class="draggable">Everything</li>
                <li class="draggable">Nonfiction</li>
                <li class="draggable">Fiction</li>
                <li class="draggable">Poetry</li>
              </ul>
            </div>
            <div class="td">
              <ul id="selection" class="collection">
              </ul>
            </div>
          </div>
        </div>
        <script src="/javascripts/jquery.js"></script>
        <script src="/javascripts/jquery-ui.js"></script>
        <script>
          jQuery(function() {
            jQuery(".collection").sortable({});
    
            jQuery('li', jQuery('.collection')).draggable({
              'connectToSortable': '.collection',
              'cancel': 'a.ui-icon',
              'revert': 'invalid',
              'containment': 'document',
              'cursor': 'move'
            });
          });
        </script>
      </div>
    </body>
    
    </html>

    【讨论】:

    • 谢谢。我想得更远,如果单击,我希望 LI 切换到另一个 UL。最好使用 jQuery.click() 从其当前 UL 中删除 LI 并将其附加到另一个,或者是否有更好的方法与小工具到位? TIA,
    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2011-03-21
    • 2013-12-08
    相关资源
    最近更新 更多