问题
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.
回答1:
You can either manually extract id from window.location.search
. Or use some library with deparam
function in it.
$(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,
});
})
来源:https://stackoverflow.com/questions/13444854/query-string-to-control-bxslider-startslide