【发布时间】:2014-05-02 12:24:57
【问题描述】:
我的一些网页包含几个文本元素,它们可以通过 jQuery“手风琴”效果展开和折叠:
function show_panel(num) {
jQuery('div.panel').hide();
jQuery('#a' + num).slideToggle("slow");
}
function hide_panel(num) {
jQuery('div.panel').show();
jQuery('#a' + num).slideToggle("slow");
}
这会导致窗口大小发生变化,因此必须重新初始化 jScrollPane,这也会改变滚动条的长度。为了实现滚动条的平滑长度调整,我将“autoReinitialise”选项设置为“true”,将“autoReinitialiseDelay”设置为“40”毫秒:
$(document).ready(function () {
var win = $(window);
// Full body scroll
var isResizing = false;
win.bind(
'resize',
function () {
if (!isResizing) {
isResizing = true;
var container = $('#content');
// Temporarily make the container tiny so it doesn't influence the
// calculation of the size of the document
container.css({
'width': 1,
'height': 1
});
// Now make it the size of the window...
container.css({
'width': win.width(),
'height': win.height()
});
isResizing = false;
container.jScrollPane({
showArrows: false,
autoReinitialise: true,
autoReinitialiseDelay: 40
});
}
}).trigger('resize');
// Workaround for known Opera issue which breaks demo (see
// http://jscrollpane.kelvinluck.com/known_issues.html#opera-scrollbar )
$('body').css('overflow', 'hidden');
// IE calculates the width incorrectly first time round (it
// doesn't count the space used by the native scrollbar) so
// we re-trigger if necessary.
if ($('#content').width() != win.width()) {
win.trigger('resize');
}
});
效果还可以,但代价是 CPU 使用率非常高,这让我的粉丝疯狂。
这是一个显示设置和效果的 jsfiddle:http://jsfiddle.net/VVxVz/
这是一个示例页面(实际上它是所显示网页中的 iframe):http://www.sicily-cottage.net/zagaraenausfluege.htm
是否有可能在不使用“autoReinitialise”选项的情况下实现滚动条长度的相同“平滑”过渡,可能使用额外的脚本,对 jscrollpane.js 进行一些修改,或者只是滚动条的 css 动画和然后手动调用重新初始化?
我在 javascript 方面完全没用,所以任何帮助都将不胜感激。
【问题讨论】:
标签: javascript jquery html css jquery-jscrollpane