Wednesday 6 June 2012

Rich checkbox





Demo:


http://jsfiddle.net/tjerabek/ft6UZ/


HTML:


<div class="input-block">
<label for="newsletter" class="switch-label">Send newsletter</label>
<input type="checkbox" name="newsletter" id="newsletter" checked="checked" />
</div>

CSS:


body{
    padding50px;
    font-familyArial;
    font-size100%;
}

.switch-label{
    line-height26px;
    displayblock;
    floatleft;
    margin-right10px;
}
.input-block{
    margin-bottom1em;
}

/* Rich checkbox */

.rich-checkbox{
    positionrelative;
    cursorpointer;
    displayinline-block;
    background#DDD;
    color#3f3f3f;
    width70px;
    
    text-shadow0px 1px 0px #f2f2f2;
    filterdropshadow(color=#f2f2f2offx=0offy=1);
      
    -webkit-border-radius15px;
    -moz-border-radius15px;
    border-radius15px;
    height26px;
    line-height26px;
    
    -webkit-box-shadowinset 0px 1px 2px 0px rgba(0000.2);
    box-shadowinset 0px 1px 2px 0px rgba(0000.2);
}
.rich-checkbox.on{
    background#C4E69F;
    color#3c5226;
}
.rich-checkbox .handler{
    displayblock;
    background#CCC;
    width28px;
    height28px;
    positionabsolute;
    margin-top-1px;
    -webkit-border-radius15px;
    -moz-border-radius15px;
    border-radius15px;
    -webkit-box-shadow0px 1px 1px 0px rgba(0000.3);
    box-shadow0px 1px 1px 0px rgba(0000.3)

-webkit-transitionall 0.3s ease;
-moz-transitionall 0.3s ease;
-ms-transitionall 0.3s ease;
-o-transitionall 0.3s ease;
transitionall 0.3s ease;   

backgroundrgb(255,255,255);
background-moz-linear-gradient(toprgba(255,255,255,10%rgba(235,235,235,1100%);
background-webkit-gradient(linearleft topleft bottomcolor-stop(0%,rgba(255,255,255,1))color-stop(100%,rgba(235,235,235,1)));
background-webkit-linear-gradient(toprgba(255,255,255,10%,rgba(235,235,235,1100%);
background-o-linear-gradient(toprgba(255,255,255,10%,rgba(235,235,235,1100%);
background-ms-linear-gradient(toprgba(255,255,255,10%,rgba(235,235,235,1100%);
backgroundlinear-gradient(toprgba(255,255,255,10%,rgba(235,235,235,1100%);
filterprogid:DXImageTransform.Microsoft.gradientstartColorstr='#ffffff'endColorstr='#ebebeb',GradientType=);    
}
.rich-checkbox.on .handler{
    left0;
}
.rich-checkbox.off .handler{
    left0;
    margin-left42px;
}
.rich-checkbox .on-state,
.rich-checkbox .off-state{
    text-transformuppercase;
    font-size0.875em;
    font-weightbold;
}
.rich-checkbox .on-state{
    padding-left35px;
    padding-right10px;
}
.rich-checkbox .off-state{
    padding-right35px;
    padding-left10px;
}
.rich-checkbox.on .on-state,
.rich-checkbox.off .off-state{
    displayblock;
}
.rich-checkbox.on .off-state
.rich-checkbox.off .on-state{
    displaynone;
}



jQuery plugin:


;
(function(${
    $.checkbox function(eloptions{
        var base this;

        base.$el $(el);
        base.el el;

        base.$el.data("checkbox"base);

        base.init function({
            base.options $.extend({}$.checkbox.defaultOptionsoptions);
        };

        // Run initializer
        base.init();
    };

    $.checkbox.defaultOptions {
        onchangefunction(state{}
    };

    $.fn.checkbox function(options{
        return this.each(function({
            var base (new $.checkbox(thisoptions));
            var $check $(this);
            $check.hide();

            var $richCheck $('<div class="rich-checkbox"></div>');
            var $handler $('<div class="handler"></div>');
            var $onState $('<div class="on-state">On</div>');
            var $offState $('<div class="off-state">Off</div>');

            if ($check.attr('checked'== 'checked'{
                $richCheck.addClass('on');
            else {
                $richCheck.addClass('off');
            }

            $richCheck.append($handler);
            $richCheck.append($onState);
            $richCheck.append($offState);

            $onState.click(function({
                var $parentNode $(this).parent();
                $parentNode.removeClass("on");
                $parentNode.addClass("off");
                $check.removeAttr('checked');
                base.options.onchange(false);
            });
            $offState.click(function({
                var $parentNode $(this).parent();
                $parentNode.removeClass("off");
                $parentNode.addClass("on");
                $check.attr('checked''checked');
                base.options.onchange(true);
            });
            $handler.click(function({
                var $parentNode $(this).parent();
                if ($parentNode.hasClass('on'){
                    $parentNode.removeClass("on");
                    $parentNode.addClass("off");
                    $check.removeAttr('checked');
                    base.options.onchange(false);
                else {
                    $parentNode.removeClass("off");
                    $parentNode.addClass("on");
                    $check.attr('checked''checked');
                    base.options.onchange(true);
                }
            });

            $check.after($richCheck);

            $check.change(function({
                if ($check.attr('checked'== 'checked'{
                    $richCheck.removeClass('off');
                    $richCheck.addClass('on');
                    base.options.onchange(true);
                else {
                    $richCheck.removeClass('on');
                    $richCheck.addClass('off');
                    base.options.onchange(false);
                }
            });
        });
    };

})(jQuery);

$(document).ready(function({
    $('input[type="checkbox"]').checkbox();
});


No comments:

Post a Comment