how to dynamic control some picture's size by Javascript

B

blk.coffee

i have some pictures in a page. there are have difference sizes. some
of them are too widen for be arrange in a line. so
i been tried to adject their sizes to more suitable.

i add a event to that image likes:
<img id="img" onLoad="adjustSize(this)" src="123.jpg">

and write a function in javascript:
function adjustSize(obj){
if (obj.width>250){
sizeRate=250/obj.width;
obj.width=obj.width*sizeRate;
if (obj.hight>268)
obj.hight=268;
}

}

but exactly,i can get the image's width and height. cuz the loading
hasn't finished while the function of adjustSize works.

can somebody tell me how to deal this.
Tks advanced.
 
B

blk.coffee

In addition,those picture were loaded from a database.
so first i can't exactly get the sizse when a bigger picture is
loading.
thus in the event of picture loading the function isn't works cuz it
keeping a wrong sizes as zero.
but the event 'onMouseOver' works well.cuz it cant get the size exactly
after they were completely loaded.
 
J

Jeff North

| In addition,those picture were loaded from a database.
| so first i can't exactly get the sizse when a bigger picture is
| loading.
| thus in the event of picture loading the function isn't works cuz it
| keeping a wrong sizes as zero.
| but the event 'onMouseOver' works well.cuz it cant get the size exactly
| after they were completely loaded.

There are 2 options.
1. when storing the images to the database get the image dimension and
save them also.

2. since you are using server-side scripting you could use:
http://www.4guysfromrolla.com/webtech/code/imgsz.asp.html
 
B

blk.coffee

to mouseit101: TKS for your advice,but put that code in body tag got
the same result.

to Jeff: TKS a lot,you have been your wonderful help.
i would choose the second way,since i had uploaded so many pictures to
my webspace.
Thanks for your wonderful code, it help me.
 
J

Jeff North

| to mouseit101: TKS for your advice,but put that code in body tag got
| the same result.
|
| to Jeff: TKS a lot,you have been your wonderful help.
| i would choose the second way,since i had uploaded so many pictures to
| my webspace.
| Thanks for your wonderful code, it help me.

No probs but I must point out that I'm not the author of the code :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top