Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Jquery slider without plugin

    Hello,

    I'm new to jQuery and tought to give myself a small challenge. I wanted to make my own jQuery slider (for div's or images) without any plugins.



    When the document is loaded it changes the first div class to 'current'.

    This is my div lay-out.
    Code:
    	
    <div id="up"></div>
            <div class="test" class="slide">1</div>
            <div class="test" class="slide">2</div>
            <div class="test" class="slide">3</div>
            <div class="test" class="slide">4</div>
            <div class="test" class="slide">5</div>
        <div id="down"></div>
    The following code is executed when I click on a div with id 'up':

    Code:
    if ($('.slide').is(':first')){
    			console.log("error");
    		} else {
    			$('.current').animate({height:0}, 2000, function() {});
    			$('.current').addClass('test');
    			$('.current').next('.test').animate({height:300}, 2000, function() {}).addClass('current').removeClass('.test');
    			$('.current:last').removeClass('current');
    		}
    	});
    And ofcourse I have similar code for a div with id 'down'.

    I think it should work but somehow it does not. When I click a div (up or down) it says, no matter what, 'error'.

    The console.log(" ") thing is for firebug.

    I hope somebody can show me what is wrong or how I could do the whole thing better. (if even possible)

    Thanks,

    Jasper

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,478
    Thanks
    6
    Thanked 981 Times in 954 Posts
    At first fix your HTML. If you want to assign more than one class to an element, put space separated values in the attribute:
    Code:
    <div class="test slide">
    Also in jQuery you can chain functions. This also increases performance because the DOM doesn’t have to be iterated several times for the same selector.
    Code:
    $('.current').animate({height:0}, 2000, function() {}).addClass('test').next('.test').animate({height:300}, 2000, function() {}).addClass('current').removeClass('.test');
    Last edited by VIPStephan; 12-16-2011 at 11:33 PM.

  • Users who have thanked VIPStephan for this post:

    TheAcidtouch (12-17-2011)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •