导航:首页 > 编程语言 > js点击打开图片

js点击打开图片

发布时间:2023-05-15 08:55:55

『壹』 如何用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>

阅读全文

与js点击打开图片相关的资料

热点内容
怎么他么怎么又网络了 浏览:649
java会出现内存泄露么 浏览:617
苹果4s锁屏后怎么还显示歌曲 浏览:207
鸿蒙系统文件管理哪些可以删除 浏览:550
ubuntuqt创建工程没有配置文件 浏览:126
网站登录变成其他网站怎么处理 浏览:202
ug数控编程学校有哪些 浏览:203
java图片上传显示 浏览:402
ppt的文件名后缀 浏览:902
ug编程软件下载到哪个盘 浏览:359
炫酷字体APP下载的文件在哪里 浏览:668
廊坊哪里有少儿编程机构 浏览:312
cad新文件能找回来吗 浏览:951
导出手机qq文件到u盘 浏览:456
电脑如何打开ppt文件怎么打开方式 浏览:782
魅族锁定区文件夹 浏览:357
刻字cnc怎么编程 浏览:182
学校的网络拓扑结构图 浏览:784
收集100个pdf文件里关键词 浏览:594
苹果关闭4g网络设置 浏览:289

友情链接