Leverage jquery to adjust the height or width of an element depending on which dimension is larger

I'm a beginner in jquery and html and am currently developing a gallery webpage. The functionality involves setting the selected image as the page background and then scrolling vertically based on the mouse pointer's position. This works well for images that are taller than they are wide, but I also need it to scroll horizontally if the image aspect ratio requires it.

To achieve this, I am attempting to use an if/else if statement to determine whether to set the height or width to 100% in the .css. However, the code snippet I have isn't functioning as expected:

$('<img class="fp_preview"/>').load(function() {
    var $newimg = $(this);
    var $currImage = $('#fp_gallery').children('img:first');
    if (newimg.width() > newimage.height()) {
        jQuery('#fp_preview').css({
            height: "100%",
            width: ""
        });
    }
    else if (newimg.height() > newimage.width()) {
        jQuery('#fp_preview').css({
            height: "",
            width: "100%"
        });

    }
    $newimg.insertBefore($currImage);
    $loader.hide();

The closing brackets and syntax are all correct. I've added the if statements based on code I found online, but it's not working. "fp_preview" is the part of the .css responsible for displaying the background image.

If anyone has insight on how to accomplish this, I'd greatly appreciate the assistance.

Here is the complete code and the portion of the .css I'm trying to modify:

And here's the corresponding .css:

img.fp_preview{
position:absolute;
left:0px;
top:0px;
width:0%;  /* Set to 100% if image is taller than wide */
height:0%; /* Set to 100% if image is wider than tall */
}

Thank you in advance for your help. This project was inspired by a tutorial from Codrops: http://tympanus.net/codrops/2010/09/08/full-page-image-gallery/

Answer №1

It may have been a while since the last response, but I was able to successfully make this function.

if($picture.height() > $picture.width()){

$picture.addClass('image_preview1');

}
else
{
$picture.addClass('image_preview2');
}

Answer №2

Consider trying this approach:

if($newimg.width() > $newimage.height())

as opposed to:

if(newimg.width()>newimage.height())

It's important to note that the "$" sign in this context is not functioning as it does in PHP or JQuery. Here, the "$" is simply a part of the string character, so $newimg and newimg are considered as two distinct variables.

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

Leveraging summernote in meteor with Discover Meteor tutorial

Recently, I've been delving into the world of Discover Meteor and encountering some challenges along the way. My current hurdle involves integrating content entered in summernote into mongo, but it seems to be more complicated than expected. The fil ...

Leveraging bootstrap for achieving vertical and horizontal centering of images

My webpage uses bootstrap to display content, and while I've managed to center my images vertically, I'm facing an issue with horizontal centering. I want to ensure that any image, regardless of size, remains within the column/row and adjusts it ...

Issue with scrolling feature in div

I am currently facing an issue with scrolling on my website. Visit this site to see the problem. When scrolling down, it causes the "hidden" part of the site to slide up. I want to achieve a similar sliding effect, but it needs to be smooth. I have attempt ...

What is the best method to iterate through a jQuery array variable without any errors?

I am struggling with a PHP project where I need to create a json array of data values that will be used in a jQuery script. The array looks something like this: [3,94,83,141]. My goal is to leverage these values to manipulate the visibility of rows in a ta ...

Modify the universal variable through a jQuery action

As a newcomer to jQuery with limited experience in JavaScript, I find myself facing a dilemma. I am working on a jQuery range slider that displays two year values, and I have successfully stored both the minimum and maximum years in a variable. However, I ...

Jquery script that utilizes the Steam WebAPI to return data

I'm encountering an issue with a script I found on github. I added value.appid myself, thinking it was logical, but I believe that data.response.games contains values for all my games. How can I either print or view what is defined? I would like to ...

Not all words are compatible with word-wrap, don't you think?!

I have a situation where I used the following CSS properties for a div: word-wrap: break-word; text-align: justify; When a single word is too long to fit within the width of the div, it wraps or breaks into multiple lines. However, if a second word with ...

Make sure to use jQuery waterfall 'reflow' only once all the images have finished loading

Currently, I am utilizing jQuery waterfall to achieve a grid-style display on my website. To address the issue of images overlapping, I have enclosed the waterfall method within a .load() function like this: $(window).load(function(){ $('#buildcon ...

mobile-friendly sticky footer

Currently working on a website project utilizing Bootstrap 3 (http://www.patrickmanser.ch/fnws) and I am trying to implement a sticky footer. After adapting the code snippets from Bootstrap 3 examples according to my specifications, everything seems to fun ...

Encountering a 400 (Bad Request) error while attempting to submit a form to Facebook using Ajax and jQuery

I need help with posting an image to a Facebook album using a form on my website. I have the correct access token and know that posting without jQuery or AJAX works, but it redirects me away from my site. To fix this, I attempted a jQuery AJAX post, but en ...

Ways to eliminate excessive spacing between grid blocks

Hey, I'm having an issue with displaying ads in a thumbnail format; there seems to be too much space between them. Take a look: I've been utilizing Bootstrap thumbnails to showcase products. echo '<div class="col-sm-6 co ...

Display an Open Dialog window when a Button is clicked

On a button click, I need to display an Open Dialog box. To achieve this, I am utilizing the FileUpload control for file uploading purposes, however, I prefer not to expose it to the user and would rather display a Button instead. ...

Show the total amount after multiplying the quantity by the price for WooCommerce products with only one variable

Having come across this code snippet that calculates the subtotal for WooCommerce single products based on quantity, I found it only works for simple products and not for product variations in variable products. I am seeking help to tweak the code so it ca ...

The HTML element update event was not triggered due to the excessive load of a JavaScript function

I am currently running a JavaScript function that is quite CPU intensive. To provide visual feedback to users when it starts and stops, I am attempting to display a badge on the webpage. Below is the code snippet: function updateView() { console.log(1 ...

Using jQuery to Retrieve a Table Header Value

I am currently facing an issue with retrieving table header values using a jQuery function. My timetable module has a view page that displays the schedule in table format, showing period timing and weekdays. days 09.00AM - 10.00AM 10.00Am-11.00AM 11 ...

Combining the powers of Javascript and Drupal can create

My current setup includes an "open video" button and a form that triggers an ajax call to render files with the corresponding buttons when users click on handouts or videos. I have encountered a problem: Whenever I click on "open the video" after renderi ...

I am interested in utilizing css to establish an iframe as a background

Hello fellow programmers on stackoverflow, I am curious if it is feasible to utilize an iframe as a background using CSS? Allow me to elaborate: Currently, I have a div named "lead" on my website. Whenever this div is used in my HTML files, the image ...

What is the best way to align background shapes within a specific section on a webpage using CSS?

I've been attempting to strategically place multiple shapes in the background using CSS, but I'm facing challenges. While I was successful with my hero section at the top of the page, positioning elements in a section halfway down has proven prob ...

determining CSS selector

Having trouble identifying a locator using CSS. There are 10 elements with the same locator name on the webpage. Each web element has the same XPath value: //div[@class='thumb_image'] The web element list size is 10. If I want to access the 5t ...

What is the method to retrieve a randomly generated class from an id?

Is there a way to target elements that have the class "my_class" within the element with the id of "info_id"? It's important to note that these elements may also have another class, which I'm not interested in selecting. <div id="info_id"> ...