热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我的立场:使用flexbox时粘性元素不粘

如何解决《我的立场:使用flexbox时粘性元素不粘》经验,为你挑选了1个好方法。

我被困在这一点上,并认为我会分享这个position: sticky+弹性盒子的问题:

我的粘性div工作正常,直到我将视图切换到弹性盒容器,并且当它被包装在弹性盒父中时突然div不粘.

.flexbox-wrapper {
  display: flex;
  height: 600px;
}
.regular {
  background-color: blue;
}
.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: red;
}
This is the regular box
This is the sticky box

JSFiddle显示问题



1> BHOLT..:

由于弹性框元素默认为stretch,所有元素都是相同的高度,无法滚动.

添加align-self: flex-start到sticky元素将高度设置为auto,允许滚动并修复它.

目前仅在Safari和Edge中支持此功能

.flexbox-wrapper {
  display: flex;
  overflow: auto;
  height: 200px;          /* Not necessary -- for example only */
}
.regular {
  background-color: blue; /* Not necessary -- for example only */
  height: 600px;          /* Not necessary -- for example only */
}
.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  align-self: flex-start; /* <-- this is the fix */
  background-color: red;  /* Not necessary -- for example only */
}
This is the regular box
This is the sticky box

推荐阅读
author-avatar
云崽美不美_525
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有