圖片上傳即時顯示縮略圖的js代碼
更新時間:2009年05月27日 14:39:48 作者:
主要用于上傳圖片的過程中可以顯示圖片,這樣就可以確認(rèn)圖片有沒有遠錯,利用提高正確率。
<script language="javascript" type="text/javascript">
var allowExt = ['jpg', 'gif', 'bmp', 'png', 'jpeg'];
var preivew = function(file, container){
try{
var pic = new Picture(file, container);
}catch(e){
alert(e);
}
}
//縮略圖類定義
var Picture = function(file, container){
var height = 0,
widht = 0,
ext = '',
size = 0,
name = '',
path = '';
var self = this;
if(file){
name = file.value;
if (window.navigator.userAgent.indexOf("MSIE")>=1){
file.select();
path = document.selection.createRange().text;
}else if(window.navigator.userAgent.indexOf("Firefox")>=1){
if(file.files){
path = file.files.item(0).getAsDataURL();
}else{
path = file.value;
}
}
}else{
throw "bad file";
}
ext = name.substr(name.lastIndexOf("."), name.length);
if(container.tagName.toLowerCase() != 'img'){
throw "container is not a valid img label";
container.visibility = 'hidden';
}
container.src = path;
container.alt = name;
container.style.visibility = 'visible';
height = container.height;
widht = container.widht;
size = container.fileSize;
this.get = function(name){
return self[name];
}
this.isValid = function(){
if(allowExt.indexOf(self.ext) !== -1){
throw 'the ext is not allowed to upload';
return false;
}
}
}
</script>
<div class='previewDemo'>
<input id="file" type="file" onchange="preivew(this, document.getElementById('img'));">
<img id="img" style="visibility:hidden" height="100px" width="100px">
</div>
相關(guān)文章
JS實現(xiàn)鼠標(biāo)移動到縮略圖顯示大圖的圖片放大效果
一個網(wǎng)頁上用的圖片提示效果,當(dāng)把鼠標(biāo)移動到圖片縮略圖的時候,會顯示圖片大圖,似乎在網(wǎng)上這是個很常見的效果,實現(xiàn)方法也比較多,有人用CSS,有人用JavaScript,有人用jQuery,總之,選擇自己習(xí)慣的方式去實現(xiàn),就是最棒的。2012-12-12靜態(tài)圖片的十一種濾鏡效果--不支持Ie7及非IE瀏覽器。
靜態(tài)圖片的十一種濾鏡效果--不支持Ie7及非IE瀏覽器。...2007-03-03