有人知道如何使用CSS或JS仅在滚动时使垂直滚动条出现吗?
谢谢。
谢谢。
您可以使用CSS或JavaScript/jQuery来实现此操作。
// JavaScript Example
document.querySelector('.jsExample').addEventListener('mouseenter', function(e){
e.target.style.overflow = 'auto';
}, false);
document.querySelector('.jsExample').addEventListener('mouseleave', function(e){
e.target.style.overflow = 'hidden';
}, false);
// jQuery Example
$('.jQueryExample').hover(
function() { // Run on hover/mouseenter
$(this).css('overflow', 'auto');
},
function() { // Run on mouseleave
$(this).css('overflow', 'hidden');
}
);
/* CSS Example */
.cssExample { overflow: hidden; }
.cssExample:hover { overflow: auto; }
/* Unimportant CSS just to setup examples */
.cssExample, .jsExample, .jQueryExample {
background: #EEE;
box-sizing: border-box;
height: 4em;
width: 300px;
padding: 0.5em 1em;
margin-bottom: 1em;
overflow: hidden;
}
.cssExample::after, .jsExample::after, .jQueryExample::after {
display: block;
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cssExample">CSS Example</div>
<div class="jsExample">JavaScript Example</div>
<div class="jQueryExample">jQuery Example</div>
使用JavaScript或jQuery版本,您可以做更多的事情,例如显示自定义滚动条或将悬停事件侦听器附加到覆盖滚动元素右侧100px的元素上,以便仅当鼠标移动到该侧时才出现滚动条。这只是一些例子。