点击按钮时如何将label居中定位在弹出框顶部
解决方案
仅需修改CSS部分即可实现预期效果,无需调整HTML结构,修改点如下:
- 移除label的固定左边距,避免居中时出现偏移
- 给label添加过渡动画,实现位移的平滑效果
- 针对按钮激活/聚焦状态单独设置label的居中定位规则
- 下调列表内容的垂直位置,避免和顶部label重叠
修改后的完整CSS代码:
div { position: relative; box-sizing: border-box; } p { font-size: 9pt; margin-bottom: 1pt; margin-top: 2.5pt; margin-left: 2px; z-index: 5; } a { color: inherit; text-decoration: none; } label { font-size: 10pt; margin: 0; color: #e26030; font-weight: bold; } .centered-y { position: absolute; width: 100%; height: 100%; top: calc(50% + 10px); -webkit-transform: translateY(-50%); transform: translateY(-50%); } .test { position: absolute; top: -2px; left: 16px; transition: all 0.25s ease-in-out 0.25s; } .distribution-map { position: relative; width: 100%; height: 500%; padding: 20px; box-sizing: border-box; margin: 0 auto; background: white; } .distribution-map .map-point { cursor: pointer; outline: none; z-index: 0; position: absolute; width: 13px; height: 13px; border-radius: 15px; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); -moz-transition: opacity 0.25s ease-in-out 0.25s, width 0.25s ease-in-out 0.25s, height 0.25s ease-in-out 0.25s, z-index 0.25s ease-in-out 0.25s; -o-transition: opacity 0.25s ease-in-out 0.25s, width 0.25s ease-in-out 0.25s, height 0.25s ease-in-out 0.25s, z-index 0.25s ease-in-out 0.25s; -webkit-transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out, z-index 0.25s ease-in-out; -webkit-transition-delay: 0.25s, 0.25s, 0.25s, 0.25s; transition: opacity 0.25s ease-in-out 0.25s, width 0.25s ease-in-out 0.25s, height 0.25s ease-in-out 0.25s, z-index 0.25s ease-in-out 0.25s; background: rgba(26, 26, 26, 0.85); border: 2.5px solid #e26030; } .distribution-map .map-point .content { opacity: 0; transition: opacity 0.25s ease-in-out; width: 100%; height: 100%; left: 50%; -webkit-transform: translateX(-50%); transform: translateX(-50%); overflow: overlay; } .distribution-map .map-point:active, .distribution-map .map-point:focus { margin: 0; padding: 0; opacity: 1; width: 100px; height: 100px; color: #e5e5e5; z-index: 1; transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out; } .distribution-map .map-point:active .test, .distribution-map .map-point:focus .test { left: 50%; top: 8px; transform: translateX(-50%); } .distribution-map .map-point:active .content, .distribution-map .map-point:focus .content { opacity: 1; -moz-transition: opacity 0.25s ease-in-out 0.25s, height 0.25s ease-in-out, overflow 0.25s ease-in-out; -o-transition: opacity 0.25s ease-in-out 0.25s, height 0.25s ease-in-out, overflow 0.25s ease-in-out; -webkit-transition: opacity 0.25s ease-in-out, height 0.25s ease-in-out, overflow 0.25s ease-in-out; -webkit-transition-delay: 0.25s, 0s, 0s; transition: opacity 0.25s ease-in-out 0.25s, height 0.25s ease-in-out, overflow 0.25s ease-in-out; overflow: hidden; } .distribution-map .map-point:active .content a:hover, .distribution-map .map-point:active .content a:active, .distribution-map .map-point:focus .content a:hover, .distribution-map .map-point:focus .content a:active { color: #afe1fa; }
修改完成后,点击按钮时label会从按钮侧边平滑移动到弹出框顶部居中位置,不会出现偏移问题,也不会和下方列表内容重叠。
内容的提问来源于stack exchange,提问作者W.Diaz
相关产品推荐
相关产品推荐

