javascript - 动态添加子代时如何设置父代可滚动
问题描述
多次点击按钮
为什么.wrap - max-height
不起作用
我希望它受到限制,100vh - 108px
并且如果需要也.ins
可以滚动。
有什么帮助吗?
$('button').on('click', function(){
let a = "<div class='abc'>ABC</div>";
$(a).appendTo($('#ins'));
});
.wrap{
position:fixed;
left:50%;
top:54px;
transform:translateX(-50%);
max-height:calc(100vh - 108px);
background:silver;
overflow:hidden;
}
.title{background:gold;}
.bottom{margin-top:5px;}
.sub{background:orange;}
.ins{overflow-y:scroll;}
.abc{background:lightgreen;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>CLICK</button>
<div class='wrap'>
<div class='title'>LOREM</div>
<div class='bottom'>
<div class='sub'>IPSUM</div>
<div class='ins' id='ins'>
<div class='abc'>ABC</div>
</div>
</div>
</div>
解决方案
将 .ins 更改为如下所示
$('button').on('click', function(){
let a = "<div class='abc'>ABC</div>";
$(a).appendTo($('#ins'));
});
.wrap{
position:fixed;
left:50%;
top:54px;
transform:translateX(-50%);
max-height:calc(100vh - 108px);
background:silver;
overflow:hidden;
}
.ins {
overflow-y: auto;
max-height: calc(100vh - 108px);
}
.title{background:gold;}
.bottom{margin-top:5px;}
.sub{background:orange;}
.abc{background:lightgreen;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>CLICK</button>
<div class='wrap'>
<div class='title'>LOREM</div>
<div class='bottom'>
<div class='sub'>IPSUM</div>
<div class='ins' id='ins'>
<div class='abc'>ABC</div>
</div>
</div>
</div>
推荐阅读
- node.js - 使用 nodejs/sequelize 加密 postgres 数据
- python - “列表索引超出范围”仍然出现错误
- javascript - 如果他们以前从未访问过该站点,我如何重定向到不同的 URL
- swift - 试图快速生成布局
- scala - 多个 spark scala 作业同时写入同一路径
- android - 在设置中打开设置选项
- javascript - Three.js 使用 Raycaster 和 OrthographicCamera 的点击事件
- r - 将线条添加到现有的 ggplot 并在图例中获取它们
- python - Docker要求无法安装
- python - 如何在python中重塑图像的128 d矢量