Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate the width of a div slowly with jquery

I have navigation nested in a div that is offscreen to the left and when the user scrolls down the page and reaches pixel 296, the left navigation slowly appears by it's width growing towards the right.

What I have now is half working. The navigation nested in the div appears when the user scrolls down the page but I want it to animate slowly to the right and that is not happening. Not sure what I am doing wrong. The specific line I am having problems with is:

$("#slidebottom").animate({ width: "100" }, 'slow');

But here is my entire code:

$(window).scroll(function(){

  var wintop = $(window).scrollTop(), docheight = $(document).height(), 
  winheight =    $(window).height();

  var bottomHeight = $("#slidebottom").height();
  var zeroheight = 0;

  if  (wintop > 296) {
    bottomHeight = $('#slidebottom').height(docheight);
    $("#slidebottom").animate({ width: "100" }, 'slow');

  }

  if( wintop < 296)
  {
    bottomHeight = $('#slidebottom').height(zeroheight);    
    //$("#slidebottom").animate({ width: "0" }, 'slow');
  }
});
like image 484
Ramona Soderlind Avatar asked Aug 12 '13 23:08

Ramona Soderlind


People also ask

Is jQuery slower for animations?

jQuery can be several times slower than CSS when talking about animations. CSS animations and transitions have the advantage of being hardware accelerated by the GPU, which is really good in moving pixels.

What jQuery effect alters the width of an element?

The jQuery outerWidth() and outerHeight() methods get or set the outer width and the outer height of the element respectively. This outer width and height includes padding and border but excludes the margin on the element.

How do I set the width of an element in jQuery?

jQuery width() Method The width() method sets or returns the width of the selected elements. When this method is used to return width, it returns the width of the FIRST matched element. When this method is used to set width, it sets the width of ALL matched elements.

Which jQuery method can be used to gradually change the height of the selected element?

jQuery height() Method The height() method sets or returns the height of the selected elements.


2 Answers

The jQuery docs show ints, not strings, as the arguments to width:

$("#slidebottom").animate({ width: 100 }, 'slow');

Edit: So I was wrong, that's not the problem; it handles strings just as well.

like image 128
prekolna Avatar answered Sep 30 '22 12:09

prekolna


Try the following maybe?

$("#slidebottom").animate({ width: '100px' }, 'slow');

I have a feeling that the unit is important for this, since 100 can mean anything. Not very specific. And you can define this as a string just fine. In fact, in the example they give for .animate it is defined as a string.

like image 31
Solomon Closson Avatar answered Sep 30 '22 10:09

Solomon Closson