我正在做一个项目,为网站提供一个插件工具,它使用了大量的jQuery。表示/设计是至关重要的,我想用更好看的东西替换浏览器应用于html元素的标准(丑陋)滚动条。
有许多jQuery插件可以应用自定义滚动条并允许通过CSS进行样式设置,这很棒,但我尝试过的所有插件似乎都遇到了相同的问题:如果可滚动内容包含带有文本字段等的表单,则在字段之间切换不会激活滚动条,在某些情况下可能会完全搞乱自定义滚动条布局。
我尝试过的两个插件示例:
http://manos.malihu.gr/jquery-custom-content-scroller http://baijs.nl/tinyscrollbar/
我也试过其他的,但在所有的演示/示例中,内容都是纯文本的。我已经在这方面做了很多搜索,但似乎还没有人尝试将这些插件与基于表单的内容一起使用。
所有这些插件似乎或多或少以相同的方式工作,我可以确切地看到发生了什么以及为什么,但我只是想知道是否有人遇到过这个问题和/或找到了解决方案?
这个问题可以很容易地复制如下(使用tinyscrollbar插件):
将此代码添加到标准的html测试页面-
CSS:
<style>
#tinyscrollbartest { width: 520px; height: 250px; padding-right: 20px; background-color: #eee; }
#tinyscrollbartest .viewport { width: 500px; height: 200px; overflow: hidden; position: relative; }
#tinyscrollbartest .overview { list-style: none; position: absolute; left: 0; top: 0; }
#tinyscrollbartest .scrollbar { position: relative; float: right; width: 15px; }
#tinyscrollbartest .track { background: #d8eefd; height: 100%; width: 13px; position: relative; padding: 0 1px; }
#tinyscrollbartest .thumb { height: 20px; width: 13px; cursor: pointer; overflow: hidden; position: absolute; top: 0; }
#tinyscrollbartest .thumb .end { overflow: hidden; height: 5px; width: 13px; }
#tinyscrollbartest .thumb, #tinyscrollbartest .thumb .end { background-color: #003d5d; }
#tinyscrollbartest .disable { display: none; }
</style>Html:
<div id="tinyscrollbartest">
<div class="scrollbar">
<div class="track">
<div class="thumb">
<div class="end"></div>
</div>
</div>
</div>
<div class="viewport">
<div class="overview">
</p>Here's a text field: <input type="text"/><p>
...
// lots of content to force scrollbar to appear,
// and to push the next field out of sight ..
...
<p>Here's another field: <input type="text"/></p>
</div>
</div>
</div>插件参考(假设还引用了jquery库等):
<script type="text/javascript" src="scripts/jquery.tinyscrollbar.min.js"></script>Jquery代码:
<script type="text/javascript">
$(document).ready(function () {
$('#tinyscrollbartest').tinyscrollbar();
});
</script>现在单击第一个文本字段,使其具有焦点,按tab键移动到下一个文本字段,并查看发生了什么。
发布于 2011-08-18 17:50:45
我理解你的问题..但是很难找到一个好的解决方案。您可以尝试在表单元素上设置焦点事件。并让此事件触发tinyscrollbar的scrollbar_update函数。您可以将当前具有焦点的表单元素的方法设置为offsetTop参数。我想这会奏效的。
$(‘formelements’)函数(.focus(){ YourScrollbar.tinyscrollbar_update(this.offsetTop);});
发布于 2012-05-02 01:46:46
我不得不用我自己的to键覆盖标准的to键功能:
$(".scrollable").each(function() {
if (!$(this).data("scrollbar"))
{
$(this).data("scrollbar", new Scrollbar({
holder:$(this)
}));
$(this).find("input").bind("keydown", function(e)
{
var keyCode = e.keyCode || e.which;
if (keyCode == 9)
{
e.preventDefault();
var scrollTo = $(this);
if (e.shiftKey)
{
var nextInput = $(this).prevAll("input:not([type=hidden])").first();
scrollTo = nextInput.prevAll("input:not([type=hidden]), label").first();
}
else
{
var nextInput = $(this).nextAll("input:not([type=hidden])").first();
}
if (nextInput.length)
{
console.log(scrollTo);
$(this).closest(".scrollable").data("scrollbar").scrollTo(scrollTo, function()
{
nextInput.focus().select();
});
}
}
});
}
});不得不等待卷轴有点烦人,但我看不到任何其他选择。
https://stackoverflow.com/questions/6718381
复制相似问题