简体   繁体   English

如何使用jquery缓慢地为div的宽度设置动画

[英]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. 我的导航嵌套在左侧屏幕的div中,当用户向下滚动页面并到达像素296时,左侧导航缓慢显示,其宽度朝向右侧增长。

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. 当用户向下滚动页面时,会出现嵌套在div中的导航,但我希望它能够向右慢慢动画并且不会发生。 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');
  }
});

The jQuery docs show ints, not strings, as the arguments to width: jQuery文档显示整数而不是字符串作为宽度的参数:

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

Edit: So I was wrong, that's not the problem; 编辑:所以我错了,那不是问题; it handles strings just as well. 它也处理字符串。

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. 我觉得单位对此非常重要,因为100可能意味着什么。 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. 实际上,在他们为.animate提供的示例中,它被定义为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM