您好,欢迎访问一九零五行业门户网

JS如何修改图片的大小

js修改图片大小的方法:1、通过“document.getelementbyid('图片id值')”获取对应id的dom对象;2、使用对象的style和width属性改变图片大小,语法“图片对象.style.width='图片大小值'”。
本教程操作环境:windows7系统、javascript1.8.5版、dell g3电脑。
js修改图片大小的方法
注:利用js是不能修改图片的实际大小的,修改的只是图片在标签中对应的width,height属性。
1、通过var p = document.getelementbyid('image')获取到对应id的dom对象
2、再使用对象的style属性(前提是image对象已经设置过style属性),p.style.width='200px'(切记:此处是字符串,格式一定是:?px,不能只写个数字,否则在有的浏览器上图片的大小是不会改变的)。
以下代码实现了每次点击按钮可以实现图片变大或缩小一点:
脚本中定义了两个全局变量用来记录图片的宽和高,因为style.width或style.height属性值是字符串,所以用new string(x++)+'px'的方式来实现属性值的输入,这个技巧在此做一个记录,便于以后查看。
<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>图像交换</title><style type="text/css">html,body,p,span,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,address,big,cite,code,del, em,img,ins,small,strong,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,iframe { margin:0px; padding:0px; /* 用来取消一些标签默认的margin和padding */ } body{ text-align: center; } #father{ position:relative; margin:auto; width: 800px; height:600px; border-style: solid;}#header{ height:100px; width: 800px; background-image: url("images/bg2.jpg"); } #daohang{ height:30px; width:800px; background-color: #99ffff; } #left{ width:180px; height:440px; background-color: #f0ffff; } #right{ position: absolute; top:130px; left:180px; height:440px; width:620px; text-align: left; } #footer{ position:absolute; top:570px; height:30px; width: 800px; background-color: #99ffff; } #header h1{ height: 3em; line-height: 3em; overflow: hidden; letter-spacing: 5px; } a{ height: 2em; line-height: 2em; overflow: hidden; text-decoration: none; } p{ height: 2em; line-height: 2em; overflow: hidden; } ul{ list-style-type:none; } li{ padding: 10px; } #apply{ font-size: 30px; font-weight: bold; } .ftcss{ font-family: 宋体,sans-serif; font-size:12pt; color:#0011aa; text-align: left; text-indent: 2em; line-height: 1.8; } .bold{ font-weight: 600; } .italic{ font-style: italic; } .underline{ text-decoration: underline; } </style> <script type="text/javascript" src="changeimg.js"> </script> </head> <body> <p id="father"> <p id="header"> <h1 class="title">&nbsp;&nbsp;&nbsp;&nbsp;软件开发基础-实验</h1></p><p id="daohang"> <a href="../test1/test1-index.html" class="one">实验一</a> <a href="../test2/test2-html.html" class="two">实验二</a> <a href="../test3/test3-jsimg.html" class="three">实验三</a> <a href="" class="four">实验四</a> <a href="" class="five">实验五</a> <a href="" class="six">实验六</a> <a href="" class="seven">实验七</a> <a href="" class="eight">实验八</a></p><p id="left"> <ul> <li id="apply">js应用</li> <li id="formathtml"><a href="test3-jsimg.html">图像交换</a></li> <li id="formatfont"><a href="test3-jsmin.html">网页秒表</a></li> <li id="formattable"><a href="test3-jstable.html">表格编辑</a></li> </ul> </p> <p id="right"> <br/> <h2>图像交换</h2> <br/> <img src="images/forest1.jpg" id='image' style="width: 400px; height: 200px;" onmouseout="changeimg('images/forest1.jpg')" onmouseover="changeimg('images/forest2.jpg')" onclick="changeimg('images/forest3.jpg')"/> <!-- onmouseout鼠标移出指定对象时的效果 --> <!-- onmouseover鼠标移动到指定对象上的效果 --> <!-- onclick鼠标完成一次点击(按下&松开)的效果 --> <!-- onmousedown鼠标完成按下的瞬间产生的效果 --> <!-- onmouseup鼠标完成松开的瞬间产生的效果 --> <br/> <input type="button" value="增大" onclick="bigger()"/> <input type="button" value="增小" onclick="smaller()"/> </p> <p id="footer"> <p>2015-2016-1学期&nbsp;软件工程 </p> </p> </p> <script type="text/javascript"> var x=400; var y=200; function changeimg() { var i = document.getelementbyid('image'); i.src = src; } function bigger() { var p = document.getelementbyid('image'); p.style.width=new string(x++)+'px'; p.style.height=new string(y++)+'px'; } function smaller() { var q = document.getelementbyid('image'); q.style.width=new string(x--)+'px'; q.style.height=new string(y--)+'px'; } </script> </body></html>
更多编程相关知识,请访问:编程视频!!
以上就是js如何修改图片的大小的详细内容。
其它类似信息

推荐信息