1
votes

I am having an issue with a simple slideshow that I made using jQuery. The script works fine in all other browsers except Google Chrome. You can find the live site here

There are two things wrong with it:

  1. The slide auto starts but stops after the second slide.
  2. If you click on the slide indicators (....), sometimes the background-image of that slide (which is applied via CSS) appears and sometimes it remains hidden.

On top of all that, I get this error:

event.layerX and event.layerY are broken and deprecated in WebKit. They will be removed from the engine in the near future.

Here is the JavaScript I am using:

function nextSlide() {
    var visibleSlide = $('#slider li:visible');
    var currentSlide = $(visibleSlide).index() + 1;
    var slideCount = $('#slider li').size();
    var nextSlide = (currentSlide == slideCount) ? 1 : currentSlide + 1;

    $('#slider_indicator a').removeClass('active');
    $(visibleSlide).fadeOut('fast', function() {
        $('#slider li:nth-child(' + nextSlide + ')').fadeIn('fast');
        $('#slider_indicator  li:nth-child(' + nextSlide + ') a').addClass('active');
    });
}

autoslide = setTimeout("nextSlide()", 6000);

$(function() {
    $('#slider_indicator a').bind('click', function(e) {
        clearTimeout(autoslide);
        $('#slider_indicator a').removeClass('active');
        $(this).addClass('active');
        var slide_number = $(this).parent().index() + 1;
        $('#slider li:visible').fadeOut('fast', function() {
            $('#slider li:nth-child(' + slide_number + ')').fadeIn('fast');
        });
        e.preventDefault();
    })
    $('#selection .scrollable .items a').live('click', function(e) {
        var self = $(this);
        $.ajax({
            url: $(self).attr('href'),
            type: 'GET',
            success: function(body) {
                var count = $('#selection .scrollable .items a').length - 1;
                count = (count == 1) ? count + ' Ribbon' : count + ' Ribbons';
                $(self).parent('li').fadeOut('fast', function() {
                    $('#result li a[rel="' + $(self).attr('rel') + '"]').removeClass('added');
                    $(this).remove();
                    $('#selection #header #count').text(count);
                })
            }
        })
        e.preventDefault();
    })
})​

Any help is greatly appreciated.

4
it stops in firefox 10.0.1 toodiEcho
For the error in console event.layerX... use latest version of jquery or see this stackoverflow.com/questions/7825448/…The Alpha
use setInterval, setTimeout will not continue.The Alpha
It's working now, clear your cache.The Alpha

4 Answers

2
votes

I'm trying to get what's wrong and why it's not working but:

var currentSlide = $(visibleSlide).index() + 1;

You don't need to wrap visibleSlide in jQuery since it's already a jQuery object.

slideCount = $('#slider li').size();

Usually length is used in this situation instead of size().

var nextSlide = (currentSlide == slideCount) ? 1 : currentSlide + 1;

Haven't tried, it might work, but I think it probably won't be a good solution for later on.

$('#slider_indicator  li:nth-child(' + nextSlide + ') a').addClass('active');

It looks weird, is it missing a quote, or has too much white space, don't know, it just looks like that won't work but I might be wrong. Maybe here's where your problem comes from.

autoslide = setTimeout("nextSlide()", 6000);

I think you're missing val right there.

$('#selection .scrollable .items a').live('click', function (e)

Do you need all those selectors? Also, I think live() is deprecated in favor of on().

var count = $('#selection .scrollable .items a').length - 1;
count = (count == 1) ? count + ' Ribbon' : count + ' Ribbons';

This just gives me the same feeling that it might work sometimes. I don't get why you define count and then you override it with a weird ternary operation. I feel like there might be a better way to do this.

1
votes

I don't see you declaring var autoslide anywhere.

And I think you want setInterval, not setTimeout

1
votes

As I answered in my comment that is working now but in the click event it's not working so you can simply use

$('#slider_indicator a').bind('click', function(e){
    clearInterval(autoslide);
    autoslide=setInterval("nextslide", 6000);
});

But to set the current index you may use your nextslide variable.

0
votes

Not experienced in jQuery, but I think your problem arises in this line:

$('#slider li:nth-child(' + nextSlide + ')').fadeIn('fast');

Should be:

$('#slider li:nth-child(" + nextSlide + ")').fadeIn('fast');

I am not seeing a problem though it could be just my computer.