图片下拉选择器的制作(2)
第三,图片下拉列表框的滚动,要保证自动滚动到相应的图片出现在列表框的顶端:当手动滚动停止时,如果滚动条趋势是向下滚动,则自动继续向下滚动直至最近的图片在列表框的顶端,反之亦然。
: ~+ G& c/ v3 G# j2 i" j6 P* n- C" e; M0 r: C
例子中我们将所有供选图片命名为“0.gif”、“1.gif”……“9.gif”,并放在“head”目录下。下面我们以实际程序解决这些问题:
( l% e5 f I3 s0 I
- \# f1 c) t |!--建立表单--0 Q9 c1 h5 ~- e s1 n: G, {
form name="myform" method="post" action=""请选择图片:
0 |% S! ]5 q, P f+ }2 P* `9 Jdiv id="imgBox"!--这里是图片下拉选择器的位置--/div
( F) U5 f$ y ginput name="myhead" type="text" id="myface" size="10" value=""1 [" i$ x7 c+ I K* {$ r
/form
8 a7 b- d ?; Z0 b2 u$ @' Y: ?# @% g5 ~9 r6 J1 ?
script language="JavaScript"
4 f4 { e u8 W$ p. _!--
7 N3 _5 f& U! m" f: A& avar imgWidth=64; //列表框中图片的宽度;% q' ` i8 e, ]
var imgHeight=64; //列表框中图片的高度;% \0 Z6 H6 y# o2 O1 ]
var imgSrc="head/数字序号.gif"; //供选图片的路径。"数字序号"四字将在程序中替换为0,1,2...,不要手工更改为数字 ;
# [2 O. r8 _* y# W( i9 U+ x: P0 ]var selectedNo=7; //默认选定的图片序号;
0 Y# p5 ~# W" @7 Z' W. w6 d5 }0 ovar selecteSize=2; //下拉列表框中显示的图片数; * G; G: `4 m& ?0 j4 s) `
: `" _; ^1 }" ]2 J, ~$ H
1 [3 |1 D8 M, }/* 以下把图片和层输出在"imgBox"的位置:
2 e/ G$ [, ]- _. M8 w1 C2 W! V我们用变量"isin"记录鼠标是否在该控件上;用CSS定义下拉列表框的滚动条,"overflow-x:hidden;overflow-y:scroll;"指水平方向不滚动,竖直方面滚动;
7 R' u, |1 q, V/ e9 ?" |用for()循环依次把图片输出到下拉列表框。
L2 z& t, V( m" V*/
: A9 a' N& x! T' ]! I, m. r9 U `var myHTML='SPAN onmouseout="isin=false"'; e4 I; Q q3 p# k6 ^
myHTML ='table width="1" title="选择提示框" border="0" cellspacing="0" cellpadding="0"trtdimg name="imgselected" border=1 src="' imgSrc.replace("数字序号",selectedNo) '" WIDTH=' (imgWidth) ' HEIGHT=' imgHeight '/tdtd valign=topimg src="menu.gif"/td/tr/table';
9 R( U2 B5 w/ BmyHTML ='DIV onscroll="scrollud()" id="imgBox" \n';8 s9 r8 o) ?% P" b
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"';' `0 y3 T ^ s: d
for(i=0;i10;i ){
# m+ E w H! I% M5 V9 [/ m" t! @2 O) C0 emyHTML ="img listID=" i " src='" imgSrc.replace("数字序号",i) "' alt='" imgSrc.replace("数字序号",i) "' width=" imgWidth " height=" imgHeight " onclick='selectme(this)' onload='if(init)init()'BR";
1 | _5 {1 S3 \7 L4 N" V' W}
* i/ Z0 l9 t' p. QmyHTML = "/DIV/SPAN";
$ F+ e4 D. F5 Z! f! t8 E7 r# S bimgBox.outerHTML=myHTML;
( n- ^4 d+ I0 K6 T" b/ L: f! v W+ K
3 |4 \+ {- n- N) g/* 以下控制下拉列表框的出现或隐藏 */0 d/ T" m* G) c8 c
function showlist(obj){//这个函数将在选择提示框点击时激活
2 s" z: {* d1 B1 O( x& H% b//如果列表框已经出现,这次点击则隐藏:3 `% y3 @0 B; ^
if(imgBox.style.pixelLeft!=-800){imgBox.style.pixelLeft=-800; return;}
+ M' e1 S, Y Q) C. W' Q# D5 j0 b& W$ d( u& i; ]+ f" {+ m9 Y0 g
//读取选择提示框在窗体中的绝对位置:1 J+ F5 K& r5 H/ n+ U. [7 M
//在父容器中的相对位置:
: c/ J; o! f# g6 ^# r# Z: Ivar mytop=obj.offsetTop;
" Y: B @6 j9 T0 Xvar myleft=obj.offsetLeft;; Y6 P( G9 D4 D7 ^: H# `0 v$ ^
//依次读取父容器在更高一级父容器中的相对位置: 7 }3 S% @3 i+ K
while(obj=obj.offsetParent){
+ r8 \3 M' O8 T4 |# m% g4 z+ Tmyleft =obj.offsetLeft; ( y. j3 b v3 g
mytop =obj.offsetTop; , Y% y$ _9 u, P" }# s @
}
}. f# |7 d+ v6 G! P' B//现在已经得到选择提示框的绝对位置myleft和mytop。
9 P0 Y/ y: N$ }$ X c0 y//下拉列表拉就出现在这个绝对位置的正下方:/ x2 [! H* F1 h" x: m
imgBox.style.left=myleft;) O) k+ @6 ^. y9 s- p
imgBox.style.top=mytop imgHeight 2;
+ `+ P0 R3 I4 a$ U4 ~. I+ b+ K: O+ ]! }" r, y5 W5 |
} % ~3 T9 I5 P- V8 C% d+ H
( I N3 l% {3 s6 X! r; h$ f: qvar isin=false;8 l2 F2 @. Z! V# F/ X; i2 e; H
function selectme(obj){ //这个函数将在窗体点击或选定图片时激活,窗体点击中传递的参数是null。
@. O0 l( _6 r+ gif(!isin||obj){imgBox.style.pixelLeft=-800;} //隐藏列表框* f0 b% d* d. ]/ n
if(obj){/ c* V2 R& _) G" Y; [8 ?
//改变选择提示框中的图片和"myhead"输入框中的值:$ E& b0 _1 C+ G$ ~
//listID是对象的自定义属性,即在对象的标签中定义了这个属性值,现在就可以读取或改变。& X2 ?4 N! p7 y& t3 b/ \( P
myform.myhead.value=imgSrc.replace("数字序号",obj.listID);! ?$ y E5 Y# @
document.images["imgselected"].src=imgSrc.replace("数字序号",obj.listID);
% t" n& {7 e& \0 c9 X- W. j}
& M- Q; S1 U/ K8 z6 C" O3 e}
1 o$ j% E4 h1 A% Q
+ [# h- N' V6 c5 c& M7 _/* 以下控制下拉列表框的自动滚动 */% D1 R8 I/ T5 W% r( T8 ?2 D1 [
var mytime=setTimeout("",0);
6 c1 G1 e3 W$ C5 x1 lvar pre_X=0; //这个变量用来记录滚动前滚动条的位置, A6 R0 o, l7 U( {# t+ }0 M
function scrollud(){ //本函数在下拉列表框滚动时被激活- M6 e& E5 d) U8 q
var current_X=imgBox.scrollTop; //记录函数激活时滚动条的位置+ B4 E7 M; t4 i! H% Y, Z& {3 X
//判断向上还是向下滚动,并自动滚动到"imgHeight"的整数倍。
* x; F8 ^3 b5 E, b" U9 ]//我们改变列表框的scrollTop值时,会自动激发其onscroll事件,循环激活本函数。6 G5 j/ P$ R( R" c }/ x: n1 D
if(current_Xpre_X imgBox.scrollTop Math.ceil(imgBox.scrollTop/imgHeight)*imgHeight){# N) I% @; ^1 G/ p1 o
clearTimeout(mytime);
2 O _; f7 X/ ~' Emytime=setTimeout("imgBox.scrollTop=Math.round(imgBox.scrollTop 1);",1);% t9 j$ d6 o+ l& h1 x
}% ?. x* L; y4 ?7 Y
else if(current_Xpre_X imgBox.scrollTop Math.floor(imgBox.scrollTop/imgHeight)*imgHeight){' v: n7 i: C: c2 f
clearTimeout(mytime);
* r/ A# ^" x+ c" s4 Amytime=setTimeout("imgBox.scrollTop=Math.round(imgBox.scrollTop-1);",1);
+ p' R* @" ]1 V9 i# Y: ]* T}3 d" X1 f, R( i; H! L
pre_X=current_X;
4 T7 s( P+ L D1 ^}* {% L. _) j$ X* U
//使列表框自动滚动到默认选定的图片位置,并给"myhead"输入框以默认值3 G! j9 y; I$ o7 t$ s# r3 G
function init(){ //本函数在每个列表框中的图片加载时激活- H; x) s2 f& P7 |& S5 H
imgBox.scrollTop=selectedNo*imgHeight;
* \+ z" x% W) M; v) t% {myform.myhead.value=imgSrc.replace("数字序号",selectedNo);
* \3 q* n5 A, @: ~: j0 X" W}
7 ]5 R) M' E1 B( B7 n% q; d/ C1 Q
* x% x3 J8 P4 _& R" m/ i/* 以下使窗体点击时selectme(obj)函数被激活: */
5 h% N. _6 \7 N8 qmyActivation="selectme(null)";
$ `4 J: z% v* k! b/ }0 j8 jif(document.body.onclick)
8 \0 t, Y( k( d8 T2 y{& z' Y# V- n9 H
eval(document.body.onclick.toString().replace('anonymous()','bodyclick()'));- [9 o" Y! ^4 w S1 S3 g
document.body.onclick=new Function("bodyclick();" myActivation);
4 D0 i1 x8 g3 v9 I}
5 P# i4 ?; \5 f4 P: _4 Celse document.body.onclick=new Function(myActivation);/ @' H [8 a' A* T i+ P
--
& L+ E$ J+ t+ O3 V; M2 i/script - b3 |$ o& D( L8 y
( v" K9 i! b; b$ L+ p- C% k0 {
程序最后“使窗体点击时selectme(obj)函数被激活”部分可以用document.body.onclick=new Function("selectme(null)")简单替换之。但为了不和其它程序的窗体点击事件冲突,我们用了这段代码。
点击图标进入精品网摘收藏 欢迎大家加入网络收藏夹