【问题标题】:Expand TH Element on Row Hover在行悬停时展开 TH 元素
【发布时间】:2019-09-12 00:01:31
【问题描述】:

我有一个表格,当单元格悬停时突出显示行和列。

这是实际操作中的一个小技巧: https://jsfiddle.net/cbsuwz5y/4/

控制dom的jquery在这里:

$("table").on('mouseover mousedown mouseleave', 'td', function (e) {
                    if (e.type == 'mouseover' || e.type == 'mousedown') {
                        $(this).parent().addClass("hover");
                        $(this).closest('table').find("col").eq($(this).index()).addClass("hover");
                    } else {
                        $(this).parent().removeClass("hover");
                        $(this).closest('table').find('col').eq($(this).index()).removeClass("hover");

                    }
                });

我想要的是,当单元格悬停时,TH 元素会展开。我正在尝试通过应用这个 css 类来做到这一点:

.applied-css {
    display: block;
    position: absolute;
    top: -60px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    width: 85px;
    height: 85px;
    background: white;
    z-index: 1;
}

并使用这行 JS:

$(this).closest('table').find("col").eq($(this).index()).find(".div-default").addClass("applied-css");

谁能指出我正确的方向来让它工作?

我应该补充一点,当我说“扩展”TH 时,我基本上是在假装扩展的样子。我使用 CSS 在 TH 上覆盖一个大于 TH 正方形的正方形。这会稍微切断它旁边的单元格,但我可以接受。当我在 CSS 中使用 :hover 悬停实际 TH 时,我可以让它工作。但是当我将一个单元格悬停在表格上时,我想让它在 jquery 中工作以应用它。

【问题讨论】:

  • 也许这里也包含相关的 HTML?
  • 您的小提琴不会尝试应用 applied-css 类。你能提供一个minimal reproducible example吗?
  • 您能具体说明您遇到问题的部分吗?鉴于您提供的内容,您似乎可以很容易地将.applied-css 应用到正确的位置。是css还是jquery? jsfiddle.net/ucpw0fxL
  • 使用 jquery(类似于上面),我在定位突出显示的行/列 TD 元素时遇到问题,然后将我的 CSS 应用于与其对应的 TH 元素。我会再做几个小提琴来展示我的尝试。
  • $(this).closest('table').find("tr:first").find("th").eq($(this).index())(见我之前的小提琴)

标签: jquery html css html-table


【解决方案1】:

这就是我要做的。在您希望内容扩展的地方,我将添加一个包含我要显示的内容的子层,如下所示:

<div class="expand hidden">
    test 1
</div>

我会将您的悬停行为更新为:

$("table").on('mouseover mousedown mouseleave','td', function(e) {
  if (e.type == 'mouseover' || e.type == 'mousedown') {
  $(this).parent().addClass("hover");
  $(this).closest('table').find("col").eq($(this).index()).addClass("hover");

  $(this).closest('table').find('thead > tr > th').eq($(this).index()).children(".expand").removeClass("hidden");
 } else {
  $(this).parent().removeClass("hover");
  $(this).closest('table').find('col').eq($(this).index()).removeClass("hover");

  $(this).closest('table').find('thead > tr > th').eq($(this).index()).children(".expand").addClass("hidden");
 }
});

我在这里所做的是在鼠标悬停时删除“隐藏”类并在鼠标悬停时添加它。

css 的最后一个:

th {
  border: 1px solid #ff0000;
  position: relative;
}
.expand {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
  background-color: #fff;
  width: 300px;
  height: 40px;
  transition: all 0.4s;
  overflow: hidden;
}
.hidden {
  width: 0;
  height: 0;
  visibility:hidden;
}

希望这会有所帮助。

【讨论】:

  • OP 希望扩展列的相关标题,而不是单元格本身,尽管它非常相似,只是定位略有不同。
  • 这是为我做的。非常感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多