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'".
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),
(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. <pre class='brush:php;toolbar:false;'><!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 id="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 id="图像交换">图像交换</h2>
<br/>
<img src="/static/imghwm/default1.png" data-src="images/forest1.jpg" class="lazy" id=&#39;image&#39; style="max-width:90%"
onMouseOut="changeImg(&#39;images/forest1.jpg&#39;)"
onMouseOver="changeImg(&#39;images/forest2.jpg&#39;)"
onClick="changeImg(&#39;images/forest3.jpg&#39;)"/>
<!-- 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学期 软件工程
</p>
</p>
</p>
<script type="text/javascript">
var x=400;
var y=200;
function changeImg() {
var i = document.getElementById(&#39;image&#39;);
i.src = src;
}
function bigger() {
var p = document.getElementById(&#39;image&#39;);
p.style.width=new String(x++)+&#39;px&#39;;
p.style.height=new String(y++)+&#39;px&#39;;
}
function smaller() {
var q = document.getElementById(&#39;image&#39;);
q.style.width=new String(x--)+&#39;px&#39;;
q.style.height=new String(y--)+&#39;px&#39;;
}
</script>
</body>
</html></pre>
For more programming-related knowledge, please visit:
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!

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
