Hash URL navigation with a Flexslider

前端 未结 2 922
我在风中等你
我在风中等你 2021-01-14 13:49

I\'m building a website that makes use of a flexslider, but I want to implement some URL hash navigation. Based on the hash of the URL, i plan on getting the index of the sl

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 14:25

    I was searching for the same answer, and figured it out, so here it is in case you, or someone else, needs it. As long as we're just talking number values, it's pretty simple.

    $(window).load(function(){
    
        //set some variables for calculating the hash
    
        var index = 0, hash = window.location.hash;
    
        //via malsup (Cycle plugin), calculates the hash value
    
        if (hash) {
            index = /\d+/.exec(hash)[0];
            index = (parseInt(index) || 1) - 1;
        }
    
        $(".flexslider").flexslider({
            startAt: index, //now foo.html#3 will load item 3
            after:function(slider){
                window.location.hash = slider.currentSlide+1;
                //now when you navigate, your location updates in the URL
            }
        })
    })
    

    That should do the trick

提交回复
热议问题