views:

100

answers:

1

Have been succesful in adding a jcarousel to navigate around my html site which is built with dynamic template. However, i need an image link to appear active when i am on the page it is linked to so the viewer knows where they are. Also, whenever i go to a new page the jcarousel goes back to the beginning of its scroll position when i need it to stay in the last postion it was in. Hope that makes sense. I found a great demo here which i have downloaded, but can't figure how to remove the elements i want from the image gallery in the demo. http://blog.themeforest.net/tutorials/how-to-integrate-the-jquery-galleria-and-jcarousel-plugins/ Hope you can help!

A: 

Something like this should get you started.

EDIT

Here is a more complete example. Now the start value is pulled from your url.

For instance. If the URL to your website is www.mysite.com/page2.html, you can add on a URL parameter (in this case 'startVal') which can be accessed via JavaScript.

So your URL will look like "www.mysite.com/page2.html?startVal=2" where startVal=2 determines which item in the carousel is set as the selected start item.

<script type="text/javascript">

var $sel = null;
$(document).ready(function () {

    // This function helps pull out items from your URL. (in this case 'startVal') 
    $.urlParam = function (name) {
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if(results == null){ //if results is null then return "0"
            return 0;
        }
        return results[1] || 0;
    }

    //Get the value of startVal from the QueryString. (in the example below, it would return 2)
    //EXAMPLE: www.mysite.com/page2.html?startVal=2
    var startVal = parseInt($.urlParam('startVal'));

    $('#mycarousel').jcarousel({
        start: startVal //Use the value of startVal to determine which item the carousel should default to on page load.
    });

    //Get the image you wish to default as selected, again we do this based off the startVal we received from the URL
    $sel = $('#mycarousel li:nth-child(' + startVal + ')').find('img');
    $sel.css('border', 'solid 2px blue'); //Here we can format it however we want

    //This function assigns a click event to each item in the carousel and changes which one is selected. 
    $('#mycarousel img').click(function () {
        $sel.css('border', 'solid 0px white');
        $(this).css('border', 'solid 2px blue');
        $sel = $(this);
    });
});

</script>

EDIT

You'll also need to add in your own validation. Right now, I don't check to see if "startVal" is null, or that the requested start index is in the range of available items.

EDIT

So for each URL on your site, you'll need to add a querystring parameter to determine which item in the Carousel is selected.

Examples:

  • www.mysite.com/page1.html?startVal=1
  • www.mysite.com/page2.html?startVal=2
  • www.mysite.com/page3.html?startVal=3
  • www.mysite.com/page4.html?startVal=4

You'll need to validate that the requested item actually exists. Otherwise if the URL requests item number 698 (www.mysite.com/page4.html?startVal=689) it won't exist and you may get errors.

Didn't mean to make this more confusing, but I hope this added some clarity.

Brandon Boone
Hey thanks! That's a great start. I can see that my scroller starts 3 images in. Only trouble is my script is in my web template and i have 50 pages so will have to look at how i can unattach that bit for each to set for each page. But happy to do that at this stage if no other way. I see the active border style, but it doesn't stay selected once you are on the selected page (i know from looking at alot of other forums this is a big issue ). Any ideas? Thanks for this anyway, i feel that this is a big step forward.
See my updated answer. Let me know if there's anything you don't understand.
Brandon Boone
Thanks for further help.
Apologises, but you have lost me, am in over my head and very frustrated as i know this can bedon't. Guess my only option is to apply the changes to each page as per your original code :(
Not sure if you got this first time round, but here is how code is on my page, bit of extra script there which was working fine with your other suggestion, now i have lost carousel when previewing.
Didn't mean to make you more confused or add to your frustration. I've updated my answer again with some annotations that I hope will clear some of this up for you. Perhaps if you provide a link to your site or post some of your source code it would better help me and others troubleshoot why it's not working.
Brandon Boone
i have added this in but on previewing the page the carousel does not show. I have opened the error box in the browser and it is saying '1' is null or not an object which is referring to this line of code ' return results[1] || 0; '
Also, wasn't sure where this should goif(startVal == null) { startVal = 1 //So it will always default to 1 incase it wasn't supplied in the url. }
@superstar87 - I've updated my answer, so you shouldn't get any errors now, but I feel you still don't understand that you **need** to add "startVal=A NUMBER" to your URL for this to work. That's why the carousel didn't show up in your tests and that's why you were getting errors. I have numerous examples above on how it should work and what you have to do to get a specific image selected on page load. If you're struggling with the concept of URLs (web addresses) and/or query parameters, let me know and I'll try to elaborate even further.
Brandon Boone
Thank you! Thank you! Thank you! It's brilliant! Thanks for your knowledge, your patience and your generosity, it is very much appreciated. best regards.
@superstar87 - my pleasure. If you could mark this as the answer I'd appreciate it as well.
Brandon Boone