[英]Change or animate height to css default in jquery
I have a responsive website with different height setting for a header bar. 我有一个响应式网站,标题栏的高度设置不同。 In my javascript at some point, I change this value to a fixed size :
在我的javascript中,我将此值更改为固定大小:
$('#header').animate({marginTop: 0, height:80});
But at another point in my code, I would like this to revert to the original state. 但是在代码的另一点,我希望这能恢复到原始状态。 How can I animate it back to the default behavior?
如何将其重新设置为默认行为? In other words, animating the remove inline css...
换句话说,为删除内联CSS设置动画效果...
I tried 我试过了
$('#header').animate({marginTop: 20, height:'inherit'});
but this doesn't work. 但这不起作用。
The original css is this : 原始的CSS是这样的:
#header { margin-top:20px; width:100%; height: 80px; background-color: #000; }
@media only screen and (min-width: 768px) and (max-width: 960px)
{
#header{height:100px;}
}
@media only screen and (max-width: 767px)
{
#header{height:auto; padding-bottom: 1px;}
}
storing the height value: 存储高度值:
This cannot be done because a lot of users are resizing their window between the moment where the value would be stored and the moment of restoring the stored value.. 之所以无法做到这一点,是因为许多用户正在调整其窗口的大小,在此期间该值将被存储到恢复存储的值。
You could use like this: 您可以这样使用:
var original = $('#header').css('height');//gets original height
$('#header').animate({marginTop: 0, height:80});
Then revert use this: 然后还原使用此:
$('#header').animate({marginTop: 0, height:original});
Considering you have HTML like below 考虑到您有如下所示的HTML
<div class="item">
<div class="block"></div>
<div class="info">
My info
</div>
</div>
You can do something like this: 您可以执行以下操作:
jQuery(document).ready(function($) {
$(".item").mouseleave(function(){
$(this).find('.info').stop().css('marginTop', '0');
}).mouseenter(function(){
$(this).find('.info').animate({ marginTop: '-50px', opacity: 0.5 }, 1000);
});
});
You'll have to store the original value somewhere. 您必须将原始值存储在某个地方。 I recommend this -
我推荐这个-
$('#header').data('height', $(this).height()).animate({marginTop: 0, height:80});
Then when you are ready to revert - 然后,当您准备还原时-
var oldHeight = $('#header').data('height');
$('#header').animate({marginTop: 20, height: oldHeight });
EDIT -- 编辑 -
Do you have any other inline styles for the div? div还有其他内联样式吗?
If not, you can simply use removeAttr
and remove the style
property. 如果没有,您可以简单地使用
removeAttr
并删除style
属性。
尝试不使用height值,或将空的height值留在此处:''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.