我在叠加层内添加了滚动视图。一切正常,直到我尝试向滚动视图添加填充。正确使用了顶部和左侧填充,但是滚动视图内的文本超出了右侧和底部。这是我的代码
document.addEventListener("keydown", keyDownTextField, false);
this.title_element = jQuery('.scrollviewwrapper', this.$el);
this.title_element.html('<ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> <ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> ');
function keyDownTextField(e) {
alert(document.getElementById('scrollviewwrapper').scrollTop);
var keyCode = e.keyCode;
if (keyCode == 38) {
// up arrow
} else if (keyCode == 40) {
// down arrow
}
}
.overlay {
position: absolute;
background: red;
height: 200px;
width: 500px;
top: 50px;
left: 20px;
bottom: 50px;
z-index: 100;
}
.scrollviewwrapper {
position: relative;
height: 200px;
width: 500px;
padding-top: 30px;
padding-left: 30px;
padding-right: 30px;
padding-bottom: 30px;
overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<html>
<body>
<div class="overlay">
<div class="scrollviewwrapper" id="scrollviewwrapper">
test
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
将box-sizing:border-box;
规则添加到您的.scrollviewwrapper
类中:
document.addEventListener("keydown", keyDownTextField, false);
this.title_element = jQuery('.scrollviewwrapper', this.$el);
this.title_element.html('<ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> <ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> ');
function keyDownTextField(e) {
alert(document.getElementById('scrollviewwrapper').scrollTop);
var keyCode = e.keyCode;
if(keyCode==38) {
// up arrow
} else if(keyCode==40) {
// down arrow
}
}
.overlay{
position: absolute;
background: red;
height: 200px;
width: 500px;
top: 50px;
left: 20px;
bottom : 50px;
z-index: 100;
}
.scrollviewwrapper{
position: relative;
height: 200px;
width: 500px;
padding-top: 30px;
padding-left: 30px;
padding-right: 30px;
padding-bottom: 30px;
overflow:auto;
box-sizing:border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<html>
<body>
<div class="overlay">
<div class="scrollviewwrapper" id="scrollviewwrapper">
test
</div>
</div>
</body>
</html>
答案 1 :(得分:0)
您需要将background: red;
添加到您的.scrollviewwrapper
类中:
document.addEventListener("keydown", keyDownTextField, false);
this.title_element = jQuery('.scrollviewwrapper', this.$el);
this.title_element.html('<ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> <ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> ');
function keyDownTextField(e) {
alert(document.getElementById('scrollviewwrapper').scrollTop);
var keyCode = e.keyCode;
if(keyCode==38) {
// up arrow
} else if(keyCode==40) {
// down arrow
}
}
.overlay{
position: absolute;
height: 200px;
width: 500px;
top: 50px;
left: 20px;
bottom : 50px;
z-index: 100;
}
.scrollviewwrapper{
position: relative;
background: red;
height: 200px;
width: 500px;
padding-top: 30px;
padding-left: 30px;
padding-right: 30px;
padding-bottom: 30px;
overflow:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<html>
<body>
<div class="overlay">
<div class="scrollviewwrapper" id="scrollviewwrapper">
test
</div>
</div>
</body>
</html>
希望这会有所帮助。
答案 2 :(得分:0)
交换绝对位置,以便滚动视图包装器位于背景内部:
document.addEventListener("keydown", keyDownTextField, false);
this.title_element = jQuery('.scrollviewwrapper', this.$el);
this.title_element.html('<ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> <ul>\r<li>Fermentum congue dictumst integer sapien conubia, per volutpat a duis donec, habitasse quisque tristique hac</li>\r<li>Imperdiet aliquam praesent id viverra aliquam inceptos</li>\r<li>Cubilia litora placerat fusce vulputate habitant varius cursus molestie lorem magna mi non</li>\r<li>Imperdiet sociosqu aenean ornare sagittis faucibus in facilisis vitae, lobortis curae hac justo tellus tincidunt</li>\r<li>Urna eu consequat non consectetur duis ut justo eleifend sapien</li>\r</ul> ');
function keyDownTextField(e) {
alert(document.getElementById('scrollviewwrapper').scrollTop);
var keyCode = e.keyCode;
if (keyCode == 38) {
// up arrow
} else if (keyCode == 40) {
// down arrow
}
}
.overlay {
position: relative;
background: red;
height: 200px;
width: 500px;
top: 50px;
left: 20px;
bottom: 50px;
z-index: 100;
}
.scrollviewwrapper {
position: absolute;
top: 0; /* use all 4 co-ordinates instead of width and height so it fills the container */
left: 0;
right: 0;
bottom: 0;
padding: 30px; /* this is shorthand and will apply padding to all 4 sides */
overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<html>
<body>
<div class="overlay">
<div class="scrollviewwrapper" id="scrollviewwrapper">
test
</div>
</div>
</body>
</html>