【发布时间】:2014-06-11 05:24:32
【问题描述】:
This 是我使用的参考资料,它解释了如何使 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() 否则将无法工作。
更新:
我已阅读所有答案,但我的问题仍未解决,也不知道是什么原因造成的。这就是有问题的live。 基本上,我试图遵循几乎完全相同的this,但我的网站无法按预期运行肯定是有原因的。有两个问题。
- 当我将
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;
}
更新:
在我将overflow:scroll 添加到#project-content 后,滚动按预期工作。我现在需要的只是让滚动条在#project-content 中消失。我尝试将overflow:hidden 添加到其父级,但没有成功。我也尝试将它添加到html, body,但随后整个文档拒绝接受任何滚动功能,如scrollTop()。
任何帮助将不胜感激!
【问题讨论】:
-
您可能会考虑查看诸如jscrollpane 之类的插件。至少您可以找出该代码的作用并重现您想要保留的部分
-
我很困惑?只需使用该问题中的代码即可。 DEMO 就这样?
-
也许我没有抓住重点,但你为什么还需要它呢?这是什么
$.scrollTo()你仍然可以在香草JS中使用dom.scrollLeft和dom.scrollTop,overflow: hidden;as shown in the example -
嗨@drew_w 非常感谢您帮助我。我更新了问题以更好地解释我遇到问题的原因。
-
@MaximusS 没有回答您的问题,但请注意:从可用性的角度来看,您将要做的是一个非常糟糕的主意!用户习惯于通过滚动条识别可滚动内容。如果没有 JS,您的内容将无法访问。所以我不建议这样做。
标签: javascript html css scroll