Skip to main content

Set first selected image in Galleriffic jQuery plugin?



I am using the Galleriffic jQuery plugin for my photo gallery. The gallery images are loaded from a database in PHP. I want to be able to link to one photo in the gallery from another web page, with an URL query that passes the ID of that image. This image should then be selected and shown in the big preview (or slideshow container), instead of the first image which is the default.





What I want to do is:







mywebsite.com/pictures/?pid=12345







where 12345 is the ID of the image to be selected.


Comments

  1. You can use some jQuery to click on a specific image after the gallery loads:

    $("img#"+id).trigger('click');


    There's a couple different way to populate the id variable. If you're using php as well you could use this:

    <?php echo "var id = ".$_GET['pid'].";"; ?>

    ReplyDelete
  2. or just do it in your client:

    $(function(){
    var id = location.href.replace(/.*pid=/, '');
    $.galleriffic.gotoImage(id);
    });


    You can try to add id to the hash of the URL instead of query strings.
    e.g.
    URL would be:

    http://mywebsite.com/pictures/#12345


    add these code to your page:

    $(function(){
    $.galleriffic.gotoImage(location.hash);
    });


    this would be better because Galleriffic doesn't change page URL when viewing different pictures.

    ReplyDelete
  3. I came up with a solution.

    While looping through the database results I compare the $_GET['pid'] with the ID of the current photo in the loop. If the values match i add the location of that photo (an iterating number starting at 1) into a variable.

    $i = 1;

    foreach($resultArray as $result) {
    if($_GET['pid'] == $result['ID'])
    $imagePos = $i;

    $i++;

    ...
    }


    After this loop I put the $imagePos variable into a javascript variable, so I can use it when the Galleriffic plugin has loaded. You must use the "" characters before and after the value to make $.galleriffic.gotoImage() method to work.

    if($imagePos)
    echo '<script type="text/javascript">var imagePos = "' . $imagePos . '";</script>';


    After I have loaded the Galleriffic plugin with $("#my_gallery").galleriffic({...}) I call the function $.galleriffic.gotoImage(position), using the imagePos variable I set earlier:

    $.galleriffic.gotoImage(imagePos);

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.