0

好的,我已经根据评论更新了我的代码,谢谢。所以后退按钮现在可以工作了,但是,它阻止了我的内部链接工作,因为它们不再反映在 URL 中。以前我是这样写链接并填写幻灯片编号的:

<a href="javascript:setSlide(1)"></a>

这仍然有效,但没有像使用 malsup 代码那样更改 URL。如何将这 2 位代码链接起来?我不是很有经验。

非常感谢

$(function () {

// get current slide's number
function currentSlide() {
    var hash = window.location.hash || '#slide-1';
    return parseInt(hash.replace(/[A-Za-z#\-\/!]/g, '') - 1); 
}
// global vars  
var cycleSelector = $('.images'),
        startSlide = currentSlide(),
        hasSlid = 0;

// append some markup for the controls      
cycleSelector.before() 
// start jQuery Cycle   
.cycle({ 
     startingSlide: startSlide,
     // when using the next/prev links
 onPrevNextEvent: function(isNext, idx, slide) { 
    hasSlid = 1;
    window.location.hash = "slide-"+ (parseInt(idx) + 1) + ""; 
    return false;
 },
 // when using the pager thumbnails
 onPagerEvent: function(idx, slide) { 
    hasSlid = 1;
    window.location.hash = "slide-"+ (parseInt(idx) + 1) + "";  
    return false;
 },
 timeout: 0, 
 pager:  '#nav',
 next: '.next',
 prev: '.prev',
 slideResize: 0,
 containerResize: 0,
 speed: 500,
 before: onBefore,
 nowrap: 1,
 // build the thumbnails
 pagerAnchorBuilder: function(idx, slide) { 
   return '<a href="#"><p>' + $(slide).find('h1').html() + ' </p></a>'; 
} 
});


// bind to the hashchange event
$(window).bind('hashchange', function () {
        var slideNo = currentSlide();
        // we only want to fire the slide change if the next button or the pager hasn't done it for us
        if (hasSlid === 0) { cycleSelector.cycle(slideNo); }
        // return it back to zero
        hasSlid = 0;
}); 

// when the page loads, we need to trigger a hashchange
$(window).trigger( "hashchange" );

function onBefore() { 
            var title = $(this).find('h1').html();
            $('#caption') 
            .html(title); 
        };

});


function setSlide(index) {
            $('.images').cycle(index);
        }   
4

1 回答 1

1

对于任何有兴趣的人,我都想通了。在上面的代码中,您需要更改:

function setSlide(index) {
        $('.images').cycle(index);
    } 

对此:

function setSlide(index) {
            $('.images').cycle(index);
             window.location.hash = "slide-"+ (index) + ""; 
             return false;
        }

然后继续使用以下内容创建内部链接,根据您要链接到的幻灯片更改数字

<a href="javascript:setSlide(1)"></a> 
于 2013-05-13T18:12:00.100 回答