Prev & Next button with counter for overlay using jQuery
Asked Answered
O

1

13

I build this image gallery using jquerytools, I'm using scrollable div on thumbs and overlay on the main image... Everything works like charm..

EDIT: Before I make this a bounty...I have to explain that I need something clean and simple like this, because the images come from php (encrypted) , and I can't modify this, just the "view" as I need to achieve this with something like classes and ids. This is why I try this but...

The problem is I need to insert a Next and Prev Buttons when you are viewing the overlay... so you can go trough the images, once the overlay has been loaded..

I have made this fiddle for you my teachers full of wisdom can see what I am saying. http://jsfiddle.net/s6TGs/5/

I have really tried. but api.next() it's working for the scrolling on the thumbs , so I don't know how can I tell this script.. hey if next is clicked, yo pls insert next url on thubs, if previous btn is clicked, pls go to prev url on thumbs.. But I can't

Also and no less important a Counter like 1/8 have to be displayed =S... how in the name of JavaScript you do this..

Here is My code

$(function() {
$(".scrollable").scrollable();

$(".items img").click(function() {
    // see if same thumb is being clicked
    if ($(this).hasClass("active")) { return; }

    // calclulate large image's URL based on the thumbnail URL (flickr specific)
    var url = $(this).attr("src").replace("_t", "");

    // get handle to element that wraps the image and make it semi-transparent
    var wrap = $("#image_wrap").fadeTo("medium", 0.5);
    var wrap2 = $("#mies1");

    // the large image from www.flickr.com
    var img = new Image();

    // call this function after it's loaded
    img.onload = function() {

        // make wrapper fully visible
        wrap.fadeTo("fast", 1);

        // change the image
        wrap.find("img").attr("src", url);
        wrap2.find("img").attr("src", url);

    };

    // begin loading the image from www.flickr.com
    img.src = url;

    // activate item
    $(".items img").removeClass("active");
    $(this).addClass("active");

// when page loads simulate a "click" on the first image
}).filter(":first").click();
});

// This makes the image Overlay with a div and html

  $(document).ready(function() {
      $("img[rel]").overlay({

      // some mask tweaks suitable for modal dialogs
      mask: {
        color: '#ebecff',
        loadSpeed: 200,
        opacity: 0.9
      },

      closeOnClick: true
  });
});

