【问题标题】:Smooth jScrollPane scrollbar length adjustment with dynamic content使用动态内容平滑 jScrollPane 滚动条长度调整
【发布时间】: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


    【解决方案1】:

    每次调整窗口大小时,都无需在内容上初始化 jScrollPane。你应该只做一次 - 在$(document).ready()。此外,如果您的内容是静态的,则无需使用 autoReinitialize。您应该重新初始化 jScrollPane 以更新滚动条大小,仅当您向上滑动/向下滑动容器之一或 window.resize 时。所以,code 越来越漂亮了:)

    function togglePanel(num) {
        var jsp = $('#content').data('jsp');
        jQuery('#a' + num).slideToggle({
            "duration": "slow",
            "step": function(){
                jsp.reinitialise();
            }
        });
        return false;
    }
    
    $(document).ready(function () {
    
        var container = $('#content').jScrollPane({
            showArrows: false,
            autoReinitialise: false
        });
        var jsp = container.data('jsp');
    
        $(window).on('resize', function(){
            jsp.reinitialise();
        });
    
        // 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 (container.width() != $(window).width()) {
            jsp.reinitialise();
        }
    
    });
    

    【讨论】:

    • 亲爱的@Gromo,非常感谢您的帮助。您的代码的第一部分神奇地工作!我对如此简约和高效的解决方案感到震惊,我真的应该尝试学习理解脚本。现在,第二位会导致滚动条的行为异常......只需检查your jsfiddle 并尝试在打开其他容器之一的同时访问文本#5。另外,我希望滚动条完全取代浏览器的默认滚动条。所以我决定坚持使用 jScrollPane 网站提供的初始脚本 - ......
    • ..... 这反过来又使滚动条在第一次单击时从最右边“跳”到所需位置。哦,伙计...我最终只是将您的位粘贴到 jScrollPane 脚本上,现在它工作正常,但您可能不再喜欢该脚本了。 Have a look, if you like... 不过还是要再次感谢!
    • 注意:上面提到的奇怪行为只发生在 iframe 中发布时(如在 jsfiddle 中),但我的网页也在 iframe 中
    • @zagara 很高兴知道它有帮助。您可以将我的答案标记为正确答案,它会给我增加一点声誉:)。还有一件事:下次您需要自定义滚动条时,请查看jQuery Scrollbar
    • 刚刚解决了“跳跃”滚动条问题,所以我很高兴 - 这是你的绿色勾号!
    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多