javascript - 试图改变css onclick中的不透明度
问题描述
尝试使 onclick 功能使不透明度从 0 变为 1,以便 .dropdown-content 在全高之前不显示。
$(".fa-bars").click(function() {
$(".fa-times").toggle();
$("header").css("height", "100%");
$("header").css("transition", "height 1s ease-in");
$("header").css("grid-template-rows", "50px 400px");
$("header").css("grid-row-gap", "20px");
$("header").css("grid-template-areas", "'dp logo start' 'dc dc dc'");
$(".dropdown-content").css("display", "grid");
$(".fa-bars").css("display", "none");
event.preventDefault();
});
解决方案
一种方法是利用transition-delay
CSS 属性和CSS 特性.dropdown-content
在菜单打开后触发菜单项的可见性(内部)。
因为我们知道菜单的打开动画需要一秒钟才能完成(即height 1s ease-in
),所以我们可以将菜单项的可见性延迟一秒钟以达到预期的效果。
这里的关键思想是引入一个 CSS“修饰符类”(即),它为and.open
增加了更大的特异性。这个修饰符:header
.dropdown-content
- 指定标题处于“打开”状态时的样式,并且
.dropdown-content
当标头处于打开状态时,指定后代的不透明/可见样式
在 SCSS 中,可以写成:
header {
transition : height 1s ease-in;
/* Define the dropdown-content transition styles on opacity, where
the opacity delay causes opacity of menu items to change after menu
animation (of 1sec) is complete */
.dropdown-content {
opacity:0;
transition-property: opacity;
transition-duration: 0.1s;
transition-delay: 1s;
display:block;
}
/* CSS modifier class for "header.open" causes height to change
when open class applied */
&.open {
height:100%;
grid-template-rows: 50px 400px;
grid-row-gap:20px;
grid-template-areas:'dp logo start' 'dc dc dc';
}
/* When open modifier class applied to parent header, items in the
.dropdown-content child are set to be opaque/visible */
&.open .dropdown-content {
opacity:1.0;
}
}
使用上面的 SCSS,您可以将脚本简化为:
$(".fa-bars").click(function() {
/* Add open modifier class to apply new CSS defined above and
delay visiblity of dropdown-content content */
$("header").addClass("open");
$(".fa-times").toggle();
$(".fa-bars").css("display", "none");
event.preventDefault();
});
$(".fa-times").click(function() {
/* Remove open modifier class to hide menu and items */
$("header").removeClass("open");
$(".fa-bars").toggle();
$(".fa-times").css("display", "none");
event.preventDefault();
});
现在,里面的菜单项只有在动画完成.dropdown-content
后才可见。可以在此处找到header
一个工作示例- 希望对您有所帮助!
推荐阅读
- flutter - Flutter 中的强制渲染
- html - FLASK - HTML - 未找到请求的 URL
- r - if (S == 0) { : 需要 TRUE/FALSE 的缺失值}, raster::calc
- javascript - In JS Cannot read property 'style' of null, but when copying and pasting code it works fine
- c# - C# JWT 令牌授权总是返回错误
- linker-errors - IAR 错误 [Li005] : 即使文件已添加到项目中并且文件的路径在预处理器路径中,也没有函数定义
- javascript - 加载默认相机应用程序但不期望任何返回值或输入的按钮
- sql-server - Powerbuilder 6.5 在禁用 TLS1.0 后无法连接到 SQL Server 2008 R2
- php - 无法将键值对添加到 php 中的复杂对象
- winapi - 在 VSCode 中创建 DLL