Having trouble with Resizing Images in jQuery despite successful image rotation

I am facing an issue with rotating and resizing an image using an HTML5 form. The rotation angle can be adjusted in steps of 90 degrees, but the image should always remain 770px wide regardless of its orientation (landscape or portrait).

Despite creating an event handler for this purpose, I have noticed that the function does not work as expected. When rotating a portrait image by 90 degrees, the width and height values are both set to 770px instead of switching according to the rotation.

To help you understand better, consider this use case: Initially, when the image is in portrait mode, it has dimensions of 770x1027 (w x h). After rotating it by 90 degrees, ideally the function should rotate the image and set the new dimensions to width = 1027px and height = 770px. However, currently, both width and height are being set to 770px only.

$("#degrees").change(function(){
    var element$ = $("#photo-submission"),
        w = element$.width(), 
        h = element$.height(),
        deg = $(this).val();

    element$.removeAttr("style").attr({
        "style": "-webkit-transform:rotate("+ deg +"deg); width: "+ h +"px; height: "+ w + "px;"
    })
});

Answer №1

It appears that you have interchanged the width and height values.

element$.removeAttr("style").attr({
    "style": "-webkit-transform:rotate("+ deg +"deg); width: "+ w +"px; height: "+ h + "px;"
})

Answer №2

Have you considered utilizing the jQuery method specifically designed for modifying CSS properties? This can make it simpler to manage your variables and ensure they are placed correctly:

$("#degrees").change(function(){
    var element  = $("#photo-submission"),
        width   = element.width(), 
        height   = element.height(),
        degrees = this.value;

    element.css({
        '-webkit-transform' : 'rotate(' + degrees + 'deg)',
        width  : width,
        height : height
    });
});

FIDDLE

One key benefit of this approach is that there's no need to specify the width and height if it remains the same, simplifying the code.

$("#degrees").change(function(){
    $("#photo-submission").css({
        '-webkit-transform' : 'rotate(' + this.value + 'deg)'
    });
});

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

After inviting the bot, I would like to directly message the server owner

Currently, I am searching for a solution to enable my bot to send a message to the owner of the server it has been added to. While exploring various stackoverflow responses, I came across code snippets that closely resembled the following: client.on(' ...

Finding the accurate constraints for the view of a UIViewController

I am working on an iPad application where the UIViewController's view has different dimensions depending on the orientation. In landscape mode, the width is 1024px and the height is 704px (after accounting for status and navigation bars). When trying ...

Setting up Node JS with the essentials

Recently, I successfully installed Node JS on my EC2 instance (server) by following the detailed guide provided on this helpful website: After completing the installation process as per the instructions, I attempted to implement Node in my project. Howeve ...

"Implementing conditional evaluation for pixel units in AngularJS using ng-style

Looking to adjust the style of an element based on its height. For example, if the height is less than X, apply 'style 1'; otherwise, apply 'style 2'. The syntax appears to be correct, but accurately evaluating the height property in p ...

What is the best way to verify the success of two ajax requests?

There are two ajax requests being made to check the availability of two images. Below is the code for these ajax calls: if (result['img_one'] !== '') { var src_one = "blah-blah-one.jpg"; $.ajax({url: src_one, success: ...

Implementing Rule for Password Matching in Bootstrap 4 Form Validation

I am utilizing Bootstrap 4 / JS form validation (https://getbootstrap.com/docs/4.0/components/forms/#custom-styles) and it is functioning as expected. Currently, I am working on implementing the same style and submission rules for comparing two password f ...

Inject the content into the DIV tag retrieved from the source

I am working on a website layout that consists of a left panel and a content div on the right. Currently, when a user clicks on a link in the left panel, a JavaScript Ajax call is made to load the corresponding content into the div tag named 'content& ...

Refresh information in form after submitting with Remix

Currently, I am utilizing the Remix Form element to display my form with various input fields. When there is a server-side validation error, the entered text in the fields remains, as expected. However, upon successful submission of the form, I would like ...

Stopping the Game Loop in Windows Phone 8.1 with WinJS

Despite everything working smoothly, I am facing an issue with stopping the game loop when the Start button is pressed and the app is moved to the background. The event handler for suspend, app.oncheckpoint = function(args) {}, does not seem to fire for t ...

Difficulty encountered in stretching HTML Table Footer to cover all columns

CAUTION: While the colors may not be optimal, they were specifically requested to align with my company's branding. Although I hope to update them in the future, for now this is how it stands. (If you have any suggestions on how to make them more tran ...

Integrating a dynamically created DOM node from JavaScript into a Vue component using Vue render functions

Is it possible to pass a JavaScript function that returns a DOM node representing a tree view with nested nodes into the Vue render function? I am aware that directly manipulating the DOM is not recommended in Vue, and using a recursive template would be ...

Implementing dynamic width with ng-style in AngularJS

I am trying to dynamically resize a div when an event is fired from S3. In the code below, I pass the progress variable to the updateProgress function as a parameter. This function resides in the top scope of my Angular controller. s3.upload(params).on(& ...

Three.js: "Identifying Tool" How to recognize the overlap between a 2D square and 3D elements

I am looking to achieve the following: I have a 3D map with objects, and I need to select all objects that fall within a specific 2D box defined by coordinates x1,y1 and x2,y2 on my screen. I am unsure of how to approach this task and would appreciate an ...

In relation to the adaptability of websites to smaller screens

Welcome to my first website! You can check out my portfolio at . I decided to go for free hosting on Webhost, but I've encountered a problem - despite using Bootstrap classes in my code, my page is not fully responsive on small screens. Any suggestion ...

Encountered an issue while attempting to load the required module

I'm having trouble setting up Stripe for my app and getting errors when trying to implement the module. Typically, I would require the module at the top of the file in order to use it, but when I do this in the paymentCtrl file, it doesn't work a ...

Implementing ajax for developing an authentication application, however, there seems to be a missing return statement

When I click "post", the response that I am getting from Firefox is: Password Username: k Source: username=k&password= The print received from the PHP won't display in the #errorshow div. Here is my code (HTML) <!DOCTYPE HTML PUBLIC "-//W ...

Clearing LocalStorage and Cache upon initial loading in Next.js

Our project requires continuous updates, and currently users are required to manually clear their cache and localStorage data after each update in order to access the new features. Is there a solution to automatically clear all cached data upon user visi ...

Pass information from a dynamic text field on the display to an AngularJS controller

In my webpage, I have implemented a drop down list feature. When the user selects an option from the drop down list, a corresponding text box appears for each selection. Here is the HTML code snippet- <select id="nut"> <op ...

Submitting a POST request without reloading the webpage

I am working on a new webchat application and encountered an issue - I want to send a message from a user without reloading the page. Here is what I have tried so far: index.php file: <head> <script src="https://ajax.googleapis.com/ajax/libs/jqu ...

What could be the reason that data-bs-placement="right" is not functioning as expected?

I am facing an issue with the popover functionality in my project. No matter what value I set for data-bs-placement attribute, it does not display correctly. Can you help me understand why this is happening? <!DOCTYPE html> <html lang="en ...