|
|
Posted on
2005-03-18 19:24
冷风.net
阅读( 625)
评论()
收藏
举报
<!--建立表單-->
<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="head/數字序號.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="menu.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>
|