js如何显示缩略图和点击放大查看???

JavaScript018

js如何显示缩略图和点击放大查看???,第1张

所谓的缩略图其实就是设置了一个长宽小点的<img>去存放这张图片,在<img>的点击事件中再去更改它的长宽达到放大效果,当然这样可能会打乱页面布局,所以你可以做成像QQ空间那样,点击图片后利用遮罩层显示放大的图片。

估计跟你的路径有关系。。你不妨先看下你图片的路径。。估计都会成为C:\fakepath\...jpg

如果是这样的话。。建议你用我这个。。然后把你要的照片传到站内。。在进行显示处理。。。

<html>

<head>

<script language="javascript" type="text/javascript">

var flag = false

function $(e) {return document.getElementById(e)}

function DrawImage(ImgD) {

var h = $("uploadimage").height

var w = $("uploadimage").width

if (w >0 &&h >0) {

flag = true

if (w / h >= 120 / 80) {

if (w >120) {

$("uploadimage").width = 120

$("uploadimage").height = (h * 120) / w

} else {

$("uploadimage").width = w

$("uploadimage").height = h

}

$("uploadimage").alt = w + "×" + h

}

else {

if (h >80) {

$("uploadimage").height = 80

$("uploadimage").width = (w * 80) / h

} else {

$("uploadimage").width = w

$("uploadimage").height = h

}

$("uploadimage").alt = w + "×" + h

}

}

}

function FileChange(Value) {

flag = false

document.all.uploadimage.alt = ""

var pos = Value.lastIndexOf("\\") + 1

Value = Value.substring(pos, Value.length)

document.all.uploadimage.src = "images/" + Value

}

function BeforeUpLoad() {

if (flag) alert("OK")else alert("FAIL")

}

</script>

</head>

<body>

<INPUT style="WIDTH: 143pxHEIGHT: 18px" type=file size=8 name=pic onchange="javascript:FileChange(this.value)">

<IMG id=uploadimage src="" onload="javascript:DrawImage(this)" ><BR>

<Button onclick="javascript:BeforeUpLoad()">提交</Button>

</body>

</html>

无需插件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>By:DragonDean</title>

<script type="text/javascript">

//下面用于图片上传预览功能

function setImagePreview(avalue) {

var docObj=document.getElementById("doc")

var imgObjPreview=document.getElementById("preview")

if(docObj.files &&docObj.files[0])

{

//火狐下,直接设img属性

imgObjPreview.style.display = 'block'

imgObjPreview.style.width = '150px'

imgObjPreview.style.height = '180px'

//imgObjPreview.src = docObj.files[0].getAsDataURL()

//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式

imgObjPreview.src = window.URL.createObjectURL(docObj.files[0])

}

else

{

//IE下,使用滤镜

docObj.select()

var imgSrc = document.selection.createRange().text

var localImagId = document.getElementById("localImag")

//必须设置初始大小

localImagId.style.width = "150px"

localImagId.style.height = "180px"

//图片异常的捕捉,防止用户修改后缀来伪造图片

try{

localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"

localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc

}

catch(e)

{

alert("您上传的图片格式不正确,请重新选择!")

return false

}

imgObjPreview.style.display = 'none'

document.selection.empty()

}

return true

}

</script>

</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tbody>

<tr>

<td height="101" align="center">

<div id="localImag"><img id="preview" src="http://blog.chuangling.net/Public/images/top.jpg" width="150" height="180" style="display: blockwidth: 150pxheight: 180px"></div>

</td>

</tr>

<tr>

<td align="center" style="padding-top:10px"><input type="file" name="file" id="doc" style="width:150px" onchange="javascript:setImagePreview()"></td>

</tr>

</tbody>

</table>

</body>

</html>