Question

Thank you all having a look at my question.

I am trying to write my own jQuery image gallery plugin. It works fine when the page loads but it doesnt work when I click on PHOTO link to go to the PHOTO section.

Here is the link:

http://dharmeshpatel.net/core_reporting_api/flat-page.html

Here is what I have done:

$(function() {
  for (var i = 1; i <= 12; i++) {
   var name = i + '.jpg';
   if(i<10) name = '0' + name;
   var img = $('<img class="galleryImage" src="pic/'+ name + '"/>');
   $("#gallery").append(img);
  };
  var g = new  PS.Gallery('gallery');
});

Here is the plugin code:(on page refresh it works but when I click on menu link and go to that div it the hover effect doesn't work.)

(function(ns){
        ns.Gallery = function(id){
        var gallery = $('#'+ id);
        var images  = gallery.find('.galleryImage');

        var xPos = 50;
        var yPos = 50;
        var gWidth = gallery.width();

            images.each(function(idx,el){
                var img = $(el);
                img.data('homeX',xPos);
                img.data('homeY',yPos);
                img.data('currentScale',1);
                img.css({
                    'left':xPos,
                    'top':yPos,
                    'width':img.width() * 0.25
                });
                xPos += 95;
                if(xPos > gWidth - 100){

                    xPos = 50;
                    yPos += 115;

                }

                img.hover(
                    function(event){
                    var target = $(event.target);
                        animateImage(target,500,target.data('homeX'),target.data('homeY'),2);
                    },function(event){
                        returnAllToNormal();
                    }
                );
                var returnAllToNormal = function(){
                    images.each(function(idx,el){
                        var img = $(el);
                        animateImage(img,500,img.data('homeX'),img.data('homeY'),1);
                    });
                }
                var animateImage = function(img,duration,left,top,scale){
                        img.stop();
                        img.css('textIndent',img.data('currentScale'));
                        img.animate({
                            'left':left,
                            'top':top,
                            'textIndent':scale
                        },
                        {
                        duration:duration,
                        step:function(now,fx){
                            if(fx.prop === 'textIndent'){
                                img.data('currentScale',now);
                                img.css('transform','scale(' + now + ')');
                            }else if(fx.prop === 'left'){
                                img.css('left',now);
                            }else if(fx.prop==='top'){
                                img.css('top',now);
                            }
                        }
                    });
                }
            });
        }
    }(window.PS = window.PS || {}));

Am I calling the plugin in right place??

Was it helpful?

Solution

When I changed top value of your div#con to -2500px your gallery kind of broke. On exploring further got to know that you are storing current x and y position of individual image, which is creating the issue.

Rather than storing current values, play on current values. Below is problematic code :

var img = $(el);
img.data('homeX',xPos);
img.data('homeY',yPos);
img.data('currentScale',1);
img.css({
    'left':xPos,
    'top':yPos,
    'width':img.width() * 0.25
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top