自定义图像作为单选按钮

时间:2012-06-14 00:03:43

标签: javascript html css

我一直在尝试使用HTML,CSS和JavaScript制作自定义单选按钮。我一直在查看之前关于单选按钮的问题,我觉得我已经实现了这些建议,但我的jpeg图像仍然被削减了一半。我显然错过了什么,但不确定它是什么。下面是我的HTML CSS和JavaScript。任何投入将不胜感激。

<style TYPE="text/css">
.has-js .label_check,
.has-js .label_radio { padding-left: 40px;}
.has-js .label_radio {
background: url(images/radio_button-21.jpg) no-repeat;
width: 33px;
height: 35px;}

.has-js .label_check { background: url(check-off.png) no-repeat; }
.has-js label.c_on { background: url(check-on.png) no-repeat; }
.has-js label.r_on { 
background: url(images/radio_button_selected.jpg) no-repeat;
width: 33px;
height: 35px;}

.has-js .label_check input,
.has-js .label_radio input { position: absolute; left: -9999px;}

</style>

</head>

<label class="label_radio" for="radio-01">
    <input name="sample-radio" id="radio-01" value="1" type="radio" />
    Yes
</label>


<br />
<br />

<label class="label_radio" for="radio-02">
    <input name="sample-radio" id="radio-02" value="2" type="radio" />
    No
</label>

<br />
<br />

<label class="label_radio" for="radio-03">
    <input name="sample-radio" id="radio-03" value="3" type="radio" />
    Maybe
</label>


</body>

JavaScript代码

onload = function() {

    var body = gebtn(d,'body')[0];
    body.className = body.className && body.className != '' ? body.className + ' has-js' : 'has-js';

    if (!d.getElementById || !d.createTextNode) return;
    var ls = gebtn(d,'label');
    for (var i = 0; i < ls.length; i++) {
        var l = ls[i];
        if (l.className.indexOf('label_') == -1) continue;
        var inp = gebtn(l,'input')[0];
        if (l.className == 'label_check') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
            l.onclick = check_it;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            l.onclick = turn_radio;
        };
    };
};
var check_it = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
        this.className = 'label_check c_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_check c_off';
        if (safari) inp.click();
    };
};
var turn_radio = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = gebtn(this.parentNode,'label');
        for (var i = 0; i < ls.length; i++) {
            var l = ls[i];
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.click();
    };
};

3 个答案:

答案 0 :(得分:2)

很难说无法运行代码,或者至少看到它的外观截图。但是听起来我的单选按钮(或其周围的包含元素)的宽度和/或高度对于您正在使用的图像的尺寸来说太小了。如果是这样,指定适当宽度或高度的CSS声明应该修复它。

编辑:要解决这些问题,我喜欢在Chrome浏览器中完成工作,因此我可以使用其内置的开发人员工具。在Chrome中,我可以右键单击元素并选择“Inspect Element”。这使我能够浏览DOM并查看CSS说明符对每个元素的影响,并通过实验调整它们直到我诊断出问题为止。

答案 1 :(得分:1)

确保您的图像文件与单选按钮的大小相同。

无论如何,我不明白为什么会有JavaScript。它可以是纯CSS:
http://jsfiddle.net/DerekL/9MtMx/

input[type="radio"]{
    -webkit-appearance:none;
    -moz-appearance:none;
    border-radius:8px;
    border:1px solid #dbdbdb;
    width:16px;
    height:16px;
    cursor:pointer;
    margin:0 1px;
    background-image: -webkit-gradient(linear, left top, left bottom, from(white), to(#f2f2f2));
    vertical-align:bottom;
}

input[type="radio"]:checked{
    background-position:0px 0px;
    background-image: url('checked.png'), -webkit-gradient(linear, left top, left bottom, from(white), to(#f2f2f2));
}

答案 2 :(得分:0)

这样做的CSS方式很简单但不幸的是我们仍然需要忍受IE 6,7和8这可能不支持这个。最好的方法是解决一个JS方法,只针对不兼容的浏览器(IE !!!!),然后仍然使用CSS。