1

我花了 2 个小时来尝试来自不同来源的随机代码,这些代码声称他们已经设法将滑块捕捉到特定点。我不确定这对于 jQuery Mobile 库是否可行,因为 jQuery UI 版本确实支持它,但我真的需要为当前项目找出一些东西。

基本上我有一个不同日期的时间表。它们不在“步数”中,例如 10 或 20,它们是不同的数字。像 1、3、10、12、32 之类的东西。我需要滑块在这些点上捕捉。

目前,我有一些东西只在测试 step attr。

$('#slider-1').live( "change", function(e) {
    var step = $(this).attr('step'),
        val = $(this).val();
    if(step % val == step || val == step){
        console.log(val)
    }
});

我在使用滑块时遇到的问题,不仅仅是捕捉问题,是当我在 20 或 40 或 60 等附近滑动时,我正在获取控制台日志。它没有登录 20 等。

4

1 回答 1

5

Figured it out after many iterations of code and reading the documentation for specific events I can bind.

So essentially I had a timeline of numbers in my array that looks something like:

-0--10--20-----70--100---150---200---250---300---350---400-----500----------------1000

I needed to snap to the nearest number depending on the position of the slider-handle. I did this by testing the current value when the slider was being moved against the closest number in the array. I would then dynamically change the sliders value and would then have to do a force refresh of the slider to see the change.

$(function() {

    /*  
    Array of values between 0 - 1000 (can be any set of numbers you want to snap to, 
    but you must make sure the 'max' attribute on the input element is <= the last 
    value in the array)  
    */
    var values = [0, 10, 20, 70, 100, 150, 200, 250, 300, 350, 400, 500, 1000],

        /*
        A way for us to check the current and previous values of the slider so we only 
        call doSomething() if these are different. This way the function isn't called twice. These should NOT have the same values on init.
        */
        currentVal = 0,
        prevVal = null,

        // Assign our slider to a variable so we're not hitting the dom repeatedly. 
        $slider = $("#slider-1");

    /*
    After each change of our input value, we assign the slider value to the nearest number
    in our array for our "snap" effect
    */
    $($slider).bind("change", function(){
        $($slider).val(findNearest($($slider).val()))
    });

    /*
    We must capture click events on the slider itself if the user doesn't drag the handle.
    */
    $(".ui-slider[role=application]").bind("vclick",function(){
        $($slider).slider('refresh');
    })

    /*
    jQuery creates new dom elements for us, which in the case of the handle, is an anchor element. 
    Use mouseup method to test on the computer, otherwise touchend method for mobile devices.
    */
    $("a.ui-slider-handle").touchend(function(){
        // If our currentVal hasn't changed after a snap action, do nothing, otherwise call doSomething()
        if(currentVal != prevVal) {
            prevVal = currentVal;
            doSomething();
        }

        // Force slider refresh for visible snap effect
        $($slider).slider('refresh');
    })

    // Used to iterate over our array and check for nearest value to what is passed in
    function findNearest(goal) {
        var closest = null;
        $.each(values, function(){
          if (closest == null || Math.abs(this - goal) < Math.abs(closest - goal)) {
            closest = this;
          }
        });
        return currentVal = Number(closest);
    }

    function doSomething(){
        ... 
    }
});

And my HTML:

<label for="slider-1" id="slider_label_1">Input slider:</label>
<input type="range" name="slider-1" id="slider-1" value="0" min="0" max="1000" step="10" />
于 2012-09-19T00:32:35.413 回答