【问题标题】:jquery slider - slide function canceling when it shouldntjquery滑块 - 不应该取消的滑动功能
【发布时间】:2015-04-10 13:07:22
【问题描述】:

所以我有一个滑块可以改变画布中对象的背景颜色。我希望在幻灯片上更新值,因为用户需要能够在进行更改时看到更改。

滑动时,对象的颜色会按预期发生变化,但是当滑块的句柄被释放时,ui.value 被设置为其先前的值 0。根据 jquery ui 滑块文档,这意味着事件正在被取消...为什么会这样?

http://api.jqueryui.com/slider/#event-slide

$(function() {
    $('.colorShapeSolo').slider({
        min: 0,
        max: 255,
        change: colorShapeAdapter,
        slide: function (event, ui) { colorShapeAdapter(ui.value); },   
        name: 'shape'   
    });
});

function colorShapeAdapter(value)   { colorPickerAdapter('shape', 'colorShape_noColor', 'colorShape_color', 'colorShape div', value); }

// Set color of a selected object
function colorpickerSetColor($type, $noColor, $color, $picker, $hex) {

    // Set colorpicker element colors
    if ($('#' + $noColor).css('display') == 'block') $('#' + $noColor).css('display', 'none');
    if ($('#' + $color).css('display') == 'none') $('#' + $color).css('display', 'block');
    $('#' + $picker).css('backgroundColor', '#' + $hex);

    // Check drawing mode
    if ($type == 'draw') {
        canvas.freeDrawingColor = '#' + $hex;
    } else {
        var activeObject = canvas.getActiveObject();

        if (activeObject) {
            switch ($type) {
                case 'shape':
                    activeObject.fill = '#' + $hex;
                    break;
                case 'stroke':
                    if (activeObject.type === 'named-truetypetext') {
                        var strokeCol = '#' + $hex;
                        setFontOutlineColor(strokeCol, activeObject.trackId);
                        activeObject.set({outlineColor: strokeCol, stroke: strokeCol});
                    } else {
                        activeObject.stroke = '#' + $hex;
                        activeObject.strokeStyle = '#' + $hex;
                    }
                    break;
                default: break;
            }
        } else if (activeObject == null) {
            var rgb = _util.hexToRgb($hex);
            canvas.backgroundColor = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',1)';
        }
    }

    // Update
    canvas.renderAll();
    canvasBrowser_updateCC();
}

我知道问题一定出在滑块上,因为当这个问题发生时,colorpickerSetColor 函数被发送一个值 0。

【问题讨论】:

    标签: javascript jquery html css jquery-ui


    【解决方案1】:

    change和slide不能一起使用

    $(function() {
        $('.colorShapeSolo').slider({
            change: colorShapeAdapter,
            slide: function (event, ui) { colorShapeAdapter(ui.value); },     
        });
    });
    

    jQuery UI Slider - Value returned from 'slide' event on release is different from 'change' value

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2018-02-10
      • 2016-04-20
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多