jQuery UI Slider - Disable click on slider track

后端 未结 13 1310
南方客
南方客 2020-12-31 08:57

I\'m trying to find a way to disable the click on my jQuery UI Slider\'s track. Clicking on the handle is ok but I do not want the handle to respond if the user clicks on t

13条回答
  •  再見小時候
    2020-12-31 09:02

    This should take care of it.

    /**
     *  Turns off the track click for the given slider control
     */
    
    function disableSliderTrack($slider){
    
        $slider.bind("mousedown", function(event){
    
            return isTouchInSliderHandle($(this), event);   
    
        });
    
        $slider.bind("touchstart", function(event){
    
            return isTouchInSliderHandle($(this), event.originalEvent.touches[0]);
    
        });
    }
    
    function isTouchInSliderHandle($slider, coords){
    
        var x = coords.pageX;
        var y = coords.pageY;
    
        var $handle = $slider.find(".ui-slider-handle");
    
        var left = $handle.offset().left;
        var right = (left + $handle.outerWidth());
        var top = $handle.offset().top;
        var bottom = (top + $handle.outerHeight());
    
        return (x >= left && x <= right && y >= top && y <= bottom);    
    }
    
    
        disableSliderTrack($(".ui-slider"));
    

提交回复
热议问题