[英]How to animate a position of a fixed element with jQuery
I have a div whose position properties is: 我有一个div,其位置属性为:
.some
{
position: fixed;
top: 0px;
}
I then want to animate its bottom
(not with top
, with bottom
property) 然后,我想为其bottom
设置动画(不与top
,具有bottom
属性一起使用)
$(document).ready(function(){
$("a").on("click", function(e){
e.preventDefault();
$("div").animate({ top: "none", bottom : 25});
});
});
But it does not work. 但这行不通。 The problem is that top
property is in the priority. 问题在于, top
属性处于优先地位。 If I set the top to 0 then it sticks to the top, it does not care any to bottom value. 如果我将top设置为0,则它会停留在top上,它不会在乎底部值。 However I remove the top property and animate bottom, it starts the animation right from the very bottom. 但是,我删除了top属性并为底部动画,它从最底部开始动画。 I want the animation to start from the position which is designated by top value and end in where it is specified by bottom value. 我希望动画从最高值指定的位置开始,并在最低值指定的位置结束。 What should I do? 我该怎么办?
Here is the JSFiddle: 这是JSFiddle:
http://jsfiddle.net/mostafatalebi2/ex1b69g9/ http://jsfiddle.net/mostafatalebi2/ex1b69g9/
You should animate from top: 0
to top: 100%
using a negative margin-top
value to maintain the div
at a certain distance from the bottom of the page. 您应该从top: 0
动画top: 0
到top: 100%
使用负的margin-top
值进行top: 100%
动画,以使div
与页面底部保持一定距离。 Doing so, your div
will move from the very top to the bottom of the page , like you want. 这样做, 您的div
可以根据需要从页面的顶部移至底部 。
To move your div exactly 25 pixels
distant from the bottom you can set margin-top
to the negative value of your div's height minus 25px
. 要使div距底部精确25 pixels
,可以将margin-top
设置为div高度的负值减去25px
。
Here's an example: 这是一个例子:
$(document).ready(function() { $("a").on("click", function(e) { e.preventDefault(); var $div = $('div.some'); $div.animate({ top: '100%', marginTop: - $div.height() - 25 }); }); });
.some { position: fixed; top: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class='some'>I am a DIV!</div> <br/> <br/> <a href='#'>Click Here!</a>
$(document).ready(function(){
$("a").on("click", function(e){
e.preventDefault();
$("div").animate({ top:'500px'});
});
});
Fiddle: http://jsfiddle.net/gcsx1exj/1/ 小提琴: http : //jsfiddle.net/gcsx1exj/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.