JavaScript - Adjusting Size of Images in Slideshow

My current project involves creating a slideshow in JavaScript where I only want to display half of the images. However, I have encountered an issue with some images having a width and height equal to 0 specifically in Chrome and Opera browsers. What could be causing this inconsistency?

HTML

<img id="picArt" src="0.jpg" onload="resizeImage();" /> <br />
<input type="button" value="Back" onclick="slidePic('P');" />
<input type="button" value="Forward" onclick="slidePic('N');" />

JavaScript

var picArt = document.getElementById("picArt");
var picLocal = new Array();

for(var num=0;num<=17;num++) {
   picLocal[num] = String(num) +".jpg";
} 

var desrib = new Array();

var preLoad = new Array();
var next = 0, picLength = (picLocal.length-1);
for(var num=0;num <= picLength; num++) {
    preLoad[num] = new Image();
    preLoad[num].src = picLocal[num];
}

function slidePic (how) {
    if(how == "N") next++;
    if(how == "P") next--;
    if(next> picLength) next =0;
    if(next <0) next = picLength;

    picArt.src = preLoad[next].src; 
}

function resizeImage() {
    var originImage = new Image();
    originImage.src = picArt.src;

    picArt.width = originImage.width/2;
    picArt.height = originImage.height/2;
}

Answer №1

Is it possible to resize images using CSS instead of JavaScript? For example:

img{
  width:200px;
  max-width:200px;
}
  • Images will automatically adjust their width and height without the need for JavaScript

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Making a REST API call with an ID parameter using Angular

I am currently working on an Angular application that interacts with a REST API. The data fetched from the API is determined based on the customer ID, for example: api/incident?customer_id=7. I am unsure of how to incorporate this into the API URL and serv ...

Mastering the art of reading arrays in Json with JavaScript

Recently, I stumbled upon some Json data that looks like this: var x = { "array1":"['x1','x2']", "array2":"['a1', 'a2']" } My mission is to display each element of the array individually: x1 x2 a1 a2 However, wh ...

Troubleshooting connection issues with a Chat app using Socket.io and Express: How to fix ERR

Currently, I'm immersed in a tutorial that delves into creating a rock-paper-scissors game with an integrated chat feature using socket.io and express. My focus is solely on crafting the chat component. However, a frustrating error keeps cropping up w ...

Set up Node.js application to allow CORS for designated endpoint(s) exclusively

I am currently facing a dilemma with my Node application and how to handle cross-origin resource sharing. In the past, I have used a PHP proxy to bypass this issue when calling endpoints from JavaScript/AJAX. However, I am now looking to streamline the pro ...

npm encountered an error or issue during the installation process

I have configured my proxy settings in the .npmrc file, but I am encountering errors when running the npm install command: $ npm install npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program File ...

Choose an Area within the Picture

I am attempting to specifically choose and display a 16x16 area from an image that I have selected using openfiledialog. The desired area is located at x,y coordinates 5,5 within the original image. My goal is to then draw this selected 16x16 portion into ...

Experiencing an overwhelming number of re-renders due to a specialized React Hook

I have been fetching data from a Neo4J database using the use-neo4j library and developed a custom hook for this purpose. The hook executes a query to retrieve the data from the database and then formats it in a way that enables me to visualize the data ef ...

Troubleshooting jQuery compatibility issues with Wordpress

Having some trouble implementing zclip on my Wordpress site to copy dynamically generated text. The code works fine as a standalone html page with embedded jquery, but it's not translating well to my Wordpress site. Even though I've placed the co ...

Node.JS file upload fails to maintain the current state of uploaded files

I am encountering an issue with my Node.js server-side script where it successfully uploads a file to a specified directory, but it is not the intended file. The script throws an error message: Request received: POST undefined:1 ------WebKitFormBoundar ...

Arrays cannot be used with $addFields in MongoDB

I have encountered a challenge where I am dealing with a field that can be either a string or an array. How can I handle this scenario in the $addField query? Below is my MongoDB query code snippet: db.ledger_scheme_logs.aggregate([ { $match ...

Is it possible to use only CSS to crop an image into a square and then shape it into

I am attempting to create a circular shape out of images that have varying sizes and shapes (some rectangular, some square, some portrait, and some landscape). When I apply either clip-path: circle(50% at 50% 50%); or border-radius: 50%;, the image transf ...

Sending a complex JavaScript object to the MVC controller with a consistent 400 response code

Once again facing another MVC issue. Someday, I'll look back at my "MVC learning days" and smile. So here's the problem - a constant 400 response. Despite reading numerous posts on SO, I just can't seem to get my logic working. Maybe if I s ...

Update Mapbox popups following filtering

I have successfully constructed a Mapbox map with a complex custom popup structure, complete with photos and formatting. The data is being fed from a .csv file using omnivore, and the popups are being created on ready. Additionally, I have implemented a se ...

Methods for concealing a single item in a Vue web form

I am a beginner with Vue and I am facing a challenge in hiding a specific element within a web form that is part of a loop. I am trying to use v-if along with a showHideEditComponent method call. However, when I invoke the showHideEditComponent method wi ...

Issue: (SystemJS) XHR error (404) encountered in Angular2 Plnkrsandbox

The issue: https://i.sstatic.net/jUKBU.png https://plnkr.co/edit/910M73kwYKc8xPlSIU57?p=preview index <!DOCTYPE html> <html> <head> <base href="/"> <title>Angular 2.1.2 + TypeScript Starter Kit</title> <met ...

How can I edit this code in JSPDF to print two pages instead of just one?

Currently, I have a script that can generate a PDF from one DIV, but now I need to create a two-page PDF from two separate DIVs. How can I modify the existing code to achieve this? The first DIV is identified as #pdf-one and the second DIV is #pdf-two. p ...

The function that can be used in Ajax for setting

I'm currently utilizing Ajax to automatically submit a form when a key is pressed. After the form is submitted, the page displays the resulting information success: function (response){ $("#search_results<?php echo $HoursID ?>").html(response) ...

Even after attempting to conceal objects using display:hidden, there is still whitespace present on the webpage

Check out my code in action on JsFiddle by visiting the following link: https://jsfiddle.net/2yvjm4h1/ Even after using display:none; to hide my objects rather than visibility:hidden before showing them with JavaScript, I still notice a significant amount ...

Using tabs within a textarea element in Typescript/Angular

I am struggling to find a solution for the issue I'm experiencing with tabs inside a textarea. Every time I press the tab button, it jumps to another textarea. I have found some solutions written in JavaScript and jQuery, but unfortunately, I can&apos ...

How can I make one object's position target another grouped object in three.js, while still enabling rotation to track a camera in augmented reality?

Currently, I am utilizing a cutting-edge augmented reality library that specializes in advanced image tracking capabilities. Despite extensively studying this project, I have reached a point where I require assistance. Essentially, the library generates an ...