Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 638469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:44:09+00:00 2026-05-13T20:44:09+00:00

I’m attempting to use JQuery to create a dynamic page header using an image

  • 0

I’m attempting to use JQuery to create a dynamic page header using an image that exists in the article. I plan to give the image a class (.thumbnail). I want to remove the image from the article once it is used for the header. Here is the logic:

  • Find IMG tag with .thumbnail class
  • Move that image to a new DIV (#dynHeader), class it .Image1
  • Scale the image to x pixels in height, maintain aspect for width
  • Find the width of the newly scaled image (var remainWidth)
  • Calculate the remaining width of #dynHeader
  • Duplicate the IMG to the right of .Image1, call it .Image2
  • Set its width to the value of remainWidth
  • Crop it to the height of .Image1
  • Position it on the Y axis with a specific value

I need to know how to find and duplicate the IMG .thumbnail. I’m sure more problems will surface as I continue working through this, but I am completely stuck. Am I thinking about this wrong? Is there a way to place the same image on the page twice?

Thanks for any help.

-alex

Edit: I’m posting the solution as I’m using it on my site for anyone else who might run into this problem. Took the code from the answer and tweaked it to function correctly.

 //need to clone before removing class so that the original is deletable later.
    var cache = $('img.thumbnail').clone().removeClass('thumbnail').addClass('Image1').css('float','left');
//remove the original from the text         
            $('img.thumbnail').remove();
//attach the cloned image to the header
            $('#dynHeader').append(cache);
//find the ratio
            var ratio = (cache.width() / cache.height());
//set variable to hold image height
            var imageHeight = (365);
//set variable to hold image width 
            var imageWidth = (imageHeight*ratio);
//set the image height & width
            cache.height(imageHeight).width(imageWidth);
//figure the amount of width remaining in the header
            var remainWidth = $('#dynHeader').width() - imageWidth;
//clone the header image
            var dupe = cache.clone();
//work with the cloned image - change the class to Image2, set the width and height         dupe.removeClass('Image1').addClass('Image2').css("width",remainWidth+"px").css("height","auto");
//place Image2 to the right of Image1
            cache.after(dupe);

Then I used some CSS to position Image2 and hide the overflow (the zoom & crop effect I was shooting for).

#dynHeader{
    height: 365px;
    overflow: hidden;
    margin-bottom: 30px;
}
img.Image2{
    position: relative;
    top: -150px;
}

Hope this helps someone else! Thanks Alex for pointing this the right way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:44:09+00:00Added an answer on May 13, 2026 at 8:44 pm

    This should get you started: (please bear in mind its 100% off the top of my head and its late here… could be some typos in there!)

    //Find IMG tag with .thumbnail class:
    $('img.thumbnail')
    
    //Move that image to a new DIV (#dynHeader), class it .Image1
    
    // change class before and then grab image
    var cache = $('img.thumbnail').removeClass('thumbnail').addClass('Image1').clone();
    
    // remove from context
    $('img.thumbnail').remove();
    
    // add it to the div
    $('#dynHeader').append(cache);
    
    // Scale the image to x pixels in height, maintain aspect for width
    // cache selector
    var cache = $('.Image1');
    
    // calculate aspect
    var ratio = (cache.width() / cache.height());
    
    // calculate & store width
    var remainWidth = (x*ratio);
    
    // scale to x pixels in height
    cache.height(x).width(remainWidth);
    
    // Calculate the remaining width of #dynHeader
    var remainHeaderWidth = $('#dynHeader').width() - remainWidth;
    
    // Duplicate the IMG to the right of .Image1, call it .Image2
    // assuming you mean "duplicate it and add to the right"
    var dupe = cache.clone();
    dupe.removeClass('Image1).addClass('Image2');
    
    // Set its width to the value of remainWidth
    dupe.width(remainHeaderWidth);
    
    // Crop it to the height of .Image1
    // not sure about the cropping, here's how you would change it without maintaing aspect
    dupe.height(cache.height());
    
    // add it after the first image
    cache.after(dupe);
    
    // Position it on the Y axis with a specific value
    // cant remember off the top of my head but as far as I know you have to first find its    
    // position on the axis and then shift it maybe applying relative css positioning
    

    It can definitely be improved but should give you the general idea 🙂

    Oh and as far as placing the same image on the page, no problem, just clone() the element and add it where you want as many times as you want!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As I am inside a package namespace, I have to… May 16, 2026 at 3:51 am
  • Editorial Team
    Editorial Team added an answer You could try testing for specific, known features of the… May 16, 2026 at 3:51 am
  • Editorial Team
    Editorial Team added an answer You could use them independently of the rubygems infrastructure by… May 16, 2026 at 3:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.