112

I extended the jQuery effects called slideRightShow() and slideLeftHide() with a couple functions that work similarly to slideUp() and slideDown() as seen below. However, I would also like to implement slideLeftShow() and slideRightHide().

I know there are substantial libraries that offer this type of thing (I'd like to avoid adding another large set of javascript files), but can anyone provide a simple example of how to implement either slideLeftShow() or slideRightHide()?

jQuery.fn.extend({
  slideRightShow: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'show'});
    });
  },
  slideLeftHide: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'hide'});
    });
  },
  slideRightHide: function() {
    return this.each(function() {
      ???
    });
  },
  slideLeftShow: function() {
    return this.each(function() {
      ???
    });
  }
});

The above slideRightShow function starts showing the image from the left side and it progresses toward the right side. I am looking for some way to do the same thing but start from the right side and progress toward the left. Thanks!

EDIT

jQuery Interface has something like I need (I basically need their "slide in right" and "slide out left" functions), but I couldn't get this to work with jQuery 1.3: http://interface.eyecon.ro/demos/ifx.html . Also, their demo seems to broken as well as it will only do a slide once before throwing a million errors.

Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
Wickethewok
  • 6,534
  • 11
  • 42
  • 40

4 Answers4

186

This feature is included as part of jquery ui http://docs.jquery.com/UI/Effects/Slide if you want to extend it with your own names you can use this.

jQuery.fn.extend({
  slideRightShow: function() {
    return this.each(function() {
        $(this).show('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'left'}, 1000);
    });
  },
  slideRightHide: function() {
    return this.each(function() {
      $(this).hide('slide', {direction: 'right'}, 1000);
    });
  },
  slideLeftShow: function() {
    return this.each(function() {
      $(this).show('slide', {direction: 'left'}, 1000);
    });
  }
});

you will need the following references

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.core.js"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.slide.js"></script>
Urbycoz
  • 7,247
  • 20
  • 70
  • 108
bendewey
  • 39,709
  • 13
  • 100
  • 125
  • Hi! I'm looking for the inverse of what you have implemented there. Basically, when I use what I have above to show an element, the left part appears first and progresses towards the right. I am trying to get it to start from the right side and progress towards the left. – Wickethewok Feb 06 '09 at 18:30
  • 1
    it works if you float the element right. Otherwise. you might want to annimate the left property and move the element as you shrink it. – bendewey Feb 06 '09 at 18:56
  • 1
    Changing the element to float "right" didn't reverse the slide for me. I clarified above if that helps. – Wickethewok Feb 06 '09 at 21:14
  • 3
    Thank you so much! I didn't know this was part of the jQuery's effects. I would give +2 if I could! – Wickethewok Feb 07 '09 at 04:15
  • 4
    Link of the source of the two ui scripts are now: http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.core.js and "http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.slide.js – Muleskinner Jan 07 '11 at 21:25
  • Any idea how you would add callbacks to these solutions (and while you're at it, why not speed too? I am guessing you can't without swapping to use animate() instead. – Steve Grove Jan 31 '13 at 00:10
  • And how to do it in case you use jQuery >= 2.0 ? – edteke May 29 '14 at 20:58
34

Don't forget the padding and margins...

jQuery.fn.slideLeftHide = function(speed, callback) { 
  this.animate({ 
    width: "hide", 
    paddingLeft: "hide", 
    paddingRight: "hide", 
    marginLeft: "hide", 
    marginRight: "hide" 
  }, speed, callback);
}

jQuery.fn.slideLeftShow = function(speed, callback) { 
  this.animate({ 
    width: "show", 
    paddingLeft: "show", 
    paddingRight: "show", 
    marginLeft: "show", 
    marginRight: "show" 
  }, speed, callback);
}

With the speed/callback arguments added, it's a complete drop-in replacement for slideUp() and slideDown().

vdboor
  • 21,914
  • 12
  • 83
  • 96
  • How can make them to do the same effect with right sliding? – Jayant Varshney Mar 12 '14 at 11:37
  • @JayantVarshney: make sure that the block is right-aligned, possibly with some inner block. This code only shrinks the width. If your CSS can handle that, you'll have a right slide – vdboor Mar 12 '14 at 15:44
  • Thanks... But I want both the effects on same div.. like opening from right to left and then closing from left to right or vice versa... – Jayant Varshney Mar 14 '14 at 18:51
  • Well then, how about switching classes at the completion callback? :-) – vdboor Mar 25 '14 at 16:07
  • Thanks, I used CSS3 animations to achieve this functionality – Jayant Varshney Apr 14 '14 at 16:07
  • Great answer. To make it toggle as well, use this: `jQuery.fn.slideLeftToggle = function(speed, callback){ if(this.is(":visible")){ this.slideLeftHide(speed, callback); } else { this.slideLeftShow(speed, callback); } }` – Tim Malone Apr 22 '16 at 06:18
10

You can add new function to your jQuery library by adding these line on your own script file and you can easily use fadeSlideRight() and fadeSlideLeft().

Note: you can change width of animation as you like instance of 750px.

$.fn.fadeSlideRight = function(speed,fn) {
    return $(this).animate({
        'opacity' : 1,
        'width' : '750px'
    },speed || 400, function() {
        $.isFunction(fn) && fn.call(this);
    });
}

$.fn.fadeSlideLeft = function(speed,fn) {
    return $(this).animate({
        'opacity' : 0,
        'width' : '0px'
    },speed || 400,function() {
        $.isFunction(fn) && fn.call(this);
    });
}
Druid
  • 6,423
  • 4
  • 41
  • 56
thebhatta
  • 131
  • 1
  • 7
  • 1
    This is exactly what I needed without including jQuery UI. Just added the opacity and width as parameters. `... = function(opacity, speed, width, fn) {...}` – Dylan Valade Feb 23 '14 at 16:06
5

And if you want to vary the speed and include callbacks simply add them like this :

        jQuery.fn.extend({
            slideRightShow: function(speed,callback) {
                return this.each(function() {
                    $(this).show('slide', {direction: 'right'}, speed, callback);
                });
            },
            slideLeftHide: function(speed,callback) {
                return this.each(function() {
                    $(this).hide('slide', {direction: 'left'}, speed, callback);
                });
            },
            slideRightHide: function(speed,callback) {
                return this.each(function() {  
                    $(this).hide('slide', {direction: 'right'}, speed, callback);
                });
            },
            slideLeftShow: function(speed,callback) {
                return this.each(function() {
                    $(this).show('slide', {direction: 'left'}, speed, callback);
                });
            }
        });
Steve Grove
  • 915
  • 10
  • 18