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

  • SEARCH
  • Home
  • 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 6331305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:02:17+00:00 2026-05-24T18:02:17+00:00

Here’s my relevant code, I’m switch the image according to where I click. Is

  • 0

Here’s my relevant code, I’m switch the image according to where I click. Is there some way for me to gradually switch the images? Maybe animate them while switching? A sort of fade in fade out.

<script type="text/javascript">
    jQuery(document).ready(function ($) { //fire on DOM ready
        $('#mainproductpicture').addpowerzoom({
            defaultpower: 2,
            powerrange: [2, 5],
            largeimage: null,
            magnifiersize: [200, 200] //<--no comma following last option!
        })

        $('#smallpictureone').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));

            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });

        $('#smallpicturetwo').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));

            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });

        $('#smallpicturethree').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));

            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });

        $('#smallpicturefour').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));

            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });

        $('#smallpicturefive').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));

            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });
    });
</script>

I have another related question about my javascript code. I’m using a Javascript library I found online that allows me to zoom the image around very nicely. However, when I switch the src to another image, the zoom remain on the first. So I’m ‘re-hooking’ the library to the image each click I make. Does this have a negatie performance hit I’m not aware of? Will a current gen correctly clean up after me?

  • 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-24T18:02:18+00:00Added an answer on May 24, 2026 at 6:02 pm

    Just because I’m a stickler; You can super-simplify your JS code by reducing your code repetition by using a better selector. You can chain selectors by using a comma to separate them.

    <script type="text/javascript">
    jQuery(document).ready(function ($) { //fire on DOM ready
        $('#mainproductpicture').addpowerzoom({
            defaultpower: 2,
            powerrange: [2, 5],
            largeimage: null,
            magnifiersize: [200, 200] //<--no comma following last option!
        })
    
        $('#smallpictureone, #smallpicturetwo, #smallpicturethree, #smallpicturefour, #smallpicturefive').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src"));
    
            $('#mainproductpicture').addpowerzoom({
                defaultpower: 2,
                powerrange: [2, 5],
                largeimage: null,
                magnifiersize: [200, 200] //<--no comma following last option!
            })
        });
    });
    </script>
    

    But you could do even better by not targeting those ids at all and using a class instead. eg:

    $('.smallpicture').click(function(){ /* ... */ });
    

    You can also reduce your repetition by declaring your “addpowerzoom” options beforehand and reusing the variable reference. eg:

    var powerZoomOpts = {
      defaultpower: 2,
      powerrange: [2, 5],
      largeimage: null,
      magnifiersize: [200, 200]
    };
    

    Then in your call to initialize the power zoom plugin:

     $('#mainproductpicture').addpowerzoom(powerZoomOpts);
    

    But wait! There’s more. You can also chain the powerzoom onto the attr() call on the line before that. eg:

    $("#mainproductpicture").attr("src", $(this).attr("src")).addpowerzoom(powerZoomOpts);
    

    For a final result of:

    <script type="text/javascript">
    jQuery(document).ready(function ($) { //fire on DOM ready
        var powerZoomOpts = {
            defaultpower: 2,
            powerrange: [2, 5],
            largeimage: null,
            magnifiersize: [200, 200]
        };
    
        $('#mainproductpicture').addpowerzoom(powerZoomOpts);
    
        $('.smallpicture').click(function () {
            $("#mainproductpicture").attr("src", $(this).attr("src")).addpowerzoom(powerZoomOpts);
        });
    });
    </script>
    

    Doesn’t that look so much better?

    In order to cross-fade (as others have stated) You must have two img elements. One positioned overtop the other. The bottom image should start off being hidden from view by the image on top. It won’t yet have a src attribute.

    The second step is to change the src of the bottom img to be the image you want to crossfade to. Then you .fadeOut(200) the top image. Use a callback function to change the top image’s src attribute to the bottom image’s src attribute so as to ‘prime’ the set of images for the next crossfade.

    Here’s an example:

    var $topimg = $('#topimage');
    var $bottomimg = $('#bottomimage'); /* cache $topimg & $bottomimg jQuery objects for later use */
    
    $('.fadeable-images').click(function(e){
      $bottomimg.attr('src', $(this).attr('src'));
      $topimg.fadeOut(200, function(){
        $topimg.attr('src', $bottomimg.attr('src')).show();
      });
    });
    

    The rest is CSS to align the two image tags in the appropriate positions.

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

Sidebar

Related Questions

Here's some of my production code (I had to force line breaks): task =
Here's my code: // Not all headers are relevant to the code snippet. #include
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?

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.