Query String to Control BXSlider startSlide

会有一股神秘感。 提交于 2020-01-06 04:30:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!