Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: jQuery: Slide Left to Right

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question jQuery: Slide Left to Right

    Hi,

    I know how to Slide a DIV from top to bottom and vice-versa. The following code shows how to do it:

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script>
    $().ready(function(){
    				   $("#clicky").click(function(){
    					$("#slide").slideToggle("slow");						   
    											   });
    				   
    				   });
    </script>
    
    <input name="" type="button" value="Click me" id="clicky"/>
    <div id="slide">
    <h1>Hello World!</h1>
    </div>

    Question: Is there anyway I can slide a DIV from left to right and vice-versa?

    Please help.


    Thanx in advance

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    You can use animate for that:
    http://docs.jquery.com/Effects/animate

    Good luck!

  3. #3
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    But how do I set the direction?

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Just a tip, I've never seen $().ready(function(){, and don't think many people use it.

    One of the following is better (in my opinion):

    Shorty:
    Code:
    $(function(){
    
    
    });
    Tally:
    Code:
    $(document).ready(function(){
    
    });
    Jeremy | jfein.net

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function(){
     var s = $('#slide'), w = $(s.find('span')[0]).width();
     s.css({width: w, overflow: 'hidden', whiteSpace: 'nowrap'});
     $('#clicky').toggle(function(){
      s.stop(true).animate({width: 0}, {duration: 'slow', queue: false, complete: function(){s.hide();}});
     }, function(){
      s.stop().animate({width: w}, {duration: 'slow', queue: false});
     });
    });
    </script>
    
    </head>
    <body>
    <input name="" type="button" value="Click me" id="clicky">
    <div id="slide">
    <h1><span>Hello World!</span></h1>
    </div>
    
    </body>
    </html>
    Last edited by jscheuer1; 02-03-2009 at 11:01 AM. Reason: syntax (duration, not speed), validate html
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    That works fine.


    Thanx

  7. #7
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Sorry to bump such old topic; but is it possible to integrate this with jQuery cookies? So it could be toggled with cookies. Thanks!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Anything that can be done with javascript/jQuery can be setup to respond to a cookie. The jQuery library itself has no cookies though that I'm aware of, but there are probably one or more units available for cookies written in jQuery. However, for most cookie operations, this:

    http://www.dynamicdrive.com/forums/blog.php?b=32

    is all you will need.

    Without knowing exactly what you want the cookie to keep track of or how exactly you want the code to respond to whatever values you set in the cookie, I cannot be more specific.

    One caveat with cookies though, they can be turned off and/or cleared in the user's browser, so nothing too important should be setup to depend upon them.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Ah, I apologize for not mentioning what I wanted. What I meant was to keep the collapsed/expanded state with cookies.

    For example, if you collapsed the things to the left, it would stay collapsed even if another page were loaded. And if it is expanded to the right, it would stay expanded.

    Perhaps it is caused by my lack of knowledge in jQuery, but apparently I do not know what to do with the blog post you have given to integrate that with this... so, I will be grateful for any help offered. Thanks!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Something like so:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    document.write('<style type="text/css">.slideHide {visibility: hidden;}<\/style>');
    jQuery(function($){
     var s = $('#slide'), w = $(s.find('span')[0]).width(), cookie = {
       set: function(n, v, d){ // cookie.set takes (name, value, optional_persist_days) - defaults to session if no days specified
         if(d){var dt = new Date(); 
           dt.setDate(dt.getDate() + d);
           d = '; expires=' + dt.toGMTString();}
       document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
       },
       get: function(n){ // cookie.get takes (name)
       var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
       return c? unescape(c[2]) : null;
       },
       kill: function(n){ // cookie.kill takes (name)
       cookie.set(n, '', -1);
       }
     };
     s.css({width: w, overflow: 'hidden', whiteSpace: 'nowrap'});
     $('#slideContainer').css({minWidth: w, height: s.height()});
     $('#clicky').toggle(function(){
      s.stop(true).animate({width: 0}, {duration: 'slow', queue: false, complete: function(){s.hide();}});
      cookie.set('slider', 'in');
     }, function(){
      s.stop().animate({width: w}, {duration: 'slow', queue: false});
      cookie.kill('slider');
     });
     if(cookie.get('slider') === 'in'){
      s.css({width: 0, display: 'none'});
      $('#clicky').click();
     }
     s.removeClass('slideHide');
    });
    
    </script>
    
    </head>
    <body>
    <input name="" type="button" value="Click me" id="clicky">
    <div id="slideContainer">
    <div id="slide" class="slideHide">
    <h1><span>Hello World!</span></h1>
    </div>
    </div>
    
    </body>
    </html>
    Last edited by jscheuer1; 10-17-2009 at 10:51 AM. Reason: minor code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Xaliber (10-17-2009)

Bookmarks

Posting Permissions

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