admin管理员组文章数量:1026373
I try to enlarge images in my html page by using JavaScript. The image in the html- source code always has the height/width 100 Pixel. By clicking on the image, the image should be displayed in original size. By clicking the image again, the image should displayed in height/width 100 Pixel.
Example image in the html source code:
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
My JavaScript source code:
function swap(img)
{
if (img.width==="100")
{
img.width="auto";
img.height="100%";
}
else
{
img.width="100";
img.height="100";
}
}
My Problem is the image size dose not change. What am I doing wrong? Does anyone have maybe a better idea?
I try to enlarge images in my html page by using JavaScript. The image in the html- source code always has the height/width 100 Pixel. By clicking on the image, the image should be displayed in original size. By clicking the image again, the image should displayed in height/width 100 Pixel.
Example image in the html source code:
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
My JavaScript source code:
function swap(img)
{
if (img.width==="100")
{
img.width="auto";
img.height="100%";
}
else
{
img.width="100";
img.height="100";
}
}
My Problem is the image size dose not change. What am I doing wrong? Does anyone have maybe a better idea?
Share Improve this question edited May 2, 2016 at 9:25 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked May 2, 2016 at 8:01 OlliOlli 897 bronze badges 5-
1
===(strict parison)
will not make ittrue
asimg.width
will be of typeNumber
– Rayon Commented May 2, 2016 at 8:05 -
2
Play with the
style-properties
thanElement-properties
– Rayon Commented May 2, 2016 at 8:07 - Haven't done it in a while but i think you should use img.clientWidth, this returns the width as it is rendered in the browser. – namlik Commented May 2, 2016 at 8:07
- 1 Why not using JS only for toggling class and leave the rest of the job to CSS? – markoffden Commented May 2, 2016 at 8:12
- Are you looking for a pure-javascript solution or you are using any javascript framework/library? – Hitmands Commented May 2, 2016 at 8:20
7 Answers
Reset to default 2Provided your images don't already have a CSS class, you can swap a full-size class easily.
function swap(img) {
if (img.className !== "full-size") img.className = "full-size";
else img.className = "";
}
.full-size {
height: 100%;
width: 100%;
}
<img src="https://multimedia.journalism.berkeley.edu/wp-content/uploads/2014/09/css101-main.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
You'd be better off using jQuery
to handle multiple classes on a single HTML element though.
Using image.width is not the best practice, and it has been so for a very long time.
What you should be doing is using the "style" property, both in your HTML and in the JavaScript. Aside from that, you always need to define units, e.g. pixels (px), percent (%).
So your image should look something like this (I ignored the events):
<img style="cursor: pointer; width: 100%, height: 100%;">
I didn't understand if you wanted 100px or 100%, so change as needed.
Your JS should look something like this:
function swap(img)
{
if (img.style.width==="100%")
{
img.style.width="auto";
img.style.height="100%";
}
else
{
img.style.width="100%";
img.style.height="100%";
}
}
Fiddle Link
<img src="https://c7.staticflickr./3/2538/3742771246_2648fa4e6e_b.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
<script>
function swap(img)
{
debugger;
if (img.width===100)
{
img.style.height = 'auto';
img.style.width = 'auto';
}
else
{
img.style.height = '100px';
img.style.width = '100px';
}
}</script>
Use style
property of the element to apply style
. Use .width
property just for conditions.
function swap(img) {
if (img.width === 100) {
//Remove the quotes around `100` as you are using strict parison
img.width = "auto";
img.style.width = "auto";
img.style.height = "100%";
} else {
img.width = "100";
img.style.width = "";
img.style.height = "";
}
}
<img src="http://www.mariodemiranda./uploads/showcase/4_pic_indian-art-4.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Edit: Using css class
, .contains
,.add
/.remove
will be handy.
function swap(img) {
img.classList.contains('fs') ? img.classList.remove('fs') : img.classList.add('fs')
}
.fs {
width: auto;
height: 100%;
}
<img src="http://www.mariodemiranda./uploads/showcase/4_pic_indian-art-4.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Your actual code:
In your code the problem was, as stated in ments, the strict paraison where you were paring a string
with a number
, because img.width
is a number
and "100"
is a string
.
So to fix it you just need to change the paraison, either byu using ==
paraison or just 100
instead of "100"
:
function swap(img) {
if (img.width === 100) {
img.width = "auto";
img.height = "100%";
} else {
img.width = "100";
img.height = "100";
}
}
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Alternatives:
- In fact in Javascript to access an element attribute (width or
height) you better use
element.getAttribute()
method, this is how should be your code:
function swap(img) {
if (img.getAttribute("width") === "100") {
img.width = "auto";
img.height = "100%";
} else {
img.width = "100";
img.height = "100";
}
}
<img src="image.jpg" onclick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
- Otherwise, just use
height
andwidth
as properties in the style of your element, then access it usingelement.style.height
.
Further reading:
For further reading you can see the diffirence between height
/width
element attributes and styles properties, here:
Should I specify height and width attributes for my IMGs in HTML?
I would prefer using css to do this and changing the element class accordingly when click occurs.
The script:
function swap(img) {
if (img.classList.contains('fullsize')) {
img.classList.remove('fullsize');
} else {
img.classList.add('fullsize');
}
}
The element:
<img src="image.jpg" onclick="swap(this)" class="resize-image" style="cursor: pointer;">
CSS:
.resize-image {
width: 100px;
height: 100px;
}
.resize-image.fullsize {
width: auto;
height: 100%;
}
// Or if using SASS etc
.resize-image {
width: 100px;
height: 100px;
&.fullsize {
width: auto;
height: 100%;
}
}
function swap(img)
{
debugger
if (img.style.width==="100px")
{
img.style.width="auto";
img.style.height="100%";
}
else
{
img.style.width="100px";
img.style.height="100px";
}
}
I try to enlarge images in my html page by using JavaScript. The image in the html- source code always has the height/width 100 Pixel. By clicking on the image, the image should be displayed in original size. By clicking the image again, the image should displayed in height/width 100 Pixel.
Example image in the html source code:
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
My JavaScript source code:
function swap(img)
{
if (img.width==="100")
{
img.width="auto";
img.height="100%";
}
else
{
img.width="100";
img.height="100";
}
}
My Problem is the image size dose not change. What am I doing wrong? Does anyone have maybe a better idea?
I try to enlarge images in my html page by using JavaScript. The image in the html- source code always has the height/width 100 Pixel. By clicking on the image, the image should be displayed in original size. By clicking the image again, the image should displayed in height/width 100 Pixel.
Example image in the html source code:
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
My JavaScript source code:
function swap(img)
{
if (img.width==="100")
{
img.width="auto";
img.height="100%";
}
else
{
img.width="100";
img.height="100";
}
}
My Problem is the image size dose not change. What am I doing wrong? Does anyone have maybe a better idea?
Share Improve this question edited May 2, 2016 at 9:25 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked May 2, 2016 at 8:01 OlliOlli 897 bronze badges 5-
1
===(strict parison)
will not make ittrue
asimg.width
will be of typeNumber
– Rayon Commented May 2, 2016 at 8:05 -
2
Play with the
style-properties
thanElement-properties
– Rayon Commented May 2, 2016 at 8:07 - Haven't done it in a while but i think you should use img.clientWidth, this returns the width as it is rendered in the browser. – namlik Commented May 2, 2016 at 8:07
- 1 Why not using JS only for toggling class and leave the rest of the job to CSS? – markoffden Commented May 2, 2016 at 8:12
- Are you looking for a pure-javascript solution or you are using any javascript framework/library? – Hitmands Commented May 2, 2016 at 8:20
7 Answers
Reset to default 2Provided your images don't already have a CSS class, you can swap a full-size class easily.
function swap(img) {
if (img.className !== "full-size") img.className = "full-size";
else img.className = "";
}
.full-size {
height: 100%;
width: 100%;
}
<img src="https://multimedia.journalism.berkeley.edu/wp-content/uploads/2014/09/css101-main.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
You'd be better off using jQuery
to handle multiple classes on a single HTML element though.
Using image.width is not the best practice, and it has been so for a very long time.
What you should be doing is using the "style" property, both in your HTML and in the JavaScript. Aside from that, you always need to define units, e.g. pixels (px), percent (%).
So your image should look something like this (I ignored the events):
<img style="cursor: pointer; width: 100%, height: 100%;">
I didn't understand if you wanted 100px or 100%, so change as needed.
Your JS should look something like this:
function swap(img)
{
if (img.style.width==="100%")
{
img.style.width="auto";
img.style.height="100%";
}
else
{
img.style.width="100%";
img.style.height="100%";
}
}
Fiddle Link
<img src="https://c7.staticflickr./3/2538/3742771246_2648fa4e6e_b.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
<script>
function swap(img)
{
debugger;
if (img.width===100)
{
img.style.height = 'auto';
img.style.width = 'auto';
}
else
{
img.style.height = '100px';
img.style.width = '100px';
}
}</script>
Use style
property of the element to apply style
. Use .width
property just for conditions.
function swap(img) {
if (img.width === 100) {
//Remove the quotes around `100` as you are using strict parison
img.width = "auto";
img.style.width = "auto";
img.style.height = "100%";
} else {
img.width = "100";
img.style.width = "";
img.style.height = "";
}
}
<img src="http://www.mariodemiranda./uploads/showcase/4_pic_indian-art-4.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Edit: Using css class
, .contains
,.add
/.remove
will be handy.
function swap(img) {
img.classList.contains('fs') ? img.classList.remove('fs') : img.classList.add('fs')
}
.fs {
width: auto;
height: 100%;
}
<img src="http://www.mariodemiranda./uploads/showcase/4_pic_indian-art-4.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Your actual code:
In your code the problem was, as stated in ments, the strict paraison where you were paring a string
with a number
, because img.width
is a number
and "100"
is a string
.
So to fix it you just need to change the paraison, either byu using ==
paraison or just 100
instead of "100"
:
function swap(img) {
if (img.width === 100) {
img.width = "auto";
img.height = "100%";
} else {
img.width = "100";
img.height = "100";
}
}
<img src="image.jpg" onClick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
Alternatives:
- In fact in Javascript to access an element attribute (width or
height) you better use
element.getAttribute()
method, this is how should be your code:
function swap(img) {
if (img.getAttribute("width") === "100") {
img.width = "auto";
img.height = "100%";
} else {
img.width = "100";
img.height = "100";
}
}
<img src="image.jpg" onclick="swap(this)" onmouseover="" style="cursor: pointer;" height="100" width="100">
- Otherwise, just use
height
andwidth
as properties in the style of your element, then access it usingelement.style.height
.
Further reading:
For further reading you can see the diffirence between height
/width
element attributes and styles properties, here:
Should I specify height and width attributes for my IMGs in HTML?
I would prefer using css to do this and changing the element class accordingly when click occurs.
The script:
function swap(img) {
if (img.classList.contains('fullsize')) {
img.classList.remove('fullsize');
} else {
img.classList.add('fullsize');
}
}
The element:
<img src="image.jpg" onclick="swap(this)" class="resize-image" style="cursor: pointer;">
CSS:
.resize-image {
width: 100px;
height: 100px;
}
.resize-image.fullsize {
width: auto;
height: 100%;
}
// Or if using SASS etc
.resize-image {
width: 100px;
height: 100px;
&.fullsize {
width: auto;
height: 100%;
}
}
function swap(img)
{
debugger
if (img.style.width==="100px")
{
img.style.width="auto";
img.style.height="100%";
}
else
{
img.style.width="100px";
img.style.height="100px";
}
}
本文标签: Enlarge images in my html page using JavaScriptStack Overflow
版权声明:本文标题:Enlarge images in my html page using JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745642038a2160805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论