这个 是我使用的参考资料,它解释了如何隐藏带有滚动条的div,并仍然能够滚动。唯一的区别是我有嵌套的div。请查看我的 fiddle
HTML:
<div id="main">
<div id="sub-main">
<div id="content">
<div id="item-container">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>
</div>
</div>
</div>
CSS:
#main {
width: 500px;
height: 500px;
}
#sub-main {
width: 500px;
height: 500px;
overflow: hidden;
}
#content {
background-color: red;
width: 500px;
height: 500px;
overflow: auto;
}
#item-container {
width: 1500px;
height: 500px;
}
.item {
width: 500px;
height: 500px;
font-size: 25em;
text-align: center;
float: left;
}
如上所述,我有一个溢出的水平div,我想隐藏它的滚动条。 我必须使其仍然可滚动,因为否则 $.scrollTo()
将无法使用。
更新:
我已经阅读了所有答案,但我仍然没有解决我的问题,也不知道是什么原因导致的。 这是遇到问题的实时演示网站。 基本上,我试图按照这个做几乎相同的事情,但我的网站似乎不像预期的那样工作。 有两个问题。
- 当我将
overflow:hidden
设置为可滚动项目的父容器时,我无法滚动(本机JavaScript滚动函数也不起作用)。 - 我只想滚动溢出的容器,而不是整个窗口。 这可以通过在
$ .localScroll({target:'#projects-content'})
中设置目标来完成,但是当我设置目标时,没有任何滚动。 如果我不这样做,则只要不应用overflow:hidden
,滚动就可以正常工作。 再次感谢任何帮助。
HTML:
<div id="projects"> <!-- start of entire projects page -->
<div id="project-sidebar">
<a href="#project-first">
<div class="sidebar-item sidebar-first">first</div>
</a>
<a href="#project-second">
<div class="sidebar-item sidebar-second">second</div>
</a>
<a href="#">
<div class="sidebar-item sidebar-third">third</div>
</a>
</div>
<div id="project-content"> <!-- this must be the scrollable itmes' container, not the entire window -->
<div id="project-first" class="project-item">
<!-- these items should be scrollable -->
<div class="project-subitem" id="first-sub1">
<a href='#first-sub2' class='next'>next</a>
</div>
<div class='project-subitem' id='first-sub2'>
<a href='#first-sub1' class='prev'>prev</a>
</div>
<!-- end of scrollable items -->
</div>
</div> <!-- end of scroll scroll container -->
</div> <!-- end of entire projects page -->
<script>
// FIXME: when I set target, nothing scrolls.
// But I don't want the entire window to scroll
$('#projects').localScroll({
//target: '#project-content',
hash: false
});
</script>
CSS
#project-content {
width: 80%;
height: 100%;
position: relative;
float: left;
}
#project-sidebar {
float: left;
width: 20%;
height: 100%;
}
.project-item {
width: 300%;
height: 100%;
}
.project-subitem {
height: 100%;
width: 33.33%;
position: relative;
float: left;
}
更新:
在我给#project-content
添加了overflow:scroll
后,滚动条可以按预期工作了。现在我需要做的是让#project-content
中的滚动条消失。我尝试将overflow:hidden
添加到它的父级元素上,但没有成功。我还尝试将其添加到html, body
中,但整个文档都无法使用任何滚动功能,例如scrollTop()
。
非常感谢您的帮助!
$.scrollTo()
是什么?你仍然可以在纯JS中使用dom.scrollLeft
和dom.scrollTop
,并且通过overflow: hidden;
如示例所示。 - Idra