select标记美化--JS式插件、后期加载 |
本文标签:select标记,美化 <select>标签的外观问题很恼人,各个浏览器都不一致,单单就IE,一个版本就一个长相,还不能用CSS修饰 。 在这将本人对<select>的美化方法共享出来 。 啥也不说了,都在代码里 。 复制代码 代码如下: $(document).ready(function () { // 找出需要美化的<select>标记,我们用一个class名称 "beautify" 来确定,没有这个样式的<select>则将被忽略 //先在代码底部增加一个<div>,用来承载和显示下拉框选项 //挨个美化呗 //给本函数下的 this (也就是 <select>) 设置一个别名,在下面的匿名函数中将会被用到 //创建一个 <input> , .dummy 将用于我们对此类 <input> 进行专门样式定义 //将 <select> 藏掉,不要在 .beautify 中去定义 display:none, 因为js加载失败时,我们还得用上它 // 当 <input class=dummy> 被点击时 //设置 <div> 的宽度 //将 <option> 复制到 <div id=dummydata> 里面,一个 <option> 对应一个 <a> 标记 //在这里我们判断一个特殊的class名 "noscroll" //在这里我们判断一个特殊的class名 "onside" //对反复点击 <input> 之类的事情,做一些智能调节 //在有滚动条的情况下,我们需要将滚动条滚动到当前选中项的位置 //最后别忘了:点击网页上的游离区域时,应该隐藏<div #dummydata> 上面代码里说用到了2个方法: locateBeside 和 locateBelow, 是本人js库中对 jQuery 的扩展,顺便多赠送2个方法 locate 和 locateCenter 复制代码 代码如下: $.fn.extend({ locate: function (x, y) { if (this.css("position") == "fixed") { y -= $(document).scrollTop(); } return this.css({ left: x, top: y }); }, locateBeside: function (el, adjustX) { var p = $(el).offset(), w1 = $(el).outerWidth(), w2 = this.outerWidth(), h2 = this.outerHeight(), x = p.left + w1 + 5 + (adjustX || 0), y = p.top; if ($(document).width() < x + w2) { x = p.left - w2 - 5 - (adjustX || 0); } if ($(document).height() < y + h2) { y = p.top - (y + h2 + 15 - $(document).height()); } return this.locate(x, y); }, locateBelow: function (el, adjustY) { var p = $(el).offset(); return this.locate(p.left, p.top + $(el).outerHeight() + 3 + (adjustY || 0)); }, locateCenter: function () { return this.locate( ($(window).width() - this.width()) / 2, ($(window).height() - this.height()) / 2 + $(document).scrollTop() ); } }); 最后给出一些样式表定义的例子,以及演示效果: 复制代码 代码如下: input.dummy { background-image: url(/static/images/combo.gif); background-position: right 12px; background-repeat: no-repeat; cursor: pointer !important; } input.dummy:hover, input.dummy:focus { background-image: url(/static/images/combo_hover.gif); } #dummydata { position: absolute; z-index: 20; border: 1px solid #a4601e; background-color: #393939; max-height: 200px; overflow: auto; } #dummydata a { display: block; color: #ddd; line-height: 25px; text-indent: 3px; text-overflow: ellipsis; } #dummydata a:hover { color: #198cef; text-decoration: none; } #dummydata.matrix { width: 208px; padding: 5px; } /* matrix 效果 */ #dummydata.matrix a { float: left; width: 33%; } #dummydata.matrix-large { width: 640px; padding: 5px; } /* matrix-large 效果 */ #dummydata.matrix-large a { float: left; width: 25%; } #dummydata a.fullwidth { float: none; } #dummydata a.delimiter { float: none; width: 100%; height: 10px; visibility: hidden; } #dummydata a.selected { color: yellow; } 上面样式定义的效果图
html中要做的,只是加几个class修饰 |