前言
dropdown list 是web应用中比较常用的一种控件,HTML标准提供的select标签的功能略显单薄,最大的缺点是不能定制图标,所以一般都是用div标签来模拟。又由于通用,所以有必要将其抽象成一个通用的控件,这样每次使用的时候只需要提供dropdown list 的数据模型即可。
效果图
收缩起来的效果
展开的效果
设计与实现
初步的设想是,提供一个标准的数据模型(data model),然后通过调用一个javascript函数,动态的画出dropdown list来,这个例子中使用的数据模型如下:
/**
* This is the list data model, defined
* the style of the list, and all items it contained.
*/
var listDataModel = {
style : {width:"440px", float:"left"},
contents : [
{content:"CrossTable", icon:"images/crosstable.gif"},
{content:"Table", icon:"images/table.gif"},
{content:"Label", icon:"images/label.gif"},
{content:"Image", icon:"images/image.gif"},
{content:"Chart", icon:"images/chart.gif"}
]
};
用一个内部的(私有的)函数构造dropdown list的panel,也就是下拉出来的那个panel,其中包含所有的item,类似于select控件的option,这个函数不必暴露给最终的使用者:
/**
* generate the dropdown panel, contains all list items
* and then fulfill the container.
* @dataModel data model of the list items
* @container container of all list items
*/
function buildDropDownList(dataModel, container){
var dropdownList = $("<div></div>").addClass("dropdownList_");
var dropdownPanel = $("<div></div>").addClass("dropdownPanel");
dropdownList.css("padding-left", "18px").text("Please Select a value");
dropdownPanel.hide();
dropdownList.click(function(){
dropdownPanel.toggle();
});
for(var i = 0;i < dataModel.length;i++){
var itemContainer = $("<div></div>").addClass("listItemContainer");
itemContainer.css("background", "url(" + dataModel[i].icon + ") no-repeat");
var item = $("<div></div>").addClass("listItem");
item.text(dataModel[i].content);
item.mouseover(function(){
$(this).addClass("selected");
}).mouseout(function(){
$(this).removeClass("selected");
}).click(function(){
dropdownList.text($(this).text());
dropdownList
.css("background", $(this).parent().css("background"))
.css("padding-left",$(this).parent().css("padding-left"));
dropdownPanel.hide();
});
dropdownPanel.append(itemContainer.append(item));
}
container.append(dropdownList).append(dropdownPanel);
}
然后,是我们需要暴露给用户的接口函数,这个函数接受一个参数,集数据模型,然后返回一个jQuery对象,调用者通常是一个container, 使用jQuery的话,可以很方便的使用container.append()将其插入到合适的位置:
/**
* this is the interface for end-user, to use this function, you should provide:
* @datamodel datamodel of the list object
*/
function dropdownList(dataModel){
var ddcontainer = $("<div></div>").addClass("dropdownlist");
for(var p in dataModel.style){ddcontainer.css(p, dataModel.style[p]);}
var layout =
$("<table></table>")
.attr({border:"0", cellspacing:"0", cellpadding:"0", width:"100%"});
var dropdown = $("<tr></tr>");
var listContainerTd = $("<td></td>").addClass("black");
var listContainer = $("<div></div>");
buildDropDownList(dataModel.contents, listContainer);
listContainer.appendTo(listContainerTd);
listContainerTd.appendTo(dropdown);
var ddicon =
$("<td></td>").css({width:"17px", align:"right"}).append(
$("<div></div>").attr("id", "ddicon").append(
$("<img />")
.css({width:"16px", height:"16px"})
.attr("src", "images/dropdownlist_arrow.gif")
)
);
ddicon.children(0).mouseover(function(){
$(this).children(0).attr("src", "images/dropdownlist_arrow_hov.gif");
}).mouseout(function(){
$(this).children(0).attr("src", "images/dropdownlist_arrow.gif");
}).click(function(){
listContainer.children(0).click();
});
ddicon.appendTo(dropdown);
dropdown.appendTo(layout);
layout.append(dropdown);
layout.appendTo(ddcontainer);
return ddcontainer;
}
代码中,为每个item(option)提供必要的事件处理器,比如当鼠标移过时高亮显示,当单击时将值放入list中,并隐藏panel等。
使用jQuery确实可以提高效率,比如创建DOM元素,并添加到节点上,注册事件,设置DOM元素的style等,非常方便。最后,当然是$(document).ready() :
/**
* execute those code when document tree is ready, it'll generate the
* dropdown list.
*/
$(document).ready(function(){
var container = $("#list");
container.append(dropdownList(listDataModel));
$("#click").click(function(){
alert("item ["+$("#list > div > table tr > td > div :first").text()+"] is selected");
});
});
到这里,这个dropdown list就已经完成了,看看如何使用
使用
使用起来很简单,需要在你的页面中指定一个dropdown list的容器div就可以了:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>yalist</title>
<link rel="stylesheet" href="style/yalist.css" type="text/css">
<script src="js/jquery-1.3.2.min.js" type="text/javascript" ></script>
<script src="js/yalist.js" type="text/javascript" ></script>
</head>
<body>
<div id="page" class="page">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td>
<div id="list"></div>
</td>
</tr>
<tr>
<td>
<input
type="button"
name="click"
value="show item value"
id="click">
</td>
</tr>
</table>
</div>
</body>
</html>
okay, 这个dropdown list的介绍就到这里,有时间的话可以对table, text panel之类的做一些封装,方便使用,可以在一定程度上提高开发效率。
jQuery真是个好东西,不但支持全部的CSS3的选择器,而且支持一些自定义的选择器,如XPath选择器,重要的,最重要的是,在浏览器的兼容性方面的支持,这才是WEB开发中最令人头疼的问题。代码附在附件中,有需要的可以去看看(此list名叫yalist ,意为 yet another list)。
- 大小: 2.3 KB
- 大小: 5.1 KB
分享到:
- 2009-08-20 21:28
- 浏览 4678
- 评论(3)
- 论坛回复 / 浏览 (2 / 7161)
- 查看更多
相关推荐
资源分类:Python库 所属语言:Python 资源全名:django-cascading-dropdown-widget-0.2.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
前端项目-angularjs-dropdown-multiselect,这个指令为您提供了一个具有AngularJS指令功能的引导下拉列表。
前端项目-country-region-dropdown-menu,使用javascript创建的下拉菜单,允许用户轻松实现国家/地区下拉列表。
jquery 代码实现 效果很不错 移动开发类
前端项目-myforce-angularjs-dropdown-multiselect,这个指令为您提供了一个具有AngularJS指令功能的引导下拉列表。
bower install bootstrap-dropdown-on-hover 或下载或。 在您的网页中: < script src =" jquery.js " > </ script > < script src =" dist/bootstrap-dropdown-on-hover.min.js " > </ ...
在一个html文件中 < html > < head > < script type =" module " > import '@polymer/paper-dropdown-menu/paper-dropdown-menu.js' ; import '@polymer/paper-item/paper-item.js' ; import...
dropdownmenu一个写好的微信小程序帅选条件框,多条件筛选,下拉菜单,直接引用组件就可以 读懂并灵活运用必须具有html,css ,js基础,除此之外还必须阅读小程序官方文档对组件的封装与使用相关文档
css-jquery-dropdown-menu-like-bootstrap css-jquery-dropdown 菜单类似于 bootstrap 下拉菜单样式
android-dropdown-menu An dropdown menu for android, you can attach your own listView(s) with this. 文档 DropdownMenu Demo Usage You can use this to make several(as you want) listViews act like ...
主要介绍了解决vue使用vant下拉框van-dropdown-item 绑定title值不变问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
安装npm install -S @jswork/react-ant-dropdown-menu特性姓名类型必需的默认描述班级名称细绳错误的-- menuOptions 形状错误的--用法导入css @import " ~@jswork/react-ant-dropdown-menu/dist/style.css " ;...
主要介绍了vant 自定义 van-dropdown-item的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
前端项目-jquery-cascading-dropdown,用于创建级联下拉列表的简单而轻量级的jquery插件。
安装npm i @ innologica / vue-dropdown-menu-保存或毛线添加@ innologica / vue-dropdown-menu import可以使用:import来自'@ innologica / vue-dropdown-menu'演示的DropdownMenu演示演示和文档位于...
paper-dropdown-menu, 材质设计浏览器选择元素 <paper-dropdown-menu>材质设计:下拉菜单paper-dropdown-menu 与本机浏览器选择元素类似。 paper-dropdown-menu 可以用于可以选择的内容。<pa
易于使用。 安装 npm i react-native-dropdown-menu --save 或者 yarn add react-native-dropdown-menu 用法 import DropdownMenu from 'react-native-dropdown-menu' ; export default class Demo extends ...
例如在document.body 自动位置小捆安装npm install --save react-dropdown-select网站动机react-select非常好,但是有时项目要求超出了它的能力范围用法进口: import Select from "react-dropdown-select";...
网页首页,通用[二级菜单]模板,下拉菜单项的下拉箭头有翻转的动画特效。完整源代码,仅供参考学习,乐在分享。
unity-ugui-dropdown-示例资源包