简体   繁体   English

简单的三元操作无效

[英]Simple ternary operation is not working

I am attempting to make a div change position on click and then revert to it's original position when clicked again. 我试图在点击时进行div更改位置,然后再次单击时恢复到原始位置。 I cannot for the life of me figure out what the problem is. 我不能为我的生活找出问题所在。

$("#bottom").click(function() {

  var $about = $('#bottom'),
  top = $about.css('top') === '50%' ? '90%' : '50%';

$about.stop().animate({top: top}, 500);  
});

my css is: 我的css是:

#bottom {
top: 90%;
left: 0; right: 0;
height: 100%;
}

When I click #bottom, the div positions itself at 50%, but when I hit it again, nothing happens. 当我点击#bottom时,div将自己定位在50%,但当我再次点击它时,没有任何反应。

.css('top') doesn't give you the literal CSS top declaration; .css('top')没有给你文字CSS top声明; it gives you the calculated pixel amount of the top. 它为您提供顶部的计算像素数量 If your container for #bottom is 100px tall, then $('#bottom').css('top') will give you 90px , not 90% . 如果#bottom的容器高100px,那么$('#bottom').css('top')会给你90px而不是 90%

Here's a fiddle with a solution for you. 这是一个为您提供解决方案的小提琴。

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

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