【问题标题】:jQuery Cycle with hash permalinks带有哈希永久链接的 jQuery 循环
【发布时间】:2013-01-20 20:53:29
【问题描述】:

我正在尝试让 jQuery Cycle 使用每张幻灯片中的唯一名称,以便能够直接链接到每张幻灯片。我在 malsups 网站上找到了这个:http://jquery.malsup.com/cycle/perma2.html

尝试将其合并到我的演示中,但无济于事,不确定我做错了什么?
演示: http://jsbin.com/uviram/1

即使我去http://jsbin.com/uviram/1#slide2,它仍然会引导我去http://jsbin.com/uviram/1#slide1..

$(function() {

    var h, 
        hash = window.location.hash, 
        hashes = {},
        index = 0;

    $('.slideshow slide').each(function(i) {
        h = $(this).data('hash');
        hashes[h] = i;
    });

    if (hash)
        index = hashes[hash.substring(1)] || index;

    $('.slideshow').cycle({
        fx: 'scrollHorz',
        timeout: 0,
        prev: $('.prev'),
        next: $('.next'),
        after: function(curr,next,opts) {
            h = $(this).data('hash');
            window.location.hash = h;
        }
    });
});

【问题讨论】:

  • 好的,你用的是什么代码?
  • @TomWalters 现在用代码 sn-p 更新,整个代码在我链接到的演示中可用!
  • $(function() {改成$(document).ready(function() {?
  • @Mooseman 他们的效果一模一样

标签: jquery hash cycle jquery-cycle


【解决方案1】:

我已经更新了你的 jsbin 演示,现在它可以工作了。存在三个问题:

  • 首先,我将$('.slideshow slide') 更改为$('.slideshow .slide') 以正确选择幻灯片。

  • 其次,我将startingSlide选项放入循环调用中。

  • 最后,我加入了一个 hashchange 监听器,这样如果用户改变了 哈希没有重新加载页面,它会循环到正确的 幻灯片。

查看更新后的演示,看看它是否有效:

http://jsbin.com/uviram/6#slide4

【讨论】:

【解决方案2】:

您尚未包含 startingSlide 选项以将您的 index 传递给

$('.slideshow').cycle({
        /* pass index to startingSlide*/
        startingSlide:index,
        fx: 'scrollHorz',
        timeout: 0,
        prev: $('.prev'),
        next: $('.next'),
        after: function(curr,next,opts) {
            h = $(this).data('hash');
            window.location.hash = h;
        }
});

【讨论】:

    猜你喜欢
    • 2011-01-10
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多