Ahs*_*lam 4 html javascript css css3
我有一个<div>带有以下CSS属性的不同类型图像的html页面:
.images_container {
position: relative;
height: calc(100% - 200px);
padding-top: 20px;
overflow: auto;
margin: auto;
}
Run Code Online (Sandbox Code Playgroud)
当前正在显示页面滚动条,但我想仅在用户滚动页面时显示滚动条.这在CSS或JavaScript中是否可行?
如果您连接元素scroll事件,您可以执行类似这样的操作,您可以在其中设置innerto width: 100%和a padding-right: 20px.(填充权限值需要大于滚动条的宽度)
这将推动滚动条退出视图,并在滚动时删除填充,初始化一个计时器,如果停止滚动将重置填充
(function(timer) {
window.addEventListener('load', function() {
var el = document.querySelector('.inner');
el.addEventListener('scroll', function(e) {
(function(el){
el.style.padding = '0';
clearTimeout(timer);
timer = setTimeout(function() {
el.style.paddingRight = '20px';
}, 100);
})(el);
})
})
})();Run Code Online (Sandbox Code Playgroud)
.outer {
height: 180px;
width: 500px;
border: 1px solid green;
overflow: hidden;
}
.inner {
width: 100%;
height: 99%;
overflow: auto;
padding-right: 20px;
}Run Code Online (Sandbox Code Playgroud)
<div class="outer">
<div class="inner">
Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some
content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some
content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
使用Element.classList而不是内联样式更新了切换类,这可能是更推荐的方式.
(function(timer) {
window.addEventListener('load', function() {
var el = document.querySelector('.inner');
el.addEventListener('scroll', function(e) {
(function(el){
el.classList.add('scroll');
clearTimeout(timer);
timer = setTimeout(function() {
el.classList.remove('scroll');
}, 100);
})(el);
})
})
})();Run Code Online (Sandbox Code Playgroud)
.outer {
height: 180px;
width: 500px;
border: 1px solid green;
overflow: hidden;
}
.inner {
width: 100%;
height: 99%;
overflow: auto;
padding-right: 20px;
}
.inner.scroll {
padding-right: 0;
}Run Code Online (Sandbox Code Playgroud)
<div class="outer">
<div class="inner">
Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some
content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some
content<br>Some content<br> Some content<br>Some content<br>Some content<br> Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>Some content<br>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
小智 -5
这是一个自动功能,
您可以尝试设置:
overflow : scroll;
Run Code Online (Sandbox Code Playgroud)
强制它按你想要的方式工作。
您也可以尝试overflow-x或overflow-y,具体取决于您滚动的轴。
| 归档时间: |
|
| 查看次数: |
6639 次 |
| 最近记录: |