『壹』 如何用js語句顯示圖片
需要准備的材料分別有:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器專,新建html文件,例如:index.html。
『貳』 javascript點擊查看圖片,彈框顯示圖片,怎麼用js怎麼實現
最好用插件,去layer官網有專門的點擊圖片,彈出瀏覽還支持多個圖片。
先去官網下載版layer包,你的權網頁引用layer的js文件。
地址:網頁鏈接點擊相冊層
然後js代碼:
//調用示例
layer.ready(function(){ //為了layer.ext.js載入完畢再執行
var ps=$("#psize").val();
layer.photos({
photos: '#layer-photos-demo'
,shift: ps //0-6的選擇,指定彈出圖片動畫類型,默認隨機
});
});
html代碼:
<div id="layer-photos-demo" class="layer-photos-demo" >
<img layer-pid="圖片id,可以不寫" layer-src="縮略圖片地址" src="圖片地址" alt="" style="height: 140px;width: 120px;border:1px solid #bbb;">
</div>
『叄』 基於javascript代碼實現通過點擊圖片顯示原圖片
廢話不多說了,直接給大家貼js實現點擊圖片顯示原圖片的代碼,具體代碼如下所示:
function
DrawImage(ImgD){
var
image
=
new
Image();
image.src=ImgD.src;
var
width
=
$(ImgD).attr("width");
var
height
=
$(ImgD).attr("height");
if(width
>100
&&
height>80){
ImgD.width=100;
ImgD.height=80;
ImgD.alt=image.width+"×"+image.height;
}else{
if(image.width>0
&&
image.height>0){
flag=true;
if(image.width>300
||
image.height>200){
ImgD.width=image.width/2;
ImgD.height=image.height/2;
ImgD.alt=image.width+"×"+image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
ImgD.alt=image.width+"×"+image.height;
}
}
}
}
下面分享一段關於js實現上傳圖片及時預覽
<!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/html;
charset=utf-8"
/>
<title>圖片上傳本地預覽</title>
<style
type="text/css">
#preview{width:260px;height:190px;border:1px
solid
#000;overflow:hidden;}
#imghead
{filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}
</style>
<script
type="text/javascript">
//圖片上傳預覽
IE是用了濾鏡。
function
previewImage(file)
{
var
MAXWIDTH
=
260;
var
MAXHEIGHT
=
180;
var
div
=
document.getElementById('preview');
if
(file.files
&&
file.files[0])
{
div.innerHTML
='<img
id=imghead>';
var
img
=
document.getElementById('imghead');
img.onload
=
function(){
var
rect
=
clacImgZoomParam(MAXWIDTH,
MAXHEIGHT,
img.offsetWidth,
img.offsetHeight);
img.width
=
rect.width;
img.height
=
rect.height;
//
img.style.marginLeft
=
rect.left+'px';
img.style.marginTop
=
rect.top+'px';
}
var
reader
=
new
FileReader();
reader.onload
=
function(evt){img.src
=
evt.target.result;}
reader.readAsDataURL(file.files[0]);
}
else
//兼容IE
{
var
sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
file.select();
var
src
=
document.selection.createRange().text;
div.innerHTML
=
'<img
id=imghead>';
var
img
=
document.getElementById('imghead');
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src
=
src;
var
rect
=
clacImgZoomParam(MAXWIDTH,
MAXHEIGHT,
img.offsetWidth,
img.offsetHeight);
status
=('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
div.innerHTML
=
"<div
id=divhead
style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>";
}
}
function
clacImgZoomParam(
maxWidth,
maxHeight,
width,
height
){
var
param
=
{top:0,
left:0,
width:width,
height:height};
if(
width>maxWidth
||
height>maxHeight
)
{
rateWidth
=
width
/
maxWidth;
rateHeight
=
height
/
maxHeight;
if(
rateWidth
>
rateHeight
)
{
param.width
=
maxWidth;
param.height
=
Math.round(height
/
rateWidth);
}else
{
param.width
=
Math.round(width
/
rateHeight);
param.height
=
maxHeight;
}
}
param.left
=
Math.round((maxWidth
-
param.width)
/
2);
param.top
=
Math.round((maxHeight
-
param.height)
/
2);
return
param;
}
</script>
</head>
<body>
<div
id="preview">
<img
id="imghead"
width=100
height=100
border=0
src='<%=request.getContextPath()%>/images/defaul.jpg'>
</div>
<input
type="file"
onchange="previewImage(this)"
/>
</body>
</html>
『肆』 js如何實現點擊圖片彈出窗口並放大這張圖片,彈出的窗口有半透明遮罩層效果,彈出的窗口不跳頁面
通過JS代碼 document.getElementById('divID').style.etElementById('divID').style.display = 'none'。函數可以實現。
『伍』 如何用JS實現,單擊圖片,在新的頁顯示圖片
<img src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/avarta/66/r11s1g5.gif" onclick="window.open(this.src,'_blank')"/>
onclick 點擊圖片事件
window.open('','') 打開一個新網頁 第一個參數是網址 第二個是打開方式
window.open(this.src,'_blank') 以該圖片的路徑回為網址,以新頁答的方式打開
『陸』 javascript打開新窗口顯示圖片,怎麼實現
<img src="1.jpg" onclick="javascript:window.open(this.src)">
如斗運棚空則果需要手形狀滑鼠,用下面代碼
<悄棚img src="1.jpg" onclick="javascript:window.open(this.src)" style="cursor:hand">
『柒』 js文件里實現點擊一個圖標,彈出選擇圖片的對話框
1、可以使用trigger方式,javascript中沒有原生的trigger函數,可以自己寫一個,也可以直接使用jquery的實現:
[javascript]
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
button1.onclick = function(){alert("button1");trigger(button2,"onclick");}
button2.onclick = function(){alert("button2");}
function trigger(ele,event)
{ele[event]();}
2、也可以使用更加簡單的方式,就是將input覆蓋住要點擊的圖片或者超鏈或者其它div,然後將input設置成完全透明,這樣點擊圖片時其實點擊的是input,如下:
[html]
<div class="ps-image" style="width:300px;height:300px;border:0px sold red; background:url('/images/ps.png')">
<input type="file" id="file" style="filter:alpha(opacity=0);opacity:0;width:100%;height:100%;"/>
</div>
『捌』 javascript:點擊圖片打開新窗口瀏覽圖片
方法一:
<style>
img{
width:50%;
height:50%
}
</style>
<img src=1.PNG onclick="lookimg(this.src)">
<script>
function lookimg(str)
{
var newwin=window.open()
newwin.document.write("<img src="+str+" />")
}
</script>
方法二版權:
<img src=1.png onclick="lookimg(this.src)">
<script>
function lookimg(str)
{
var newwin=window.open()
myimg=newwin.document.createElement("img")
myimg.src=str
newwin.document.body.appendChild(myimg)
}
</script>
『玖』 js通過點擊一次按鈕動態獲取圖片顯示出來
在按鈕來的點擊事件里動態給自img賦值。如:
functioncheck(){//這是按鈕的點擊事件
varimg=document.getElementById('ele');//獲取img對象
img.src="images/h.png";//把要顯示的圖片地址賦給img對象src就行了
}
『拾』 用JS在新窗口打開本圖片
<img src="圖片連接" width="400" height="400" onclick="op(this.src)" />
<script language="javascript">
function op(c_url)
{
window.open(c_url)
}
</script>