I know here is part of my answer I just can make it work :(

http://jquerytools.org/demos/combine/portfolio/index.html

EDIT: Thanks to the first answer by QuakeDK I almost achieve the goal.. But the counter is not ok, also when you get to the 4 image (number 5 on counter) you cant go to the 5th thumb .. This is the CODE with that answer integrated

http://jsfiddle.net/xHL35/5/

And here is the CODE for PREV & NEXT BUTTON

//NExt BTN

  $(".nextImg").click(function(){
            // Count all images
            var count = $(".items img").length;

            var next = $(".items").find(".active").next("img");
            if(next.is(":last")){
                next = $(".items").find(".active").parent().next("div").find("img:first");
                if(next.index() == -1){
                    // We have reached the end - start over.
                    next = $(".items img:first");
                    scrollapi.begin(200);
                } else {
                    scrollapi.next(200);
                }
            }

            // Get the current image number
            var current = (next.index("img"));

            var nextUrl = next.attr("src").replace("_t", "");

            // get handle to element that wraps the image and make it semi-transparent
            var wrap = $("#image_wrap").fadeTo("medium", 0.5);
            var wrap2 = $("#mies1");

            // the large image from www.flickr.com
            var img = new Image();

            // call this function after it's loaded
            img.onload = function() {
                // make wrapper fully visible
                wrap.fadeTo("fast", 1);

                // change the image
                wrap.find("img").attr("src", nextUrl);
                wrap2.find("img").attr("src", nextUrl);
            };

            // begin loading the image from www.flickr.com
            img.src = nextUrl;

            $("#imageCounter").html("Image: "+current+" of "+count);

            // activate item
            $(".items img").removeClass("active");
            next.addClass("active");

        });

  //PREV BTN

    $(".prevImg").click(function(){
            // Count all images
            var count = $(".items img").length;

            var prev = $(".items").find(".active").prev("img");
            if(prev.is(":first")){
                prev = $(".items").find(".active").parent().prev("div").find("img:first");
                if(prev.index() == -1){
                    // We have reached the end - start over.
                    prev = $(".items img:first");
                    scrollapi.begin(200);
                } else {
                    scrollapi.prev(200);
                }
            }

            // Get the current image number
            var current = (prev.index("img"));

            var prevUrl = prev.attr("src").replace("_t", "");

            // get handle to element that wraps the image and make it semi-transparent
            var wrap = $("#image_wrap").fadeTo("medium", 0.5);
            var wrap2 = $("#mies1");

            // the large image from www.flickr.com
            var img = new Image();

            // call this function after it's loaded
            img.onload = function() {
                // make wrapper fully visible
                wrap.fadeTo("fast", 1);

                // change the image
                wrap.find("img").attr("src", prevUrl);
                wrap2.find("img").attr("src", prevUrl);
            };

            // begin loading the image from www.flickr.com
            img.src = prevUrl;

            $("#imageCounter").html("Image: "+current+" of "+count);

            // activate item
            $(".items img").removeClass("active");
            prev.addClass("active");    
        });

There must be a reward option here, if somebody help me I give you 20box! jajaja I'm desperate. Because now I also need to display title for each image, and I think it's the same process of URL replace, but next & prev is just something I can't manage.. Post the full solution and your email on paypal, I will pay 20!

Oarlock answered 29/5, 2013 at 22:36 Comment(0)
K
10

Okay, never tried jQueryTOOLS, so thought it would be fun to play with.

first of all, here's the JSFiddle I just created: http://jsfiddle.net/xHL35/1/

Now, the API calls need a variable to hold it

 $(".scrollable").scrollable();
 var scrollapi = $(".scrollable").data("scrollable");

Now scrollapi, can call the functions like this:

scrollapi.next(200);

I've copied your own code for choosing image and just rewritten it to fit the NEXT image. I haven't created the PREV function, but should not be that hard to reverse the NEXT function.

$(".nextImg").click(function(){
    // Count all images
    var count = $(".items img").length;

    // Finding the next image
    var next = $(".items").find(".active").next("img");
    // Is the next image, the last image in the wrapper?
    if(next.is(":last")){
        // If it is, go to next DIV and get the first image
        next = $(".items").find(".active").parent().next("div").find("img:first");
        // If this dosn't exists, we've reached the end
        if(next.index() == -1){
          // We have reached the end - start over.
          next = $(".items img:first");
          scrollapi.begin(200);
        } else {
          // Not at the end, show next div in thumbs
          scrollapi.next(200);
        }
    }

    // Get the current image number
    var current = (next.index("img"));

    var nextUrl = next.attr("src").replace("_t", "");

    // get handle to element that wraps the image and make it semi-transparent
    var wrap = $("#image_wrap").fadeTo("medium", 0.5);
    var wrap2 = $("#mies1");

    // the large image from www.flickr.com
    var img = new Image();

    // call this function after it's loaded
    img.onload = function() {
      // make wrapper fully visible
      wrap.fadeTo("fast", 1);
      // change the image
      wrap.find("img").attr("src", nextUrl);
      wrap2.find("img").attr("src", nextUrl);
    };

    // begin loading the image from www.flickr.com
    img.src = nextUrl;

    // Show the counter
    $("#imageCounter").html("Image: "+current+" of "+count);

    // activate item
    $(".items img").removeClass("active");
    next.addClass("active");

});

Hoping you can use this to develop the rest of the gallery.

Kriskrischer answered 31/5, 2013 at 23:8 Comment(13)
You are my hero!!! but you missunderstood one part, and im really sorry, sure was my bad english.. I need the NExt and Prev inside the "pop" or "main image" as i call it... so once it "pops" you can go trough all images (not outside de "pop"):D (thanks really its awesome, im sure i cant even do what yo just did!Oarlock
And i will need to style them isnt better to use a normal <a href>Oarlock
I just move the button and still works even inside the Overlay, pls let me know how can i change it to normal <a href> and i hope i can manage to do prev function. jsfiddle.net/xHL35/2 . If no better answer is given you are the winner palOarlock
@JulesMartinez Just change it to: <a href="javascript:();" class="nextImg">NEXT</a> and it should work as a link also.Kriskrischer
@JulesMartinez and thank you, loves to help :) throw a comment if the PREV dosn't do as you say :)Kriskrischer
really appreciate your words, and sharing wisdow..here you can see my prev code, i really try :( jsfiddle.net/xHL35/4 , its at the bottom you will find it below the code you insert.Oarlock
And just one more question, why the counter says 12 images?, ajaja and why if i direct pop 3er thumb the counter is not set to 3.. how can i do that?Oarlock
I can wait until tomorrow, the point of SO is that the answer is public, maybe this helps someone else, i will try to fix prev function until then thanks!!!!Oarlock
Thanks to you i almost got it.. pls just help me fix the counter , prev button is now working jsfiddle.net/xHL35/5Oarlock
@JulesMartinez - Actually there are 12 images, that's why the count. I've updated some css so you can see the next & prev buttons on the slider, and JS to scroll accordingly, hope this solves your issues: jsfiddle.net/xHL35/7Telegenic
This is so great!!!, Its working on the PHP site actually now, =) thanks to you, i just found a new problem, this line wrap2.find("img").attr("src", url); Seek for all images inside the wrap2 div... Can i "Omit" or "exclude" a div so the images there are not replaced for the current image... because all the images on the overlay ,even buttons are replaced for the current overlayed image..Oarlock
DO you know why when i insert this code here produce an error, i tried to disable jquery.min.js but i still get this errors queretaro.orsilin.com.mx/index.php?page=item/view/15/ProbandoOarlock
@JulesMartinez Just took a look, guessing you've fixed it? Because I can't see anything wrong.Kriskrischer

© 2022 - 2024 — McMap. All rights reserved.