【问题标题】:Bootstrap slider with slideStop event带有 slideStop 事件的引导滑块
【发布时间】:2014-05-29 16:14:53
【问题描述】:

我的页面上有一个引导滑块。我不知道如何更改此代码,即 page.php 在拖动滑块时不会持续加载,但只有在我停止拖动它时(在所需的时间段之后)。可能我必须使用 slideStop 事件,但不知道如何。

<script type="text/javascript">
    $(document).ready(function() {
        var intSeconds = 1;
        var refreshId;

        function sTimeout() {
            $("#mydiv").load("page.php"); // load content
            refreshId = setTimeout(function() { // saving the timeout
                sTimeout();
            }, intSeconds *3000);
        }
        sTimeout();
        $.ajaxSetup({cache: false});

        // The slider
        $("#ex1").slider({
            min : 1, // minimum value
            max : 20, // maximum value
            step : 1,
            value : intSeconds, // copy current value
            formater: function(value) { // option to format the values before they are sent to the tooltip
                clearTimeout(refreshId); // clear it
                intSeconds = value; // update value
                sTimeout(); // restart it
                return value*3 + ' s';
            }
        });
    });
</script>

【问题讨论】:

    标签: javascript jquery slider


    【解决方案1】:

    好的,我会尝试一下。我猜你正在使用引导滑块插件/附加组件https://github.com/seiyria/bootstrap-slider 或类似的分支。

    所以您要做的是首先取消slideStart 上的setTimeout 并在slideStop 上恢复它。但是,如果 ajax 请求在滑块移动之前启动并在拖动过程中返回,您也不想更新 div 的内容。

    代码会有点像这样:

    Javascript:

    $(document).ready(function () {
        var intSeconds = 1;
        var refreshId;
    
        //set a flag so we know if we're sliding
        slideStart = false;
        $('#ex1').slider();
        $('#ex1').on('slideStart', function () {
            // Set a flag to indicate slide in progress
            slideStart = true;
            // Clear the timeout
            clearInterval(refreshId);
        });
    
        $('#ex1').on('slideStop', function () {
            // Set a flag to indicate slide not in progress
            slideStart = false;
            // start the timeout
            refreshId = setInterval(function () { // saving the timeout
                sTimeout();
            }, intSeconds * 3000);
        });
    
        //Change the sTimeout function to allow interception of div content replacement
        function sTimeout() {
            $.ajax({
                url: 'page.php',
                dataType: 'html',
                success: function (response) {
                    if (slideStart) {
                        // slide in progress so bail out.
                        return;
                    } else {
                        // slide not in progress so go ahead.
                        $("#mydiv").html(response);
                    }
                },
                error: function () {
                    // handle your error here
                }
            });
        }
    
    
        refreshId = setInterval(function () { // saving the timeout
            sTimeout();
        }, intSeconds * 3000);
    });
    

    【讨论】:

    • 嗨,谢谢,它看起来不错,但是在开始时没有 sTimeout() 加载的部分(page.php)在我触摸滑块之前是不可见的。我不明白的一件事是为什么当我移动滑块时page.php在值高于1时一次加载多次?
    • OK 看看这个测试小提琴jsfiddle.net/robschmuecker/WvsKp/1 它应该可以满足您的需求。我也更新了答案代码。
    • 看起来不错,但在您的示例中,滑块指示器不会更新时间 - 它始终是 3 秒。您还可以向您添加有关刷新时间的示例滑块工具提示信息吗?
    • 谢谢哥们,你真的帮了我! :)
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多