jquery - 使用 jquery minicolors 更改颜色

标签 jquery color-picker

如何使用 jquery minicolors 更改页面背景、页眉、字体和页脚颜色?

http://www.abeautifulsite.net/blog/2011/02/jquery-minicolors-a-color-selector-for-input-controls/

演示:http://jsfiddle.net/fNHkL/4/

$(document).ready( function() {
    var consoleTimeout;

    $('.minicolors').each( function() {

        $(this).minicolors({
            control: $(this).attr('data-control') || 'hue',
            defaultValue: $(this).attr('data-default-value') || '',
            inline: $(this).hasClass('inline'),
            letterCase: $(this).hasClass('uppercase') ? 'uppercase' : 'lowercase',
            opacity: $(this).hasClass('opacity'),
            position: $(this).attr('data-position') || 'default',
            styles: $(this).attr('data-style') || '',
            swatchPosition: $(this).attr('data-swatch-position') || 'left',
            textfield: !$(this).hasClass('no-textfield'),
            theme: $(this).attr('data-theme') || 'default',
            change: function(hex, opacity) {

                // Generate text to show in console
                text = hex ? hex : 'transparent';
                if( opacity ) text += ', ' + opacity;
                text += ' / ' + $(this).minicolors('rgbaString');

                // Show text in console; disappear after a few seconds
                $('#console').text(text).addClass('busy');
                clearTimeout(consoleTimeout);
                consoleTimeout = setTimeout( function() {
                    $('#console').removeClass('busy');
                }, 3000);

            }
        });

    });

});

最佳答案

Updated DEMO

$(document).ready(function () {
    var consoleTimeout;

    $('.minicolors').each(function () {
        var self = this;
        $(this).minicolors({
            control: $(this).attr('data-control') || 'hue',
            defaultValue: $(this).attr('data-default-value') || '',
            inline: $(this).hasClass('inline'),
            letterCase: $(this).hasClass('uppercase') ? 'uppercase' : 'lowercase',
            opacity: $(this).hasClass('opacity'),
            position: $(this).attr('data-position') || 'default',
            styles: $(this).attr('data-style') || '',
            swatchPosition: $(this).attr('data-swatch-position') || 'left',
            textfield: !$(this).hasClass('no-textfield'),
            theme: $(this).attr('data-theme') || 'default',
            change: function (hex, opacity) {
                var color = $(this).minicolors('rgbaString');
                var parent = $(self).closest("div");
                if (parent.hasClass("bg-option")) {
                    $(".page-bg").css("background-color", color);
                } else if (parent.hasClass("header-option")) {
                    $(".header-bg").css("background-color", color);
                } else if (parent.hasClass("font-option")) {
                    $(".fonts-bg").css("color", color);
                } else if (parent.hasClass("footer-option")) {
                    $(".footer-bg").css("background-color", color);
                }
            }
        });

    });

});

关于jquery - 使用 jquery minicolors 更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610990/

相关文章:

android-studio - 无法解决: uz.shift:colorpicker:0.5

javascript - 为什么数组中没有填充 ajax 响应?

javascript - 在数据表中进行筛选时更新总和

android - 如何使用 devmil-android-color-picker?

javascript - HSL 颜色到色素沉着

javascript - 颜色选择器 Farbtastic 在 2 个类上同步

javascript - jQuery 如何将#output 传递给变量以在输入字段中使用

jquery - 如何在动画结束后才执行 Action ?

jquery - 光滑的轮播不适用于引导弹出窗口

javascript - Spectrum 颜色选择器不适用于 jQuery 对话框