javascript實(shí)現(xiàn)上傳圖片并預(yù)覽的效果實(shí)現(xiàn)代碼
更新時(shí)間:2011年04月11日 23:58:32 作者:
圖片上傳預(yù)覽,就是在使用文件選擇框選擇了文件之后就可以在頁(yè)面上看見圖片的效果,關(guān)于這個(gè)效果我一直認(rèn)為是無(wú)法做到的
今天用alphaimageloader濾鏡的src屬就是其中的主角它將使用絕對(duì)或相對(duì)url地址指定背景圖像。假如忽略此參數(shù),濾鏡將不會(huì)作用。
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.3ppt.com /">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css教程">
#picshow
{
filter:progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=scale);
width:88px;
height:125px;
}
</style>
<script type="text/網(wǎng)頁(yè)特效" language="javascript">
<!--
function upimg(imgfile)
{
var picshow = document.getelementbyid("picshow");
picshow.filters.item("dximagetransform.microsoft.alphaimageloader").src = imgfile.value;
picshow.style.width = "88px";
picshow.style.height = "125px";
}
-->
</script>
</head>
<body>
<div id="picshow"></div>
<p>選擇圖片:<input type="file" size="20" onchange="upimg(this);" /></p>
</body>
</html>
實(shí)例二、同時(shí)兼容ie6,ie7,ie8和 firefox。
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script>
var picpath;
var image;
// preview picture
function preview()
{
document.getelementbyid('preview').style.display = 'none';
// 下面代碼用來(lái)獲得圖片尺寸,這樣才能在ie下正常顯示圖片
document.getelementbyid('box').innerhtml
= "<img width='"+image.width+"' height='"+image.height+"' id='apic' src='"+picpath+"'>";
}
// show view button
function buttonshow()
{
/*
這里用來(lái)解決圖片加載延時(shí)造成的預(yù)覽失敗.
簡(jiǎn)單說(shuō)明一下,當(dāng)image對(duì)象的src屬性發(fā)生改變時(shí)javascript會(huì)重新給image裝載圖片內(nèi)容,
這通常是需要一些時(shí)間的,如果在加載完成之前想將圖片顯示出來(lái)就會(huì)造成錯(cuò)誤,所以我們
通過(guò)圖片的寬度和高度來(lái)判斷圖片是否已經(jīng)被成功加載,加載完畢才會(huì)顯示預(yù)覽按鈕.
這里我仍然有一個(gè)困惑,在ie7下預(yù)覽效果偶爾會(huì)失效.
*/
if ( image.width == 0 || image.height == 0 ) {
settimeout(buttonshow, 1000);
} else {
document.getelementbyid('preview').style.display = 'block';
}
}
function loadimage(ele) {
picpath = getpath(ele);
image = new image();
image.src = picpath;
settimeout(buttonshow, 1000);
}
function getpath(obj)
{
if(obj)
{
//ie
if (window.navigator.useragent.indexof("msie")>=1)
{
obj.select();
// ie下取得圖片的本地路徑
return document.selection.createrange().text;
}
//firefox
else if(window.navigator.useragent.indexof("firefox")>=1)
{
if(obj.files)
{
// firefox下取得的是圖片的數(shù)據(jù)
return obj.files.item(0).getasdataurl();
}
return obj.value;
}
return obj.value;
}
}
</script>
</head>
<body>
<input type="file" name="pic" id="pic" onchange='loadimage(this)' />
<input id='preview' type='button' value='preview' style='display:none;' onclick='preview();'>
<div id='box'></div>
</body>
</html>
復(fù)制代碼 代碼如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.3ppt.com /">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css教程">
#picshow
{
filter:progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=scale);
width:88px;
height:125px;
}
</style>
<script type="text/網(wǎng)頁(yè)特效" language="javascript">
<!--
function upimg(imgfile)
{
var picshow = document.getelementbyid("picshow");
picshow.filters.item("dximagetransform.microsoft.alphaimageloader").src = imgfile.value;
picshow.style.width = "88px";
picshow.style.height = "125px";
}
-->
</script>
</head>
<body>
<div id="picshow"></div>
<p>選擇圖片:<input type="file" size="20" onchange="upimg(this);" /></p>
</body>
</html>
實(shí)例二、同時(shí)兼容ie6,ie7,ie8和 firefox。
復(fù)制代碼 代碼如下:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script>
var picpath;
var image;
// preview picture
function preview()
{
document.getelementbyid('preview').style.display = 'none';
// 下面代碼用來(lái)獲得圖片尺寸,這樣才能在ie下正常顯示圖片
document.getelementbyid('box').innerhtml
= "<img width='"+image.width+"' height='"+image.height+"' id='apic' src='"+picpath+"'>";
}
// show view button
function buttonshow()
{
/*
這里用來(lái)解決圖片加載延時(shí)造成的預(yù)覽失敗.
簡(jiǎn)單說(shuō)明一下,當(dāng)image對(duì)象的src屬性發(fā)生改變時(shí)javascript會(huì)重新給image裝載圖片內(nèi)容,
這通常是需要一些時(shí)間的,如果在加載完成之前想將圖片顯示出來(lái)就會(huì)造成錯(cuò)誤,所以我們
通過(guò)圖片的寬度和高度來(lái)判斷圖片是否已經(jīng)被成功加載,加載完畢才會(huì)顯示預(yù)覽按鈕.
這里我仍然有一個(gè)困惑,在ie7下預(yù)覽效果偶爾會(huì)失效.
*/
if ( image.width == 0 || image.height == 0 ) {
settimeout(buttonshow, 1000);
} else {
document.getelementbyid('preview').style.display = 'block';
}
}
function loadimage(ele) {
picpath = getpath(ele);
image = new image();
image.src = picpath;
settimeout(buttonshow, 1000);
}
function getpath(obj)
{
if(obj)
{
//ie
if (window.navigator.useragent.indexof("msie")>=1)
{
obj.select();
// ie下取得圖片的本地路徑
return document.selection.createrange().text;
}
//firefox
else if(window.navigator.useragent.indexof("firefox")>=1)
{
if(obj.files)
{
// firefox下取得的是圖片的數(shù)據(jù)
return obj.files.item(0).getasdataurl();
}
return obj.value;
}
return obj.value;
}
}
</script>
</head>
<body>
<input type="file" name="pic" id="pic" onchange='loadimage(this)' />
<input id='preview' type='button' value='preview' style='display:none;' onclick='preview();'>
<div id='box'></div>
</body>
</html>
您可能感興趣的文章:
- JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
- js實(shí)現(xiàn)圖片上傳并預(yù)覽功能
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- js實(shí)現(xiàn)上傳圖片之上傳前預(yù)覽圖片
- Javascript圖片上傳前的本地預(yù)覽實(shí)例
- js圖片上傳前預(yù)覽功能(兼容所有瀏覽器)
- js實(shí)現(xiàn)圖片上傳預(yù)覽原理分析
- js 上傳圖片預(yù)覽問(wèn)題
- 圖片上傳之前檢查大小、尺寸、格式并預(yù)覽的js代碼
- javascript實(shí)現(xiàn)的圖片預(yù)覽和上傳功能示例【兼容IE 9】
相關(guān)文章
js簡(jiǎn)易namespace管理器 實(shí)例代碼
js簡(jiǎn)易namespace管理器 實(shí)例代碼,需要的朋友可以參考一下2013-06-06
Javascript圖像處理—平滑處理實(shí)現(xiàn)原理
這里直接引用OpenCV 2.4+ C++ 平滑處理和OpenCV 2.4+ C++ 邊緣梯度計(jì)算的相關(guān)內(nèi)容平滑也稱模糊, 是一項(xiàng)簡(jiǎn)單且使用頻率很高的圖像處理方法,需要了解的朋友可以參考下2012-12-12
js代碼驗(yàn)證手機(jī)號(hào)碼和電話號(hào)碼是否合法
這篇文章主要介紹了js代碼驗(yàn)證手機(jī)號(hào)碼和電話號(hào)碼是否合法,手機(jī)號(hào)碼和電話號(hào)碼在某些網(wǎng)站都是必填項(xiàng),為了提高用戶體驗(yàn)度,一般要進(jìn)行合法性校驗(yàn)的,需要的朋友可以參考下2015-07-07
使用JS實(shí)現(xiàn)圖片展示瀑布流效果(簡(jiǎn)單實(shí)例)
下面小編就為大家?guī)?lái)一篇使用JS實(shí)現(xiàn)圖片展示瀑布流效果(簡(jiǎn)單實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法
這篇文章主要介紹了使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法,需要的朋友可以參考下2018-12-12
PPK 談 JavaScript 的 this 關(guān)鍵字 [翻譯]
在 JavaScript 中 this 是最強(qiáng)的關(guān)鍵字之一。這篇貼文就是要告訴你如何用好 this。2009-09-09
JS實(shí)現(xiàn)簡(jiǎn)單的浮動(dòng)碰撞效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)單的浮動(dòng)碰撞效果,類似于廣告懸浮圖片在屏幕上來(lái)回碰撞的效果,涉及javascript結(jié)合時(shí)間動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,需要的朋友可以參考下2017-12-12
微信小程序網(wǎng)絡(luò)層封裝的實(shí)現(xiàn)(promise, 登錄鎖)
這篇文章主要介紹了微信小程序網(wǎng)絡(luò)層封裝(promise, 登錄鎖),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
簡(jiǎn)單的分頁(yè)代碼js實(shí)現(xiàn)
簡(jiǎn)單的分頁(yè)代碼js實(shí)現(xiàn),分享給大家,感興趣的小伙伴們可以參考一下2016-05-05

