【发布时间】:2009-09-24 12:04:33
【问题描述】:
是否可以使用 YUI 将我的所有输入框更改为圆角?我不能使用背景图像,因为输入的宽度是可变的,并且我不能在它们周围添加 div,因为会生成一些输入元素。此外,我不能使用边框半径或任何 moz/webkit 变体,因为它需要在 IE6 中显示相同。
感谢任何指点,谢谢。
【问题讨论】:
标签: javascript css yui
是否可以使用 YUI 将我的所有输入框更改为圆角?我不能使用背景图像,因为输入的宽度是可变的,并且我不能在它们周围添加 div,因为会生成一些输入元素。此外,我不能使用边框半径或任何 moz/webkit 变体,因为它需要在 IE6 中显示相同。
感谢任何指点,谢谢。
【问题讨论】:
标签: javascript css yui
有multiple techniques to make cross-browser rounded corners,并且YUI当然可以用于即时转换input元素,如果需要添加包装器divs以支持您选择使用的方法。
这里是 YUI 3 的圆角实现 text-type input,使用角的背景图像:
<html>
<head>
<title>Stack Overflow 1471254</title>
<link rel="stylesheet" type="text/css" href="roundMyCorners.css">
<script type="text/javascript" src="http://yui.yahooapis.com/3.0.0b1/build/yui/yui-min.js"></script>
</head>
<body>
<p>Here is an input box: <input type="text" value="type stuff" class="roundMyCorners"> Thanks!</p>
<script type="text/javascript">
YUI({combine: true, timeout: 10000}).use("node", function(Y) {
Y.all('body input.roundMyCorners').each(function(rcInput) {
var outerDiv = Y.Node.create('<div class="roundMyCornersOuterDiv"></div>');
outerDiv.appendChild(Y.Node.create('<div class="tl"></div>'));
outerDiv.appendChild(Y.Node.create('<div class="tr"></div>'));
outerDiv.appendChild(rcInput.cloneNode());
outerDiv.appendChild(Y.Node.create('<div class="bl"></div>'));
outerDiv.appendChild(Y.Node.create('<div class="br"></div>'));
rcInput.get('parentNode').replaceChild( outerDiv, rcInput );
});
});
</script>
</body>
</html>
这是 CSS 文件。出于演示目的,我(有点粗鲁地)在这段代码中从 site that has a rounded corner demo 热链接 PNG。当然,最好为您的网站制作自己的图片。
.roundMyCorners {
width: 12em;
border: none;
font-weight: bold;
color: white;
background-color: #3f6daf;
}
.roundMyCornersOuterDiv {
position: relative;
display: -moz-inline-stack; /* inline-block for older Gecko */
display: inline-block;
*zoom: 1; /* force hasLayout for IE */
*display: inline; /* rendered as inline-block in IE after hasLayout */
vertical-align: middle;
padding: 6px;
color: white;
background-color: #3f6daf;
}
.roundMyCornersOuterDiv .tl {
position: absolute;
width: 6px;
height: 6px;
background: url(http://www.bestinclass.com/images/ui/rounded/colhead-tl.png) top left no-repeat;
top: 0;
left: 0;
}
.roundMyCornersOuterDiv .tr {
position: absolute;
width: 6px;
height: 6px;
background: url(http://www.bestinclass.com/images/ui/rounded/colhead-tr.png) top right no-repeat;
top: 0;
right: 0;
}
.roundMyCornersOuterDiv .bl {
position: absolute;
width: 6px;
height: 6px;
background: url(http://www.bestinclass.com/images/ui/rounded/colhead-bl.png) bottom left no-repeat;
bottom: 0;
left: 0;
}
.roundMyCornersOuterDiv .br {
position: absolute;
width: 6px;
height: 6px;
background: url(http://www.bestinclass.com/images/ui/rounded/colhead-br.png) bottom right no-repeat;
bottom: 0;
right: 0;
}
当然,tl、tr、bl、br 甚至 roundMyCornersOuterDiv 类的样式都可以通过 JavaScript 设置。为了清楚起见,我将它们留在 CSS 中。
请注意,如果您想更改所有 input 元素,只需将初始选择器从“body input.roundMyCorners”更改为“input”即可。但是,我不希望这对checkbox 和radio 类型的input 很好地工作,所以如果你想避免在任何地方标记类名,也许'input[type="text"]' 是一个更好的选择器。
另一个注意事项:由于input 是一个内联元素,我希望包装器div 是inline-block。这对于popular techniques for table-free form layouts 是必不可少的。不幸的是,这需要一些专有的调整。
最后,如果您不想对 CSS 大惊小怪,也不想维护自己的 YUI/jQuery/任何代码,您可以尝试 Nifty Corners 或 Curvy Corners,它们是声称可以做这类事情的 JavaScript 库自动地,至少对于divs。您的里程可能会有所不同。
【讨论】:
可能是一个选择器来查找输入,然后用输入周围的 div 替换它们?
【讨论】: