我正在尝试创建一个表单,用户可以在其中选择选项,而不是使用复选框,我希望他们能够选择图像。
当他们选择图像时,他们周围可能会有一个边框,表明他们已被选中。
哪种类型的输入处理这个?
由于复选框是选中/未选中的默认 HTML 行为,我会这样做:
这将具有额外的优势,即禁用 Javascript 的人仍然可以通过单击复选框来选择图像。
您可以使用 jQuery 插件,http://ajaxdump.com/?0BNm4jJ0
您可以反过来使用(仅在 chrome 中测试):before
和图像:
演示:http: //jsfiddle.net/SO_AMK/hG9Fh/
不,我不只是从头顶上输入所有的 CSS:D
CSS,这段代码纯粹是为 Chrome 构建的,在任何情况下都不兼容跨浏览器:
input[type='checkbox'] {
-webkit-appearance: none;
-webkit-user-select: none;
background-image: -webkit-linear-gradient(#ededed, #ededed 38%, #dedede);
border: 1px solid rgba(0, 0, 0, 0.25);
border-radius: 2px;
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08),
inset 0 1px 2px rgba(255, 255, 255, 0.75);
color: #444;
font: inherit;
margin: 0 1px 0 0;
text-shadow: 0 1px 0 rgb(240, 240, 240);
}
input[type='checkbox'] {
bottom: 2px;
height: 13px;
position: relative;
vertical-align: middle;
width: 13px;
}
input[type='checkbox']:checked::before {
-webkit-user-select: none;
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9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uQmCC');
background-size: 100% 100%;
content: '';
display: block;
height: 100%;
width: 100%;
}
:enabled:hover:input[type='checkbox'] {
background-image: -webkit-linear-gradient(#f0f0f0, #f0f0f0 38%, #e0e0e0);
border-color: rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12),
inset 0 1px 2px rgba(255, 255, 255, 0.95);
color: black;
}
:enabled:active:input[type='checkbox'] {
background-image: -webkit-linear-gradient(#e7e7e7, #e7e7e7 38%, #d7d7d7);
box-shadow: none;
text-shadow: none;
}
input:disabled:[type='checkbox'] {
opacity: .75;
}
:enabled:focus:input[type='checkbox'] {
-webkit-transition: border-color 200ms;
border-color: rgb(77, 144, 254);
outline: none;
}
将复选框替换为触发它们 onclick 的图像,这里有一篇很好的文章:http ://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/
使用按钮。您可以使用用户将看到的背景图像,并使用元素的值处理表单。
再见