Hash URL navigation with a Flexslider

前端 未结 2 896
我在风中等你
我在风中等你 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:31

    I came across this question when looking for a solution to jump to a slide in FlexSlider using the anchor part (hash) of a URL. However, Andrew's answer did not work for me, possibly because at the time of writing this, FlexSlider is now version 2.6.4 and requires jQuery version 1.7.0+.

    My solution borrows the first part of Andrew's answer, for getting the number out of the URL, and then uses a built-in helper string from FlexSlider 2.6.4 for the rest:

    $(window).on('load', function () {
    
      // Get number from hash part of URL
      var index = 0, hash = window.location.hash;
      if (hash) {
        index = /\d+/.exec(hash)[0];
        index = (parseInt(index) || 1) - 1;
      }
    
      // Initialise a basic FlexSlider
      $('.flexslider').flexslider({
      animation: "slide"
      });
    
      // Pass index into helper string to jump to specific slide
      $('.flexslider').flexslider(index); 
    }
    

    Hope this helps anyone looking for a solution on later versions of FlexSlider :)

提交回复
热议问题