SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery image swap with fade in and fade out

    I have a page with four images side by side. What I would like to do is use jquery to fade out the current image and fade in a new one as a hover effect on each image. I didn't want to do it with just plain css and image sprites because I wanted a smoother effect.

    I tried using this code. I could get the images to switch but couldn't figure out how to add the fade in and fade out to the images.
    Thanks

    Code:
    jQuery(document).ready(function($) {
     
    //rollover swap images with rel 
      var img_src = "";
      var new_src = "";
     
        $(".rollover").hover(function(){
          //mouseover
     
          img_src = $(this).attr('src'); //grab original image
          new_src = $(this).attr('rel'); //grab rollover image
          $(this).attr('src', new_src); //swap images
          $(this).attr('rel', img_src); //swap images 	 
        },
        function(){
          //mouse out
     
          $(this).attr('src', img_src); //swap images
          $(this).attr('rel', new_src); //swap images	 
        });
     
      //preload images
        var cache = new Array();
        //cycle through all rollover elements and add rollover img src to cache array
        $(".rollover").each(function(){
          var cacheImage = document.createElement('img');
          cacheImage.src = $(this).attr('rel');
          cache.push(cacheImage);
        }); 
    });
    Last edited by waldo567; Mar 5, 2012 at 21:36. Reason: forgot code

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    To do a fade, you have to create a second image and stack them on top of each other (with position: absolute or the like). Then do the fade. You can then add a timeout to remove the other image once the fade is complete.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here is what I came up with

    Here is what I came up with. I can't takle credit though, found the answer through google.

    Demo: http://waldrondigitaldesigns.com/gallery.html

    Jquery:
    Code:
     // wrap as a jQuery plugin and pass jQuery in to our anoymous function
        (function ($) {
            $.fn.cross = function (options) {
                return this.each(function (i) { 
                    // cache the copy of jQuery(this) - the start image
                    var $$ = $(this);
                    
                    // get the target from the backgroundImage + regexp
                    var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');
    
                    // nice long chain: wrap img element in span
                    $$.wrap('<span style="position: relative;"></span>')
                        // change selector to parent - i.e. newly created span
                        .parent()
                        // prepend a new image inside the span
                        .prepend('<img>')
                        // change the selector to the newly created image
                        .find(':first-child')
                        // set the image to the target
                        .attr('src', target);
    
                    // the CSS styling of the start image needs to be handled
                    // differently for different browsers
                    if ($.browser.msie || $.browser.mozilla) {
                        $$.css({
                            'position' : 'absolute', 
                            'left' : 0,
                            'background' : '',
                            'top' : this.offsetTop
                        });
                    } else if ($.browser.opera && $.browser.version < 9.5) {
                        // Browser sniffing is bad - however opera < 9.5 has a render bug 
                        // so this is required to get around it we can't apply the 'top' : 0 
                        // separately because Mozilla strips the style set originally somehow...                    
                        $$.css({
                            'position' : 'absolute', 
                            'left' : 0,
                            'background' : '',
                            'top' : "0"
                        });
                    } else { // Safari
                        $$.css({
                            'position' : 'absolute', 
                            'left' : 0,
                            'background' : ''
                        });
                    }
    
                    // similar effect as single image technique, except using .animate 
                    // which will handle the fading up from the right opacity for us
                    $$.hover(function () {
                        $$.stop().animate({
                            opacity: 0
                        }, 450);
                    }, function () {
                        $$.stop().animate({
                            opacity: 1
                        }, 450);
                    });
                });
            };
            
        })(jQuery);
        
        // note that this uses the .bind('load') on the window object, rather than $(document).ready() 
        // because .ready() fires before the images have loaded, but we need to fire *after* because
        // our code relies on the dimensions of the images already in place.
        $(window).bind('load', function () {
            $('img.fade').cross();
        });
        
        //-->
    HTML
    Code:
    <div id="gallery">
           <a href="engagements.html"><img src="Images/engagement-small.jpg" style="background: url(Images/engagement2-small.jpg);" alt="link to engagement gallery" class="fade" /></a>
        <a href="weddings.html"><img src="Images/wedding-small.jpg" style="background: url(Images/wedding2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
        <a href="weddings.html"><img src="Images/family-small.jpg" style="background: url(Images/family2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
        <a href="weddings.html"><img src="Images/senior-small.jpg" style="background: url(Images/senior2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
        </div>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •