【参加讨论】 第三,图片下拉列表框的滚动,要保证自动滚动到相应的图片出现在列表框的顶端:当手动滚动停止时,如果滚动条趋势是向下滚动,则自动继续向下滚动直至最近的图片在列表框的顶端,反之亦然。
例子中我们将所有供选图片命名为“0.gif”、“1.gif”……“9.gif”,并放在“head”目录下。下面我们以实际程序解决这些问题:
<!--建立表单-->
<form name="myform" method="post" action="">请选择图片:
<div id="imgbox"><!--这里是图片下拉选择器的位置--></div>
<input name="myhead" type="text" id="myface" size="10" value="">
</form> <script language="javascript">
<!--
var imgwidth=64; //列表框中图片的宽度;
var imgheight=64; //列表框中图片的高度;
var imgsrc="http://www.iyit.net/files/beyondpic/2006-5/5/065510010118655.gif"; //供选图片的路径。"数字序号"四字将在程序中替换为0,1,2...,不要手工更改为数字 ;
var selectedno=7; //默认选定的图片序号;
var selectesize=2; //下拉列表框中显示的图片数;
/* 以下把图片和层输出在"imgbox"的位置:
我们用变量"isin"记录鼠标是否在该控件上;用css定义下拉列表框的滚动条,"overflow-x:hidden;overflow-y:scroll;"指水平方向不滚动,竖直方面滚动;
用for()循环依次把图片输出到下拉列表框。
*/
var myhtml='<span onmouseover="isin=true" onmouseout="isin=false">';
myhtml+='<table width="1" onclick="showlist(this)" title="选择提示框" border="0" cellspacing="0" cellpadding="0"><tr><td><img name="imgselected" border=1 src="'+imgsrc.replace("数字序号",selectedno)+'" width='+(imgwidth)+' height='+imgheight+'></td><td valign=top><img src="http://www.iyit.net/files/beyondpic/2006-5/5/065510010161854.gif"></td></tr></table>';
myhtml+='<div onscroll="scrollud()" id="imgbox" \n';
myhtml+='style="position:absolute;left=-800;top=0;background-color:#ffffff;border: 1px solid #000000;overflow-x:hidden;overflow-y:scroll; width:'+(imgwidth+20)+'px; height: '+imgheight*selectesize+'px">';
for(i=0;i<10;i++){
myhtml+="<img listid="+i+" src='"+imgsrc.replace("数字序号",i)+"' alt='"+imgsrc.replace("数字序号",i)+"' width="+imgwidth+" height="+imgheight+" onclick='selectme(this)' onload='if(init)init()'><br>";
}
myhtml+= "</div></span>";
imgbox.outerhtml=myhtml;
/* 以下控制下拉列表框的出现或隐藏 */
function showlist(obj){//这个函数将在选择提示框点击时激活
//如果列表框已经出现,这次点击则隐藏:
if(imgbox.style.pixelleft!=-800){imgbox.style.pixelleft=-800; return;}
//读取选择提示框在窗体中的绝对位置:
//在父容器中的相对位置:
var mytop=obj.offsettop;
var myleft=obj.offsetleft;
//依次读取父容器在更高一级父容器中的相对位置:
while(obj=obj.offsetparent){
myleft+=obj.offsetleft;
mytop+=obj.offsettop;
}
//现在已经得到选择提示框的绝对位置myleft和mytop。
//下拉列表拉就出现在这个绝对位置的正下方:
imgbox.style.left=myleft;
imgbox.style.top=mytop+imgheight+2;
}
var isin=false;
function selectme(obj){ //这个函数将在窗体点击或选定图片时激活,窗体点击中传递的参数是null。
if(!isin||obj){imgbox.style.pixelleft=-800;} //隐藏列表框
if(obj){
//改变选择提示框中的图片和"myhead"输入框中的值:
//listid是对象的自定义属性,即在对象的标签中定义了这个属性值,现在就可以读取或改变。
myform.myhead.value=imgsrc.replace("数字序号",obj.listid);
document.images["imgselected"].src=imgsrc.replace("数字序号",obj.listid);
}
}
/* 以下控制下拉列表框的自动滚动 */
var mytime=settimeout("",0);
var pre_x=0; //这个变量用来记录滚动前滚动条的位置
function scrollud(){ //本函数在下拉列表框滚动时被激活
var current_x=imgbox.scrolltop; //记录函数激活时滚动条的位置
//判断向上还是向下滚动,并自动滚动到"imgheight"的整数倍。
//我们改变列表框的scrolltop值时,会自动激发其onscroll事件,循环激活本函数。
if(current_x>pre_x && imgbox.scrolltop< math.ceil(imgbox.scrolltop/imgheight)*imgheight){
cleartimeout(mytime);
mytime=settimeout("imgbox.scrolltop=math.round(imgbox.scrolltop+1);",1);
}
else if(current_x<pre_x && imgbox.scrolltop> math.floor(imgbox.scrolltop/imgheight)*imgheight){
cleartimeout(mytime);
mytime=settimeout("imgbox.scrolltop=math.round(imgbox.scrolltop-1);",1);
}
pre_x=current_x;
}
//使列表框自动滚动到默认选定的图片位置,并给"myhead"输入框以默认值
function init(){ //本函数在每个列表框中的图片加载时激活
imgbox.scrolltop=selectedno*imgheight;
myform.myhead.value=imgsrc.replace("数字序号",selectedno);
}
/* 以下使窗体点击时selectme(obj)函数被激活: */
myactivation="selectme(null)";
if(document.body.onclick)
{
eval(document.body.onclick.tostring().replace('anonymous()','bodyclick()'));
document.body.onclick=new function("bodyclick();"+myactivation);
}
else document.body.onclick=new function(myactivation);
-->
</script>
程序最后“使窗体点击时selectme(obj)函数被激活”部分可以用document.body.onclick=new function("selectme(null)")简单替换之。但为了不和其它程序的窗体点击事件冲突,我们用了这段代码。