Home > Web Front-end > JS Tutorial > body text

How to modify the size of pictures in JS

醉折花枝作酒筹
Release: 2023-01-04 09:35:05
Original
11697 people have browsed it

JS method to modify the image size: 1. Get the DOM object corresponding to the id through "document.getElementById('image id value')"; 2. Use the style and width attributes of the object to change the image size, the syntax is " Picture object.style.width='Picture size value'".

How to modify the size of pictures in JS

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

How to modify the image size using JS

Note: The actual size of the image cannot be modified using js. It's just the width and height attributes corresponding to the image in the tag.

1. Obtain the DOM object corresponding to the id through var p = document.getElementById('image')2. Then use the style attribute of the object (The premise is that the image object has set the style attribute),

p.style.width='200px'

(Remember: this is a string, the format must be:???px, you cannot just write a number , otherwise the size of the image will not change on some browsers). The following code realizes that each time the button is clicked, the image can be enlarged or reduced:

The script defines two global variables to record the width and height of the image, because

The style.width

or style.height attribute value is a string, so use new String(x) 'px' to input the attribute value. This technique is Make a record of this for easy review later. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false;">&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt; &lt;title&gt;图像交换&lt;/title&gt; &lt;style type=&quot;text/css&quot;&gt; 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(&quot;images/bg2.jpg&quot;); } #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; } &lt;/style&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;changeimg.js&quot;&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p id=&quot;father&quot;&gt; &lt;p id=&quot;header&quot;&gt; &lt;h1 class=&quot;title&quot;&gt; 软件开发基础-实验&lt;/h1&gt;&lt;/p&gt;&lt;p id=&quot;daohang&quot;&gt; &lt;a href=&quot;../test1/test1-index.html&quot; class=&quot;one&quot;&gt;实验一&lt;/a&gt; &lt;a href=&quot;../test2/test2-html.html&quot; class=&quot;two&quot;&gt;实验二&lt;/a&gt; &lt;a href=&quot;../test3/test3-jsimg.html&quot; class=&quot;three&quot;&gt;实验三&lt;/a&gt; &lt;a href=&quot;&quot; class=&quot;four&quot;&gt;实验四&lt;/a&gt; &lt;a href=&quot;&quot; class=&quot;five&quot;&gt;实验五&lt;/a&gt; &lt;a href=&quot;&quot; class=&quot;six&quot;&gt;实验六&lt;/a&gt; &lt;a href=&quot;&quot; class=&quot;seven&quot;&gt;实验七&lt;/a&gt; &lt;a href=&quot;&quot; class=&quot;eight&quot;&gt;实验八&lt;/a&gt;&lt;/p&gt;&lt;p id=&quot;left&quot;&gt; &lt;ul&gt; &lt;li id=&quot;apply&quot;&gt;JS应用&lt;/li&gt; &lt;li id=&quot;formathtml&quot;&gt;&lt;a href=&quot;test3-jsimg.html&quot;&gt;图像交换&lt;/a&gt;&lt;/li&gt; &lt;li id=&quot;formatfont&quot;&gt;&lt;a href=&quot;test3-jsmin.html&quot;&gt;网页秒表&lt;/a&gt;&lt;/li&gt; &lt;li id=&quot;formattable&quot;&gt;&lt;a href=&quot;test3-jstable.html&quot;&gt;表格编辑&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/p&gt; &lt;p id=&quot;right&quot;&gt; &lt;br/&gt; &lt;h2&gt;图像交换&lt;/h2&gt; &lt;br/&gt; &lt;img src=&quot;images/forest1.jpg&quot; id=&amp;#39;image&amp;#39; style=&quot;width: 400px; height: 200px;&quot; onMouseOut=&quot;changeImg(&amp;#39;images/forest1.jpg&amp;#39;)&quot; onMouseOver=&quot;changeImg(&amp;#39;images/forest2.jpg&amp;#39;)&quot; onClick=&quot;changeImg(&amp;#39;images/forest3.jpg&amp;#39;)&quot;/&gt; &lt;!-- onMouseOut鼠标移出指定对象时的效果 --&gt; &lt;!-- onMouseOver鼠标移动到指定对象上的效果 --&gt; &lt;!-- onClick鼠标完成一次点击(按下&amp;松开)的效果 --&gt; &lt;!-- onMouseDown鼠标完成按下的瞬间产生的效果 --&gt; &lt;!-- onMouseUp鼠标完成松开的瞬间产生的效果 --&gt; &lt;br/&gt; &lt;input type=&quot;button&quot; value=&quot;增大&quot; onclick=&quot;bigger()&quot;/&gt; &lt;input type=&quot;button&quot; value=&quot;增小&quot; onclick=&quot;smaller()&quot;/&gt; &lt;/p&gt; &lt;p id=&quot;footer&quot;&gt; &lt;p&gt;2015-2016-1学期 软件工程 &lt;/p&gt; &lt;/p&gt; &lt;/p&gt; &lt;script type=&quot;text/javascript&quot;&gt; var x=400; var y=200; function changeImg() { var i = document.getElementById(&amp;#39;image&amp;#39;); i.src = src; } function bigger() { var p = document.getElementById(&amp;#39;image&amp;#39;); p.style.width=new String(x++)+&amp;#39;px&amp;#39;; p.style.height=new String(y++)+&amp;#39;px&amp;#39;; } function smaller() { var q = document.getElementById(&amp;#39;image&amp;#39;); q.style.width=new String(x--)+&amp;#39;px&amp;#39;; q.style.height=new String(y--)+&amp;#39;px&amp;#39;; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><div class="contentsignin">Copy after login</div></div>For more programming-related knowledge, please visit:

Programming Video

! !

The above is the detailed content of How to modify the size of pictures in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!