0

I am trying to have the start slide on bxslider controlled by a query string. For example, the url http://page.html?id=3 would start slide three and the user could then advance the slides in an infinite loop. The code I currently have is:

$(document).ready(function(){
$('.bxslider').bxSlider({
  startSlide: 0,
  infiniteLoop: true,
  });
});

I am not sure how to pull the query string value and then insert its value into the startSlide. Any help would be appreciated.

4

1 回答 1

1

您可以手动从中提取 id window.location.search。或者使用一些带有deparam函数的库。

$(function(){
var search = window.location.search.substr(1),
    params = $.map(search.split('&'), function(item) {
         var matches = item.split('=');
         return { name: matches[0], value: matches[2]};
    }),
    id = parseInt($.grep(params, function(param){
        return param.name === 'id';
    }).value, 10);

$('.bxslider').bxSlider({
  startSlide: id - 1, //zero based
  infiniteLoop: true,
  });
})
于 2012-11-18T23:20:00.580 回答