欢迎光临  laozi12345的blog
Banner|栏目
New BLog|日志
New Reply|回复
New message|留言
User Login|登陆
BLog Search|搜索
BLog Info|信息
  • 日志:8
  • 评论:0
  • 留言:0
  • 访问:
My Links|收藏



 

自制列表框
laozi12345 发表于 2008-1-22 17:09:00

<title>自制列表框</title>
<style>
.selectDiv { border: 2px solid inset buttonface;}
.optionDiv { border:1px solid black;border-top:0px;;visibility:hidden;}
.optionDiv div { font-size:11px;font-family:Tahoma;padding-left:8px;line-height:160%;cursor:default;width:100%;}
.defaultSelect { font-size:11px;font-family:Tahoma;text-align:center;border:1px solid white;cursor:default;width:77px;}
.arrow { font-family:webdings;line-height:13px;border:2px outset buttonhighlight;background-color:buttonface;width:15px;text-align:center;cursor:default;font-size:8px;}
</style>
< LANGUAGE="Java" DEFER>
//用户变量
var oWhere = body;
var OptionText = new Array();
OptionText[0] = "首页推荐";
OptionText[1] = "CSDN.net";
OptionText[2] = "蓝色理想";
OptionText[3] = "PcHome";
OptionText[4] = "MSDN.com";
//下拉菜单主体
var selectDiv = createElement("table");
var selectDivTR = selectDiv.insertRow();
var defaultTD = selectDivTR.insertCell();
var arrow = selectDivTR.insertCell();
with(selectDiv)cellSpacing=0,cellPadding=0,border=0,className="selectDiv";
with(defaultTD)innerText = OptionText[0],className="defaultSelect";
with(arrow)innerText=6,className="arrow";
oWhere.appendChild(selectDiv);
//外层Div
var optionDiv = createElement("div");
//设置下拉菜单选项的坐标和宽度
with(optionDiv.style) {
 var select = selectDiv;
 var xy = getSelectPosition(select);
 pixelLeft = xy[0];
 pixelTop = xy[1]+select.offsetHeight;
 width = selectDiv.offsetWidth;
 optionDiv.className = "optionDiv";
}
//下拉菜单内容
var Options = new Array();
for (var i=0;i<OptionText.length;i++) {
 Options[i] = optionDiv.appendChild(createElement("div"));
}
for (i=0;i<Options.length;i++) {
 Options[i].innerText = OptionText[i];
}
oWhere.appendChild(optionDiv);

/*事件*/
//禁止选择文本
selectDiv.onselectstart = () {return false;}
optionDiv.onselectstart = () {return false;}
//下拉菜单的箭头
arrow.down = () {
 this.setCapture();
 this.style.borderStyle="inset";
}
arrow.up = () {
 this.style.borderStyle="outset";
 this.releaseCapture();
}
arrow. = () {
 event.cancelBubble = true;
 optionDiv.style.visibility = optionDiv.style.visibility=="visible"?"hidden":"visible";
}
= () {
 optionDiv.style.visibility = "hidden";
}
defaultTD. = () {
 event.cancelBubble = true;
 optionDiv.style.visibility = optionDiv.style.visibility=="visible"?"hidden":"visible";
}
//移动Option时的动态效果
for (i=0;i<Options.length;i++) {
 Options[i].attachEvent("over",(){moveWithOptions("highlight","white")});
 Options[i].attachEvent("out",(){moveWithOptions("","")});
 Options[i].attachEvent("up",selectedText);
}
moveWithOptions(bg,color) {
 with(event.srcElement) {
  style.backgroundColor = bg;
  style.color = color;
 }
}
selectedText() {
 with(event.srcElement) {
  defaultTD.innerText = innerText;
 }
 with(defaultTD.style)background="highlight",color="white";
}
/*通用函数*/
//获取对象坐标
getSelectPosition(obj) {
 var objLeft = obj.offsetLeft;
 var objTop = obj.offsetTop;
 var objParent = obj.offsetParent;
 while (objParent.tagName != "BODY") {
  objLeft += objParent.offsetLeft;
  objTop += objParent.offsetTop;
  objParent = objParent.offsetParent;
 }
 return([objLeft,objTop]);
}
</>

阅读全文 | 回复(0) | 引用通告 | 编辑
  • 标签:自制列表框 
  • 发表评论:

      大名:
      密码:
      主页:
      标题:
      载入中
    © laozi12345的blog All rights reserved.   
    Powered by f31.net.