javascript 无法使用 jQuery 更改 CSS 底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15103755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:30:34  来源:igfitidea点击:

Can't Change CSS Bottom with jQuery

javascriptjquery

提问by JacobTheDev

I'm having trouble getting the "else" bit of this working. Anyone know what the problem is?

我很难让这个工作的“其他”部分工作。有谁知道问题是什么?

var navOpen = false;
                if (navOpen == false) {
                    $("nav").click(function() {
                        $(this).css("bottom","0");
                        navOpen = true;
                    });
                } else {
                    $("nav").click(function() {
                        $(this).css("bottom","-84");
                        navOpen = false;
                    });
                }

回答by Moak

The condition is in the wrong place.

条件是在错误的地方。

 var navOpen = false;
 $("nav").click(function() {
      if (navOpen == false) {
           $(this).css("bottom","0");
           navOpen = true;
      } else {
           $(this).css("bottom","-84px");
           navOpen = false;
      }
 });

回答by undefined

You are binding several handlers to the same element, you can use cssmethod:

您将多个处理程序绑定到同一个元素,您可以使用css方法:

$("nav").click(function() {
   $(this).css("bottom", function(i, bottom) {
      return bottom === '0px' ? '-84px' : '0px';
     // return navOpen ? '-84px' : '0px';
   });
})

回答by Daniil Ryzhkov

You need to define metering (e.g px, %). CSS doesn't support just numering parameters like HTML attribute does.

您需要定义计量(例如 px、%)。CSS 不只支持像 HTML 属性那样的编号参数。

回答by martriay

Try with

试试

$(this).css("bottom","-84px");