Mar*_*rra 2 javascript css jquery flexbox
我构建了一个 flexbox 日历布局,其中包含悬停时的 CSS 工具提示。我正在尝试将这些工具提示与它们对应的日历日期垂直对齐。
我想在 CSS 中实现这一点,但一直无法实现。目前,我已经在 jQuery 中设置了一个left值4em并动态计算了该top值,因为这些工具提示的高度可能会有所不同。即使这样做,工具提示也没有完全垂直对齐,我不知道为什么。
这是我的jQuery功能:
$('.cal-tooltip').each(function() {
var calTooltipHeight = $(this).outerHeight(true) / 2;
$(this).css('top', -calTooltipHeight);
});
Run Code Online (Sandbox Code Playgroud)
CSS:
.tooltip {
position: absolute;
z-index: 1;
background-color: white;
box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.2);
}
.tooltip:after {
content: '';
position: absolute;
width: 0;
height: 0;
margin-left: -15px;
margin-top: -15px;
top: 50%;
box-sizing: border-box;
border: 15px solid black;
border-color: transparent transparent white white;
transform-origin: 50% 50%;
box-shadow: -5px 5px 10px 0 rgba(0, 0, 0, 0.1);
}
.tooltip > div {
padding: 15px;
}
.tooltip > div:not(:first-child) {
padding-top: 5px;
}
.tooltip > div img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
.tooltip-left:after {
left: 0;
transform: rotate(45deg);
}
.tooltip-right:after {
left: 100%;
transform: rotate(-135deg);
}
#event-calendar .cal-tooltip {
display: none;
width: 20em;
cursor: auto;
}
#event-calendar .cal-tooltip .cal-tooltip-cont {
display: flex;
}
#event-calendar .cal-tooltip .cal-tooltip-cont > div {
flex-basis: 50%;
}
#event-calendar .cal-tooltip .cal-tooltip-cont > div:nth-child(1) {
padding-right: 7.5px;
}
#event-calendar .cal-tooltip .cal-tooltip-cont > div:nth-child(2) {
padding-left: 7.5px;
}
#event-calendar .cal-tooltip .cal-tooltip-cont > div h2,
#event-calendar .cal-tooltip .cal-tooltip-cont > div h3 {
font-size: 0.75em;
}
#event-calendar .cal-tooltip .cal-tooltip-cont > div h2 {
padding-bottom: 5px;
}
#event-calendar .tooltip-left {
left: 4em;
}
#event-calendar .tooltip-right {
right: 4em;
}
Run Code Online (Sandbox Code Playgroud)
演示:CodePen
绝对定位元素相对于最近的定位祖先对齐。将该祖先设为工具提示的容器:
table-cell { position: relative; }
Run Code Online (Sandbox Code Playgroud)
然后,不要使用像素进行对齐,而是使用百分比:
.tooltip { top: 50%; transform: translateY(-50%); }
Run Code Online (Sandbox Code Playgroud)
通过对代码的调整...
工具提示与日期垂直对齐:
更多细节在这里:元素不会保持居中,尤其是在调整屏幕大小时
| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |