且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

引导轮播-链接到特定的幻灯片

更新时间:2023-02-14 10:21:22

您可以使用查询字符串,例如... www.example.com?slide=2

You can use a query string like... www.example.com?slide=2

$(document).ready(function(){

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    $('#myCarousel').carousel(getParameterByName('slide'));
});

或哈希值,例如www.example.com#2

Or hash values like... www.example.com#2

$(document).ready(function(){
    $('#myCarousel').carousel(window.location.hash.substring(1));
});

无论您使用哪种方法,都只需从url中提取一个整数,因此您可能希望使代码更健壮,以确保仅在获得有效整数的情况下才移动轮播,并检查其是否正确在轮播中存在的幻灯片数量的范围内.

Which ever you use, you have to only extract an integer from the url, so you may want to make the code a little more robust to ensure you are only moving the carousel if you get a valid integer and also check that it is within the bounds of the number of slides that exist within the carousel.

.carousel(number)

将轮播循环到特定帧(从0开始,类似于数组).