Skip to content Skip to sidebar Skip to footer

Using A Jquery Slider For Text Instead Of Images?

This may be a little too specific, but I have a jquery slider that I am using

classes instead of images to cycle through customer quotes. Basically the problem I am runni

Solution 1:

So I sort of made this my Friday project. I've changed a whole lot of your code, and added a vertical-align to the quotes and authors.

Here's the fiddle http://jsfiddle.net/qLca2fz4/49/

I added a whole lot of variables to the top of the script so you could less typing throughout.

$(document).ready(function () {
    //rotation speed and timer
    var speed = 5000;

    var run = setInterval(rotate, speed);
    var slides = $('.slide');
    var container = $('#slides ul');
    var elm = container.find(':first-child').prop("tagName");
    var item_width = container.width();
    var previous = 'prev'; //id of previous button
    var next = 'next'; //id of next button

Since you used a % based width I'm setting the pixel widths of the elements in case the screen is reszed

    slides.width(item_width); //set the slides to the correct pixel width
    container.parent().width(item_width);
    container.width(slides.length * item_width); //set the slides container to the correct total width

As you had, I'm rearranging the slides in the event the back button is pressed

    container.find(elm + ':first').before(container.find(elm + ':last'));
    resetSlides();

I combined the prev and next click events into a single function. It checks for the ID of the element targeted in the click event, then runs the proper previous or next functions. If you reset the setInterval after the click event your browser has trouble stopping it on hover.

    //if user clicked on prev button

    $('#buttons a').click(function (e) {
        //slide the item

        if (container.is(':animated')) {
            return false;
        }
        if (e.target.id == previous) {
            container.stop().animate({
                'left': 0
            }, 1500, function () {
                container.find(elm + ':first').before(container.find(elm + ':last'));
                resetSlides();
            });
        }

        if (e.target.id == next) {
            container.stop().animate({
                'left': item_width * -2
            }, 1500, function () {
                container.find(elm + ':last').after(container.find(elm + ':first'));
                resetSlides();
            });
        }

        //cancel the link behavior            
        return false;

    });

I've found mouseenter and mouseleave to be a little more reliable than hover.

    //if mouse hover, pause the auto rotation, otherwise rotate it    
    container.parent().mouseenter(function () {
        clearInterval(run);
    }).mouseleave(function () {
        run = setInterval(rotate, speed);
    });

I broke this in to its own function because it gets called in a number of different places.

    function resetSlides() {
        //and adjust the container so current is in the frame
        container.css({
            'left': -1 * item_width
        });
    }

});
//a simple function to click next link
//a timer will call this function, and the rotation will begin :) 

And here's your rotation timer.

function rotate() {
    $('#next').click();
}

Solution 2:

It took me a little bit, but I think I figured out a few things.

http://jsfiddle.net/qLca2fz4/28/

First off, your console was throwing a few errors: first, that rotate wasn't defined and that an arrow gif didn't exist. Arrow gif was probably something you have stored locally, but I changed the 'rotate' error by changing the strings in the code here to your actual variables.

So, from:

 run = setInterval('rotate()', speed);   

We get:

 run = setInterval(rotate, speed);   

(No () based on the examples here: http://www.w3schools.com/jsref/met_win_setinterval.asp)

But I think a more important question is why your text wasn't showing up at all. It's because of the logic found here:

$('#slides ul').css({'left' : left_value});

You even say that this is setting the default placement for the code. But it isn't..."left_vaule" is the amount that you've calculated to push left during a slide. So if you inspect the element, you can see how the whole UL is basically shifted one slide's worth too far left, unable to be seen. So we get rid of 'left_value', and replace it with 0.

$('#slides ul').css({'left' : 0});

Now, there's nothing really handling how the pictures slide in, so that part's still rough, but this should be enough to start on.

Let me know if I misunderstood anything, or if you have any questions.


Solution 3:

So, a few things:

1) I believe you are trying to get all of the lis to be side-by-side, not arranged up and down. There are a few ways to do this. I'd just make the ul have a width of 300%, and then make the lis each take up a third of that:

#slides ul {
    ....
    width: 300%;
}

#slides li {
    width: calc(100% / 3);
    height:250px;    
    float:left;
}

2) You got this right, but JSFiddle automatically wraps all your JS inside a $(document).ready() handler, and your function, rotate needs to be outside, in the normal DOM. Just change that JSFiddle setting from 'onload' to 'no wrap - in head'

3) Grabbing the CSS value of an element doesn't always work, especially when you're dealing with animating elements. You already know the width of the li elements with your item_width variable. I'd just use that and change your code:

var left_indent = parseInt($('#slides ul').css('left')) - item_width;

$('#slides ul').animate({'left' : left_indent}, 1500, function () {

to:

$('#slides ul').stop().animate({'left' : -item_width * 2}, 1500, function () {

4) Throw in the .stop() as seen in the above line. This prevents your animations from overlapping. An alternative, and perhaps cleaner way to do this, would be to simply return false at the beginning of your 'next' and 'prev' functions if #slides ul is being animated, like so:

 if ($('#slides ul').is(':animated')) return false;

And I think that's everything. Here's the JSFiddle. Cheers!

EDIT:

Oh, and you may also want to clearInterval at the beginning of the next and prev functions and then reset it in the animation callback functions:

$('#prev').click(function() {
    if ($('#slides ul').is(':animated')) return false;
    clearInterval(run);

    $('#slides ul').stop().animate({'left' : 0}, 1500,function(){
        ....
        run = setInterval('rotate()', speed);
    });
});

Post a Comment for "Using A Jquery Slider For Text Instead Of Images?"