Instead of having a fixed start position, I'd like the carousel to automatically select an image based on a css selector. That way, a CSS selector such as .jcarousel img.current could be specified and it would be scrolled to on page load. I think there will also need to be an option to recurse up from the selected node to the first li tag to get the index of the item to start with.

I'll be starting with a 6.x patch as that's what the site I need this on is currently running, but will port to 7.x as well.

Comments

deviantintegral’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
Status: Active » Needs work
StatusFileSize
new5.07 KB

Here's a patch that applies to both 6.x and 7.x. I haven't tested it on D7 yet, but will do so shortly.

deviantintegral’s picture

Status: Needs work » Needs review
StatusFileSize
new6.49 KB

Here's an additional commit that fixes calculating the starting page when the pager size is set to "Auto".

deviantintegral’s picture

StatusFileSize
new9.62 KB
new9.58 KB

There was an issue with scrolling in Chrome. It looks to be running the setupCallback in a separate thread (cool!) so it was getting called after the we needed the pageStart variable it sets up. The attached patch moves the scroll into the setup callback to fix this.

mvc’s picture

Status: Needs review » Reviewed & tested by the community

works as advertised. thanks!

jnettik’s picture

Status: Reviewed & tested by the community » Needs work

First off thank you so much to @deviantintegral. This saved me a ton of work in figuring this out myself!

I did have trouble with the patch in #3 though in my use case. I'm preprocessing the view output to put my active class on the jcarousel

  • rather than on one of the internal elements. It seems the patch is expecting the provided selector to be a child of the carousel item and doesn't see my class on the
  • . Changing line 54 from:
      var $current = $carousel.find(options.start_css).parents(options.selector + ' li');
    

    to:

      var $current = $carousel.find(options.start_css);
    

    makes everything work for me. Not sure if if there is a reason against just using .find().

  • rankinstudio’s picture

    Could someone explain how to use this? Where do I set the active class, how does the script use it? Step by step?

    Thanks :)

    mvc’s picture

    @rankinstudio: here are the steps:

    • apply deviantintegral's patch
    • edit the view display's jcarousel options: under "Advanced", set "Start position" to "Use a CSS selector" and in the field "Start CSS Selector" enter something like "img.jcarousel_current"
    • add that css class to the active item in your carousel. for example, you could add a node id field to your view then in the views field template for your image you could do something like the following to see if the carousel item's node id matches the node id of the current page: (code is for drupal 6)
    if (arg(0) == 'node' && arg(1) == $row->nid) {
      $output = preg_replace('@(<img .*?class="[^"]*)"@', '$1 jcarousel_current"', $output);
    }
    print $output;
    

    note however that if you have a carousel of links to nodes and you just want to have the carousel positioned so that the current node is displayed in the carousel on page load, you can use the simpler approach discussed at #936932: Setting jcarousel start position to last position user saw which doesn't require you to add your own css class.

    LaurenW’s picture

    Can anyone explain how to add a css class to the active item in your carousel for Drupal 7? #7 offers some code for Drupal 6, but I'm not sure if the code is the same or where to edit the views field template for my image.

    Thanks.

    mvc’s picture

    the above code should also work for D7. for general information on views field templates, you could start here:

    http://views-help.doc.logrus.com/help/views/using-theme
    http://www.flowbyte.com/journal/drupal-7-theming-views-field-templates

    LaurenW’s picture

    Thanks! I got it working now. It does still load the page with the first set of thumbnails in the carousel and then switches to the active/current one. Any way to load the current one first and not see that flipping?

    Thanks

    mvc’s picture

    @LaurenW: sorry, but that would take a larger change to the module, and this was good enough for my client, so i stopped here.

    mvc’s picture

    Issue summary: View changes
    Status: Needs work » Reviewed & tested by the community

    i'm migrating the site i first used this patch for a year and a half ago to D7, and the patch in #3 still applies cleanly & works perfectly. in fact, in D7 by linking a field in my view display to its original source node i was able to use the css selector a.active and avoid all view preprocessing or template hacking.

    @jnettik, your change is necessary if you add a custom class to the jcarousel li tag but i would simply suggest that you put it on something contained in that tag so that the original patch will work for you.

    scsmith_calarts’s picture

    It seems that this patch no longer works with the latest version of jcarousel (v 7.x-2.7).

    I get this result:
    patching file includes/jcarousel_style_plugin.inc
    Hunk #1 succeeded at 22 (offset 1 line).
    Hunk #2 FAILED at 137.
    1 out of 2 hunks FAILED -- saving rejects to file includes/jcarousel_style_plugin.inc.rej
    patching file js/jcarousel.js
    Hunk #1 succeeded at 78 (offset 12 lines).
    Hunk #2 succeeded at 232 (offset 29 lines).
    Hunk #3 succeeded at 242 (offset 29 lines).
    patching file js/jcarousel.js
    Hunk #1 succeeded at 84 (offset 12 lines).
    Hunk #2 succeeded at 97 (offset 12 lines).
    patching file js/jcarousel.js
    Hunk #1 succeeded at 55 with fuzz 1 (offset 9 lines).
    Hunk #2 succeeded at 75 with fuzz 2 (offset 12 lines).
    Hunk #3 succeeded at 97 (offset 12 lines).