jquery - CSS过渡高度属性不起作用
问题描述
我正在尝试平滑地转换我的#superheader 元素的高度,但是出了点问题。我的登台站点在这里。我能够使用下面的代码片段使其正常工作,但似乎有些东西在翻译中丢失了。有任何想法吗?
jQuery(document).ready(function($){
// hiding superheader after scrolling a little ways
$( window ).scroll(function() {
var cutoff = 70;
if ($(window).scrollTop()>200) {
$("#superheader").addClass('hide');
}
if ($(window).scrollTop()<200) {
$("#superheader").removeClass('hide');
}
})
})
#superheader {
background-color: red;
z-index: 700;
box-shadow: none;
padding: 20px 25px;
width:100%;
display:flex;
transition: all .4s;
height:80px;
position:fixed;
top:0;
}
#superheader.hide {
height:20px;
padding:0;
overflow: hidden;
}
#page {
height:1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='superheader'></div>
<div id='page'></div>
解决方案
正如@showdev 评论,问题是因为hide
您的主题中添加了样式display:none
,并且它破坏了动画。你所能做的就是:
- 更改班级(例如从
.hide
to.hidden
)
jQuery(document).ready(function($){
// hiding superheader after scrolling a little ways
$( window ).scroll(function() {
var cutoff = 70;
if ($(window).scrollTop()>200) {
$("#superheader").addClass('hidden');
}
if ($(window).scrollTop()<200) {
$("#superheader").removeClass('hidden');
}
})
})
#superheader {
background-color: red;
z-index: 700;
box-shadow: none;
padding: 20px 25px;
width:100%;
display:flex;
transition: all .4s;
height:80px;
position:fixed;
top:0;
}
#superheader.hidden {
height:20px;
padding:0;
overflow: hidden;
}
#page {
height:1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='superheader'></div>
<div id='page'></div>
- 或使用相同的类,但只是
!important
在显示上添加,就像这样
jQuery(document).ready(function($){
// hiding superheader after scrolling a little ways
$( window ).scroll(function() {
var cutoff = 70;
if ($(window).scrollTop()>200) {
$("#superheader").addClass('hide');
}
if ($(window).scrollTop()<200) {
$("#superheader").removeClass('hide');
}
})
})
#superheader {
background-color: red;
z-index: 700;
box-shadow: none;
padding: 20px 25px;
width:100%;
display:flex !important; /* add this */
transition: all .4s;
height:80px;
position:fixed;
top:0;
}
#superheader.hide {
height:20px;
padding:0;
overflow: hidden;
}
#page {
height:1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='superheader'></div>
<div id='page'></div>
推荐阅读
- java - Java8 .getMethod() 与 ::getMethod
- wagtail - 如何让用户选择在页面中选择图像的鹡鸰集合?
- java - 在 Kotlin/Java 中寻找 .properties 和 .y(a)ml 文件的属性解析器,就像在 Spring 中一样
- xml - Apache骆驼mybatis映射器xml
- javascript - Highcharts Polar,编辑鼠标悬停标签
- python - 从 requirements.txt 安装一个新包,而不升级已经满足的依赖项
- apache-spark - 如何将具有嵌套元素的列从其他列添加到数据框(withColumn)
- wordpress - Wordpress:限制特定用户管理特定类别的帖子
- c - 不可靠的串口
- javascript - 在设置按钮的文本和颜色时使用地图功能的问题