简体   繁体   English

CSS3仅在悬停时过渡

[英]CSS3 only transition while hovered

I have an element with transition: margin-left 0.7s; 我有一个transition: margin-left 0.7s;元素transition: margin-left 0.7s; on it. 在上面。

The transition works fine, but it also animates on zoom. 过渡效果很好,但也可以设置缩放动画。 Is there any way to stop this? 有什么办法可以阻止这种情况?

Examples using width instead of margin-left : 使用width而不是margin-left示例:

JSFiddle: https://jsfiddle.net/7py46d3L/ JSFiddle: https ://jsfiddle.net/7py46d3L/

HTML: HTML:

<div></div>

CSS: CSS:

div {
    transition: width 0.7s;
    width: 5vw;
    height: 10px;
}
div:hover {
    width: 10vw
}

Remove the width:5vw; 去除width:5vw; and make width as in PX or in % 并将宽度设为PX%

HTML: HTML:

<div></div>

CSS: CSS:

div {
    transition: width 0.7s;
    width: 50px;
    height: 10px;
}
div:hover {
    width: 10vw
}

Jsfiddle 的jsfiddle

Understanding sizing in CSS 了解CSS的大小


There are two types of measurement units: 有两种类型的度量单位:

  1. relative lengths: Relative length units specify a length relative to another length. 相对长度:相对长度单位指定相对于另一长度的长度。 The relative units are: em, ex, ch, rem, vw, vh, vmin, vmax . 相对单位是: em,ex,ch,rem,vw,vh,vmin,vmax They are mainly useful when the output media has dynamic size like responsive layout for mobile device, laptop. 当输出媒体具有动态大小(如移动设备,笔记本电脑的响应式布局)时,它们主要有用。
  2. absolute lengths: these are fixed in relation to each other and anchored to some physical measurement. 绝对长度:这是相对固定的,并固定在某种物理尺寸上。 The absolute units are: px, mm, cm, in, pt, pc .They are mainly useful when the output environment is known. 绝对单位是: px,mm,cm,in,pt,pc 。当已知输出环境时,它们主要有用。

so in your example if you give width:5[any absolute lengths or in]then > it will work 因此,在您的示例中,如果您提供width:5 [任何绝对长度或in],那么它将起作用

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

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