简体   繁体   English

通过使用最小宽度和最大宽度一起应用过渡

[英]Apply transition by using min-width and max-width together

I have a nav bar for which I'm trying to make the toggle button work, my toggle button is a checkbox, so following is the css to make the navbar ( #navbar-left ) appear when you click on the checkbox 我有一个导航栏,我试图让切换按钮工作,我的切换按钮是一个复选框,所以下面是css,当你点击复选框时出现导航栏( #navbar-left

#navbar-left{
 max-width: 0;
 min-width: 0;
 width: 0;
 transition: max-width 0.2s ease;
 transition: min-width 0.2s ease;
}

.nav-trigger:checked ~ #navbar-left {
  max-width: 200%;
  min-width: 20%;
  width: auto;
  float: left;
}

where .nav-trigger is the check button, I have been able to either close the navbar smoothly using transition or open the navbar smoothly using transition by applying min-width or max-width at a time, but how can I use them both to open and close the navbar smoothly using the transitions. 其中.nav-trigger是检查按钮,我可以使用转换平滑地关闭导航栏,或者通过一次应用最小宽度或最大宽度来平滑地打开导航栏,但是如何将它们用于使用过渡平滑地打开和关闭导航栏。

I cannot simple apply the transitions using width property because I've to always set the width property to auto. 我不能简单地使用width属性应用转换,因为我总是将width属性设置为auto。

What would be the best solution or any alternate way to achieve this? 什么是最好的解决方案或任何替代方法来实现这一目标?

should be written within one single rule : 应该写在一个单一的规则中:

https://developer.mozilla.org/en-US/docs/Web/CSS/transition https://developer.mozilla.org/en-US/docs/Web/CSS/transition

The transition CSS property is a shorthand property for transition-property, transition-duration, transition-timing-function, and transition-delay. 转换CSS属性是转换属性,转换持续时间,转换时序功能和转换延迟的简写属性。 It enables you to define the transition between two states of an element. 它使您能够定义元素的两个状态之间的转换。 Different states may be defined using pseudo-classes like :hover or :active or dynamically set using JavaScript. 可以使用伪类来定义不同的状态,例如:hover或:active或使用JavaScript动态设置。

Syntaxe Syntaxe

/* Apply to 1 property */ / *适用于1个房产* /

/* property name | / *属性名称| duration */ 持续时间* /

transition: margin-left 4s; 过渡:保证金左4s;

/* property name | / *属性名称| duration | 持续时间| delay */ 延迟* /

transition: margin-left 4s 1s; 过渡:保证金 - 左4s 1s;

/* property name | / *属性名称| duration | 持续时间| timing function | 定时功能| delay */ 延迟* /

transition: margin-left 4s ease-in-out 1s; 转换:margin-left 4s轻松进出1s;

/* Apply to 2 properties */ / *适用于2个属性* /

transition: margin-left 4s, color 1s; 过渡:边缘左边4s,颜色1s;

/* Apply to all changed properties */ / *适用于所有已更改的属性* /

transition: all 0.5s ease-out; 过渡:全部0.5秒缓解;

/* Global values */ / *全球价值* /

transition: inherit; 过渡:继承;

transition: initial; 过渡:初始;

transition: unset; 过渡:未设置;

#navbar-left{
 max-width: 0;
 min-width: 0;
 width: 0;
 transition: max-width 0.2s ease,min-width 0.2s ease;
}

.nav-trigger:checked ~ #navbar-left {
  max-width: 200%;
  min-width: 20%;
  width: auto;
  float: left;
}

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

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