源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

select标记美化--JS式插件、后期加载

  • 时间:2020-11-28 10:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:select标记美化--JS式插件、后期加载
<select>标签的外观问题很恼人,各个浏览器都不一致,单单就IE,一个版本就一个长相,还不能用CSS修饰。 在这将本人对<select>的美化方法共享出来。 优点: 仍保留使用<select>,仅改变外观,不改变不干预Form行为,后期加载JS。(注:本脚本依赖jQuery) 啥也不说了,都在代码里。 效果图在底部。
[url=javascript:void(0);]                        .css("color", item.style.color)                         .addClass(item.className)                         .html(item.text)                         .appendTo(div);                     if (i == select.selectedIndex) {                         a.addClass("selected");                     }                     //当选项被点击时,<input> 内容显示为对应 <option>,关闭 <div> 层,同时将事件冒泡给原来的 <select>                     a.click(function () {                         var n = $(this).index();                         select.selectedIndex = n;                         input.val(select.options[n].text);                         div.hide();                         $(select).change();                     });                 }                 //在这里我们判断一个特殊的class名 "noscroll"                 //当选项过多时,默认会让选项列表出现滚动条;但如果有 .noscroll 修饰,则强制不出现滚动条                 var noscroll = (select.options.length < 10 || $(select).hasClass("noscroll"));                 if (/msie 6/i.test(window.navigator.userAgent)) {                     div.css("height", noscroll ? "auto" : "215px").css("overflow-y", noscroll ? "hidden" : "scroll");                 } else {                     div.css("max-height", noscroll ? "10000px" : "215px");                 }                 //在这里我们判断一个特殊的class名 "onside"                 //如果有 .onside 修饰,弹出的选项层将在侧面,否则是在下面                 //注: 此处用到2个函数 locateBeside 和 locateBelow 是本人js库中的方法,稍等另外给出                 $(select).hasClass("onside")                     ? div.locateBeside(this, -2)                     : div.locateBelow(this, -4);                 //对反复点击 <input> 之类的事情,做一些智能调节                 if (window.activeDummySelect == select) {                     div.slideToggle(100);                 } else {                     div.hide().slideDown(100);                     window.activeDummySelect = select;                 }                 //在有滚动条的情况下,我们需要将滚动条滚动到当前选中项的位置                 if (!select.selectedIndex > 6 && div[0].scrollHeight > div.height()) {                     div.scrollTop((select.selectedIndex - 3) * div[0].firstChild.offsetHeight);                 }             });         });         //最后别忘了:点击网页上的游离区域时,应该隐藏<div #dummydata>         $(document).click(function (e) {             if (!$(e.target).is(".dummy") && !$(e.target).is("#dummydata")) {                 $("#dummydata").hide();             }         });     } });
上面代码里说用到了2个方法: locateBeside 和 locateBelow, 是本人js库中对 jQuery 的扩展,顺便多赠送2个方法 locate 和 locateCenter :-)  代码如下:
最后给出一些样式表定义的例子,以及演示效果:
[u]复制代码[/u] 代码如下:
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; }
 
上面样式定义的效果图 [img]http://files.jb51.net/file_images/article/201304/201304011409562.jpg[/img]  [img]http://files.jb51.net/file_images/article/201304/201304011409563.jpg[/img] [img]http://files.jb51.net/file_images/article/201304/201304011409564.jpg[/img] html中要做的,只是加几个class修饰 [img]http://files.jb51.net/file_images/article/201304/201304011409565.jpg[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部