Utilizing jQuery animation, generate a zoomOut effect on a square-shaped image that adjusts to fit the width or height of the window while preserving its

Right off the bat: using scale() is not a viable option.

Is there a way to dynamically animate my image, which has been scaled to 700% in width or height, back to its original CSS dimensions of auto width and height? Below are the actual dimensions of my image ( .backone > img ):

Width: 1024px Height: 570px

The CSS for the image is as follows:

/*this matches the window width and height*/
.backone {
width: 100%;
height: 100%;
z-index: 6;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}

/** original CSS dimensions **/
.backone > img {
position: absolute;
width: auto;
height: auto;
min-height: 100%;
min-width: 100%;
left: 50%;
top: 0px;
bottom: auto;
transform: translateX(-50%);
}

While hidden, I added either a height of 700% or a width of 700% to the .backone > img during the script execution, depending on whether the window width is greater than the height.

Now, when the image is displayed, I want to animate it back to its "original CSS" dimensions as indicated above.

Here's what I've tried so far:

  • I attempted to capture the image's dimensions before applying the 700% scaling, but since the image was hidden at that time, it had not yet rendered in the DOM.

  • CSS animations were explored, but they only allowed me to revert the width or height back to 100%, resulting in distortion of the image.

  • I made multiple attempts with jQuery animate to calculate the correct dimensions, but encountered difficulties in getting the calculations right.

What calculations can be used to return the image to its original dimensions?

Answer №1

UPDATE: After reassessing the issue, I realized my approach was incorrect. Instead of trying to adjust the image size based on a 700% width or height, I simply needed to calculate the ratio to properly fit the screen.

    function getImgdimes() {
        var w = $(window).outerWidth();
        var h = $(window).outerHeight();
        var iw = 1024; // original image width
        var ih = 570; // original image height
        var diff = iw / w;
        if ((ih / diff) < h) {
            var nw = iw / (ih / h);
            var nh = h;
            $('div.backone img').css('height', '700%');
        } else {
            var nw = w;
            var h = ih / diff;
            $('div.backone img').css('width', '700%');
        }
        var m = [nh, nw];
        return m;
    }

    The animate function utilizes the values m[0] and m[1].

You can view this updated solution here:

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

Node.js causing issues with executing jQuery front-end code properly

I created an automatic image scroller div consisting of a ul with small images. The aim is to have a basic scroller that starts running once the page loads. It functions perfectly when I test the code (HTML, CSS, and a JS file with jQuery) locally in a bro ...

What is the process for spinning an image?

Is there a way to rotate an image around its center? I am looking for a solution where the image rotates continuously when clicked, but stops if an ajax call is unsuccessful. Please assist me in finding a resolution. ...

Tips for inserting a jQuery snippet into a universal file

Seeking some advice on integrating jQuery code into my JavaScript file, which is located at . The code snippet I am attempting to add looks like this: $(document).ready(function() { queue = new Object; queue.login = false; var $dialog = $ ...

Creating a grid layout with images encapsulated within a div using HTML

I am struggling to create a grid of images with padding between them inside the main_block div. The issue I'm facing is that I can't get the images to align next to each other using inline block or break them with a because they go in line ins ...

Creating a fixed top bar button in Ionic and iOS along with a scrollable list

In my app using Ionic with the iOS platform, I am trying to achieve a specific layout: A header A fixed bar button A scrollable list Here is what I have attempted so far: <ion-view view-title="Account"> <div class="button-bar" style="margin ...

Can a jQuery tab container be integrated into a Dojo dialog window?

I have been struggling to create a jQuery tab container within a Dojo dialog. Despite numerous attempts, I have not been successful. Is it possible to accomplish this task? If so, do you have any suggestions on how to achieve it? Thank you in advance. ...

Preventing input in one textbox if another textbox has a filled value in HTML

Apologies for asking this question, but is there a way to disable one text box if another text box has a value? I've attempted using the following code, but it doesn't seem to work. Sorry for the inexperienced inquiry T_T function disableTextbox ...

Customizing the background of a cktext_area in Rails: A step-by-step guide

Currently, I am working with Rails 3.0.9 and utilizing the ckedit gem. In one of my views, I have included the following code snippet: <%= f.cktext_area :content, :toolbar => 'Reduced', :width => 550, :height => 300 %> I'm ...

Utilizing JQuery's printThis Plugin in Combination with the Style Attribute

I am currently working on a JavaScript app and I am trying to implement a button that will allow users to print a specific div. To achieve this, I am utilizing a jQuery plugin called printThis (github link) as well as attempting to use window.print(). $(" ...

Modify the color of drop-down menu links

Recently, I set up a drop-down menu containing links. Initially, when hovering over the names in the menu, they would change color and display the drop-down. I had successfully made all the names golden, with the hovering name turning black while display ...

Is it possible to have Bootstrap 3 Navigation Tabs positioned at both the top and bottom of a

While working on a Bootstrap 3 website, I discovered that it is possible to have both a top nav-tab and bottom nav-tab section on the same page. These two distinct tab menus can control the display of the same tab content areas. However, I encountered an ...

Unique design and elements featuring the `width: auto` property for custom fonts

In my current project, I am utilizing the popular 'Open Sans' font family. However, I am encountering a specific issue. The problem arises with buttons and other elements that have width: auto. Upon page load, these elements initially have their ...

Using Jquery to Retrieve the Content of Head and Body Tags from a String

Here is a string that I currently have: <head> This is the Head </head> <body> <div> <div> <p> Body Content <br /></p> <p>&nbsp; Hello World <br />< ...

What causes the modal to appear and then vanish suddenly?

I've created a simple modal code with minimal JS involved. When the button is clicked, the modal appears briefly, then the page refreshes and the modal disappears. I'm not sure what could be causing this issue. Any help would be appreciated! h ...

HTML - Table Layout Overlap

I'm struggling with a table layout issue in HTML and could use some assistance. The table below is displaying as overlapping in IE (please refer to the image), but it appears correctly in FF. Can anyone offer some guidance? <div> <table> ...

Issue with button vanishing upon clicking page forward in pagination for table

When I click the next button on the table pager, a button that is inline with the pager disappears. I'm not sure why this is happening. If you want to see my code, check out this JS Fiddle: $table.trigger('repaginate'); var pager = documen ...

Random variable without repetition in SASS

I have a unique project where I am tasked with incorporating a list of 5 specific colors into 10 different div elements. The challenge is that each color must be used twice and in a completely random order without any repetition of colors. Although utilizi ...

The function .click does not function properly when used with an anchor element within a figure tag

In my JavaScript-loaded figure, there is an image description and two buttons. Sometimes the description contains a link with a fig attribute, like this: <a fig="allow" href="#tt5">[1]</a> If the anchor is not in a figure and has a fig attrib ...

Javascript - EventListener triggers responses to clicks on the entire span on both its left and right sides

I recently delved into the world of JavaScript and decided to challenge myself by creating a TicTacToe game for educational purposes. If you're curious about what my simple game looks like so far, here's a link to it: In the game, I prompt the ...

I found myself unable to navigate the web page through scrolling

Experiencing difficulty scrolling on my web page with the code provided. If you'd like to see the issue in action, you can view a live demo HERE JavaScript <script> $(window).bind('scroll', function () { if ($(window).scrollTop ...