jquery sliding side bar Left to right
Asked Answered
E

1

6

I am trying to create a sliding side bar with effects similar to

  1. www.wookmark.com
  2. http://www.dynamicdrive.com/dynamicindex1/slideinmenu.htm.

This is how far I wrote the code.But this is jerky.

  1. Can anyone propose a better solution with aniamtions/easing/toggle etc
  2. I want code to be independent of left parameter i.e. $("#slide").css("left", "-150px"); It should be able to slide-in/slide-out with all sorts of div width

Any ideas ?

CSS

#slide{
border:1.5px solid black;
position:absolute;
top:0;
left:0;
width:150px;
height:100%;
background-color:#F2F2F2;
layer-background-color:#F2F2F2;
}

HTML

<div id="slide" style="left: -150px; top: 0px; width: 160px;">
    <p>Something here</p>
</div>

Jquery

<script>
    jQuery(document).ready(function() {
        $('#slide').mouseover(function() {
            $("#slide").css("left", "0px");
        });

        $('#slide').mouseout(function() {
            $("#slide").css("left", "-150px");
        });

    });
 </script>
Erminois answered 1/4, 2013 at 14:24 Comment(0)
T
11

You need animate() method -

var width = $('#slide').width() - 10;
$('#slide').hover(function () {
     $(this).stop().animate({left:"0px"},500);     
   },function () {          
     $(this).stop().animate({left: - width  },500);     
});

Here I've added .stop() before. This will clear animation queue that is built up because of moving mouse quickly.

DEMO

Theresatherese answered 1/4, 2013 at 14:33 Comment(1)
Thank you @phobos.It works as i wanted. But if i hover mouse quickly many times on slider div, it goes crazy. Anyway we can fix that ?Erminois

© 2022 - 2024 — McMap. All rights reserved.