JavaScript实现缩略图

    技术2025-04-06  32

    js客户端缩略图函数说明:参数:imgD图片对象,width_s缩略图的宽,height_s缩略图的高使用方法:在html的img标签中加入onLoad事件调用DrawImage()方法。

     

    <html> <head> <title>JS实现缩略图</title> <mce:script language="JavaScript"><!-- function DrawImage(ImgD, width_s, height_s) { var image = new Image(); image.src = ImgD.src; if (image.width > 0 && image.height > 0) { flag = true; if (image.width / image.height >= width_s / height_s) { if (image.width > width_s) { ImgD.width = width_s; ImgD.height = (image.height * width_s) / image.width; } else { ImgD.width = image.width; ImgD.height = image.height; } } else { if (image.height > height_s) { ImgD.height = height_s; ImgD.width = (image.width * height_s) / image.height; } else { ImgD.width = image.width; ImgD.height = image.height; } } } else { ImgD.src = ""; ImgD.alt = "" } } // --></mce:script> </head> <body> <img id="imgId" src="./p-19.gif" mce_src="p-19.gif" οnlοad="DrawImage(this, 100, 100)"> </body> </html>

    最新回复(0)