Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI Slide animation triggering the scrollbar to display during the animation

I'm trying to use JQuery UI's slide animation to toggle a div.
When a link is clicked, the div slide in the page from the right (as seen in this fiddle).

$("#toggle").click(function(event) {
  event.stopPropagation();
  $("#slider").toggle("slide", {
    direction: "right"
  }, 300);
});
#toggle {
  font-weight: bolder;
  cursor: pointer;
}
#slider {
  z-index: 100;
  display: none;
  position: absolute;
  right: 0;
  top: 50px;
  width: 300px;
  height: 200px;
  padding: 20px;
  background: #DDD;
  border: 1px solid #000;
}
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous">
  </script>
</head>

<body>
  <div id="toggle">
    click me to slide
  </div>
  <div id="slider">
    Slidin' in & Slidin' out
  </div>
</body>

</html>

The problem I have is this scrollbar that appears during the animation, I don't want it to be displayed, like in JQuery UI's website, they don't have this scrollbar issue and I don't know how they did it.

I would like to avoid wrapping #slider into another container if possible, and I can't set his parent to overflow: hidden neither at the moment.

Is there a simple way to fix this ?

like image 850
QiMath Avatar asked Jan 20 '26 18:01

QiMath


1 Answers

The easiest solution is to set overflow: hidden on the body element. However, since you said that you can't do that, an alternative solution would be to animate the width of the element in order to make it appear like it is sliding in. In reality the width of the element is just increasing/decreasing from 0 and it is animated to make it look like it is sliding.

In jQuery you would do this with the .animate() method and you would set the value of width property to 'toggle'.

$("#toggle").click(function(event){
  event.stopPropagation();

  $("#slider").animate({
    width: 'toggle'
  }, 200);
});

In addition, you can also prevent the text from wrapping with white-space: nowrap.

See the full example below:

$("#toggle").click(function(event){
  event.stopPropagation();
  
  $("#slider").animate({
    width: 'toggle'
  }, 200);
});
#toggle {
  font-weight: bolder;
  cursor: pointer;
}

#slider {
  white-space: nowrap;
  z-index: 100;
  position: absolute;
  right: 0;
  top: 50px;
  width: 300px;
  height: 200px;
  padding: 20px;
  background: #DDD;
  border: 1px solid #000;
}
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
  <div id="toggle">
    click me to slide
  </div>
  <div id="slider">
    Slidin' in & Slidin' out
  </div>
</body>
</html>
like image 81
Josh Crozier Avatar answered Jan 22 '26 11:01

Josh Crozier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!