【问题标题】:Scroll to given jCarousel slide on page load在页面加载时滚动到给定的 jCarousel 幻灯片
【发布时间】:2011-05-29 04:28:30
【问题描述】:

你好 stackoverflow 贡献者!我有这个脚本通过浏览器 URL 的哈希获取我的 jCarousel 的起始位置。 text.html#2 之类的东西。

我想要实现的是让 jCarousel 在页面加载时滚动到给定位置。然而,我下面的代码似乎只有在我将其绑定到点击时才有效 - 它不会响应页面加载请求。

初始化 jCarousel

jQuery('#body_list').jcarousel({
        scroll: 1,
        initCallback: bodylist_initCallback
});

回调函数

function bodylist_initCallback(carousel) {
        $(window).load(function () {
            if(window.location.hash) {
                var hash = window.location.hash.slice(1); 
                carousel.scroll(jQuery.jcarousel.intval(hash));
            }
        });
});

替代滚动调用 以下行在 Safari 中除外

if(window.location.hash) {
        var hash = window.location.hash.slice(1); 
        jQuery('#body_list').jcarousel('scroll', hash);
}

【问题讨论】:

    标签: jquery hash custom-controls history jcarousel


    【解决方案1】:

    您可以在初始化 jCarousel 时设置start 选项:

    var hash = 1; // Default start index if none is specified in URL
    if (window.location.hash) {
        hash = parseInt(window.location.hash.slice(1));
    }
    jQuery('#mycarousel').jcarousel({
        start: hash,
        scroll: 1
    });
    

    更新

    如果您想在加载页面时看到滚动动画,请尝试在 jCarousel 的 initCallback 选项中设置超时:

    jQuery("#mycarousel").jcarousel({
        initCallback: function(carousel) {
            if (window.location.hash) {
                var hash = window.location.hash.slice(1);
                setTimeout(function() {
                    carousel.scroll(parseInt(hash, 10));
                }, 500);
            }
        }
    });
    

    似乎可以在 FF/Chrome 中使用。我在 Ubuntu 上,所以无法在 IE 或 Safari 上试用。

    【讨论】:

    • hmm 'start' 将开始设置为给定位置,无需滚动/滑动面板。
    • @James:哦,抱歉,我没有在 OP 中看到。我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多