Transform translate3d
Author: o | 2025-04-25
Unable to stop CSS3 transition of transform by translate3d. 1. Halting a translate3d transition. 3. Apply CSS3 Transition To Some Transforms (translate3d) but Not Others (rotate)
transform - CSS translate3d - Stack Overflow
The translate3d() function in CSS is used to translate, or move, an element in three-dimensional space. The result is a datatype.The translate3d() function results in the transformation, based on the three-dimensional vector [tx, ty, tz]. The coordinates value determine the direction in which the element will move.Possible valuesThe function translate3d() takes the following values as parameter(s).tx: Can be a or value that represents the abscissa (horizontal, x-component) of the translating vector [tx, ty, tz].ty: Can be a or value that represents the ordinate (vertical, y-component) of the translating vector [tx, ty, tz].tz: Can only be a value that represents z-component of the translating vector [tx, ty, tz]. It can not have a value and if given the property containing the transform will be invalid.Syntaxtransform: translate3d(tx, ty, tz);CSS translate3d() - Length ValueFollowing is an example of translate3d() function with the various ways in which length values can be passed to it, i.e., positive and negative values: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: translate3d(20px, 30px, 20px); background-color: yellowgreen; } .translate-3d-negative { transform: translate(-20px, -10px, -30px); background-color: tomato; } no translate() applied translate3d(20px, 30px, 20px) translate3d(-20px, -10px, -30px) CSS translate3d() - Combining x-axis and z-axis valuesFollowing is an example of translate3d() function with length values passed to x-axis and z-axis: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: translate3d(20px, 0, 20px); background-color: yellowgreen; } no translate3d() translate3d(20px, 0, 20px) no translate3d() CSS translate3d() - Combining y-axis and z-axis valuesFollowing is an example of translate3d() function with length values passed to y-axis and z-axis, along with perspective() value: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: perspective(580px) translate3d(0, 30px, 50px); background-color: yellowgreen; } no translate3d() translate3d(0, 30px, 50px) no translate3d()
Setting translate3d independent of other transform
Take values, NOT percentage values. This makes sense because there is nothing relative to which a percentage value provided to translateZ() can resolve.Let's consider some examples.In the following code, we have a element styled as a square.CSSdiv { width: 100px; height: 100px; background-color: blue;}Let's translate it forwards by 100px:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: translateZ(100px);}Surprisingly, when we run the code, there's absolutely no effect.And that's because one thing is missing from the rendering of the square so as to give the impression that it has moved closer to the viewer, i.e. perspective.So, let's incorporate perspective into our example, and see the change it brings:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: perspective(1000px) translateZ(100px);}Note that we're using the perspective() function directly on the , followed by the translateZ() function.Let's see the output:Yup, it works this time.Voila! There's our forwards-translated square, larger in size, thanks to the advent of perspective into the equation.Now, let's shift gears and instead translate the square away from the viewer, using a negative z-value.This time, as per expectation, the square should appear smaller:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: perspective(1000px) translateZ(-500px);}And it indeed does.Remember that if an element is given perspective via the perspective() transform function, the function must come before all other functions. Otherwise, we won't get any perspective effect.This we learnt in the previous chapter, and it's also illustrated below:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: translateZ(-150px) perspective(1000px);}This is because every function is processed in the very order that it appears in transform.The translate3d() functionImagine you want to work with translations in all three axes — x, y and z. One obvious way it to use the individual functions translateX(), translateY() and translateZ().A simple example follows:CSSdiv { width: 50px; height: 50px; background-color: blue; transform: perspective(1000px) translateX(100px) translateY(100px) translateZ(-500px);}However, just as translate() is a conciser way to combine the values of translateX() and translateY(), so do we have a function to combine translation in the three axes.It's the translate3d() function.Here's the syntax of translate3d():The three arguments respectively govern translation in the x, y and z axis.tx can beDetecting 'transform: translate3d' support - Stack Overflow
TIP 深入浅出 transform 属性,2D、3D 转换,项目中的应用场景和最佳实践 # 一、transform 属性 TIP 在 CSS 中,利用transform这个属性实现对元素进行旋转,缩放,倾斜或平移。 transform 属性的值可以是 none 或一个或多个 css 变换函数。 transform 的值不是 none 时,元素会创建自己的层叠上下文 # 1、transform 的 2D 变换函数 属性值 描述 实例 translate(x,y) x,y 为长度单位,可以是 px,也可以是百分比%,x 表示元素在 x 轴上的移动量y 表示元素在 y 轴上的移动量,如果 y 值不写,则默认为 0,例如 translate(10px)表示 translate(10px,0) transform:translate(100px,200px) translateX(x) x 为长度单位,可以是 px,也可以是百分比%表示元素在平面 x 轴上的位移量 transform:translateX(300px) translateY(y) y 为长度单位,可以是 px,也可以是百分比%表示元素在平面 Y 轴上的位移量 transform:translateY(200px) rotate(ax) ax 代表旋转的角度正角表示顺时针旋转负角表示逆时针旋转 transform:rotate(30deg) scaleX(x) x 数字类型,表示元素在 x 轴上的缩放比 transform:scaleX(2) scaleY(y) y 数字类型,表示元素在 y 轴上的缩放比 transform:scaleY(2) scale(x,y) x 数字类型,表示元素在 x 轴上的缩放比y 数字类型,表示元素在 y 轴上的缩放比当只有 x,没有 y 时,则 y 默认为 x,元素在 x,y 轴上均匀缩放 transform:scale(2,3) skew(ax,ay) ax 表示一个角度,表示沿着 x 横坐标扭曲元素的角度 ay 表示一个角度,表示沿着 y 纵坐标扭曲元素的角度 transform:skew(30deg,30deg) skewX(ax) ax 表示一个角度,表示沿着 x 横坐标扭曲元素的角度,形成水平拉伸效果 transform:skewX(30deg) skewY(ay) ay 表示一个角度,表示沿着 y 纵坐标扭曲元素的角度,形成垂直拉伸效果 transform:skewY(30deg) # 2、transform 的 3D 转换属性值 属性值 描述 实例 translateZ(z) x 为 px 长度单位元素在 3D 空间 z 轴方向上的位移量 transform:translateZ(200px) translate3d(x,y,z) x,y,z 分别为 px 长度单位x 表示元素在 x 轴坐标上的位移量y 表示元素在 y 轴坐标上的位移量z 表示元素在 z 轴坐标上的位移量,该值不能使用百分比,如果使用会被认为是无效属性 transform:translate3d(50px,30px ,50px) rotateX(ax) 表示元素绕X轴旋转一定的角度ax 表示度数,可以为正,也可以为负 transform:rotateX(30deg); rotateY(ay) 表示元素绕Y轴旋转一定的角度ay 表示度数,可以为正,也可以为负 transform:rotateY(30deg) rotateZ(az) 表示元素绕Z轴旋转一定的角度ax 表示度数,可以为正,也可以为负 transform:rotate(30deg); # 二、2D 转换 TIP 在学习 2D 转换之前,我们先来了解下 2D 平面。下图这个 x 和 y 轴,就是基于 2D 的平面图形。后期的所有 2D 动画特效都是在这个 2D 的平面操作。 注意事项: 元素的左上角坐标为 (0,0) (0,0)坐标的右方,为 x 轴的正方向 (0,0)坐标的下方,为 y 轴的正方向。 # 1、translate 位移 TIP css 中通过给transform添加属性值translate(x,y)或translateX(x)或translateY(y)来实现元素的水平或垂直位移。 属性值 描述 举例 translate(x,y) 实现元素水平和垂直位移量 translate(30px,50px) translateX(x) 实现元素水平位移量 translateX(30px) translateY(y) 实现元素垂直位移量 translateY(50px) # 2、translate(x,y) TIP x 和 y 是长度值,单位可以是 px,也可以是%百分比 x 表示元素在 x 轴上的位移量,为正表示向右移动,为负表示向左移动 y 表示元素在 y 轴上的位移量,为正表示向下移动,为负表示向上移动 位移和相对定位非常像,位移变形也会 "老家留坑",即会占据原来的位置 位移不会对其它元素造成影响,相当于悬浮在其它元素上面。 x,y 为 px 长度单位 渲染效果: # 2.1、位移值为%百分比 TIP 位移值单位可以 px ,也可以是%百分比 x 和 y 为%百分比,这个百分比分别是相对元素自身的 可视宽 和 可视高 而言。 在标准盒子模型下:水平位移 =(width + 左右 padding + 左右 border) * x% 垂直位移 =(height + 上下 padding + 上下 border) * y% 渲染效果: # 2.2、位移的默认值 TIP transform: translate(x,y) 实现位移时,如果 y 值不写,则默认为 0 渲染效果: # 3、translateX(x) 与 translateY(y) TIP translateX(x)实现元素在 X 轴的位移量 translateY(y)实现元素在 Y 轴的位移量 本质上 translate(x,y) 是 translateX 与 translateY 两者的复合写法 transform 后面如果跟多个变换函数,则各函数之间用空格隔开。 # 4、translate 与绝对定位结合实现元素水平垂直居中 # 5、scale 缩放 TIP css 中通过给transform添加属性值scale(x,y)或scaleX(x)或scaleY(y)来实现元素的宽高缩放效果 缩放不会对其它元素造成影响,相当于悬浮在其它元素上面 # 5.1、scale(x,y) TIP x,y 都是数字类型,也可以用百分比%表示,分别表示元素的宽和高缩放比 当 x,y 的值 >1 时,表示放大; 当只有 x,没有 y 时,则 y 默认为 x,元素宽高同时均匀缩放 scale(2,1.5) scale(2) scale(0.5,2) 宽放大 2 倍,高放大 1.5 倍 宽高同比放大 2 倍 宽缩小到 0.1 倍,高度放大 2 倍 # 5.2、百分比% 单位 TIP x% 和 y% 分别相对于元素的可视宽和可视高而言 在标准盒子模型下可视宽缩放后大小 =(width + 左右 padding + 左右 border) * x%; 可视高缩放后大小 =(height + 上下 padding + 上下 border) * y% scale(200%,50%) scale(200%) # 6、scaleX(x) 与 scaleY(y) TIP scaleX(x) 实现元素的可视宽缩放效果 scaleY(Y) 实现元素可视高缩放效果 本质上 scale(x,y) 是 scaleX(x) 与 scaleY(y) 的复合写法 # 7、skew 斜切 TIP css 中通过给transform添加属性值skew(ax,ay)或skewX(ax)或skewY(ay)变换函数来实现元素斜切效果 skewX(ax):用于水平拉伸,使元素的每个点在水平方向上扭曲一定角度 skewY(ay):用于垂直拉伸,使元素的每个点在垂直方向上扭曲一定角度 skew(ax,ay):用于水平和垂直拉伸,使元素在水平和垂直方向上扭曲一定角度 ax:表示一。度,用来表示沿着横坐标扭曲元素的角度 ay:表示一个角度,用来表示沿着纵坐标扭曲元素的角度 skewX(30deg) skewX(-30deg) skewY(30deg) skew(30deg,30deg) # 9、rotate 旋转 TIP css 中通过给transform:rotate(ax)来实现元素旋转效果 ax 表示旋转的角度,比如rotate(45deg) ax 为正,表示顺时针方向旋转 ax 为负,表示逆时针方向旋转 点击查看完整源代码 # 10、transform-origin 2D 变形原点 TIP transform-origin 属性,设置元素的 2D 和 3D 变换原点,即元素在变换时是以围绕那个点来发生变换的。 注: x , y 的值,可以长度值(px 或百分比%)还可以是预设的关键词 z 的值只能是长度值(后面 3d 详细讲) 2D 变形原点 在没有讲到 3D 之前,我们先只在 2D 的基础上来演示,所以只需要写两个值就 ok,即transform-origin:x y; 在 2D 转换中,元素的默认变换原点为元素的中心 # 10.1、关键字表示 TIP 关键词(left、right)与(top、bottom). Unable to stop CSS3 transition of transform by translate3d. 1. Halting a translate3d transition. 3. Apply CSS3 Transition To Some Transforms (translate3d) but Not Others (rotate) CSS3 Multiple Transforms: translate() and scale() 0. Setting translate3d independent of other transform. 2. Using translate3d and scale for transformations. HotCSS Animation – 3D Transforms Using translate3d ()
Skewed in the x direction. The angle can be represented by deg, rad or grad units. For example, skew(40deg) The skewX() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. skewY() Skews the element along the x axes. Syntax: -webkit-transform: skewY(angle) Where angle represents how much the element should be skewed in the y direction. The angle can be represented by deg, rad or grad units. For example, skew(40deg) The skewY() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translate() Specifies a 2D translation vector. Syntax: -webkit-transform: translate(deltaX [, deltaY]) Where deltaX represents how much the element should be translated in the x direction, and deltaY is the number of units to translate in the y direction. The angle can be represented by a percentage or length. The translate() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translate3d() Specifies a 3D translation vector. Syntax: -webkit-transform: translate3d(deltaX, deltaY, deltaZ) Where deltaX represents how much the element should be translated in the x direction, deltaY is the number of units to translate in the y direction, and deltaZ is the number of units to translate in the z direction. The angle can be represented by a percentage or length. The translate3d() transform function is available on the following: Safari 4.0.3 and later running on Mac OS X v10.6 and later. iOS 2.0 and later. Google Chrome 12.0 and later. translateX() Specifies a translation in the x direction. Syntax: -webkit-transform: translateX(deltaX) Where deltaX represents how much the element should be translated along the x axis. The angle can be represented by a percentage or length. The translateX() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translateY() Specifies a translation in the y direction. Syntax: -webkit-transform: translateY(deltaY) Where deltaY represents how much the element should be translated along the y axis. The angle can be represented by a percentage or length. The translateY() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translateZ() Specifies a translation in the z direction. Syntax: -webkit-transform: translateZ(deltaZ) Where deltaZ represents how much the element should be translated along the zcss transforms - translate3d vs translate performance
Definition The transform-style CSS property is used to define how child elements are rendered in relation to their parent when 3D transformations are applied. It specifies whether child elements should preserve their 3D transformations or be flattened and rendered in a 2D plane. The transform-style property accepts the following values: flat: This is the default value. Child elements are rendered in a flattened manner, disregarding any 3D transformations applied to their parent. This means that child elements are rendered in a 2D plane, as if the parent’s 3D transformations do not affect them. preserve-3d: Child elements preserve their 3D transformations and are rendered in their own 3D space, respecting the transformations applied to their parent. This allows for the nesting of multiple 3D transformed elements, creating a more realistic 3D scene. Here’s an example: .container { transform-style: preserve-3d;} In this example, the .container class sets the transform-style property to preserve-3d, indicating that child elements within the container should preserve their 3D transformations. It’s important to note that the transform-style property only has an effect when used in conjunction with 3D transformations (transform: translate3d(), transform: rotate3d(), etc.) on parent and child elements. It is primarily used in 3D animations and transitions to create more immersive and realistic effects. When using transform-style: preserve-3d, it’s essential to ensure that the parent and child elements have appropriate 3D transformations set and that their rendering order is considered. Additionally, keep in mind that the preserve-3d value may not be fully supported in older browsers orjavascript - Transform translate3d on scroll - Stack Overflow
与 center 三组中任意一个与另一组中的一个关键字组合来表示 如下表 关键 字 描述 left 原点为元素左边中间位置,同left center一样 right 原点为元素右边中间位置,同right center一样 top 原点为元素上边中间位置,同top center一样 bottom 原点为元素下边中间位置,同bottom center一样 center 原点为元素中间位置,同center center一样 top left 原点为元素左上角,相当于坐标(0 0) top right 原点为元素右上角 left bottom 原点为元素左下角 right bottom 原点为元素右下角 元素的默认变换原点为元素的中间位置transform-origin:center; 点击查看完整源代码 # 10.2、 数值表示法 TIP x , y 为长度单位(px 或百分比%) 当 y 省略不写时,默认为元素的垂直中心点 点击查看完整源代码 # 10.3、百分比表示法 TIP 百分比相对的是元素自身的可视宽和可视高而言 点击查看完整源代码 # 11、 scale 与 skew 都可以设置其变换原点 TIP 元素默认的转换原点为元素的中心点 transform-origin:50% 50%; 点击查看完整源代码 未设置原点 transform-origin:0 0; transform-origin:100%; # 11、2D 转换综合写法顺序问题 TIP 同时使用多个转换,其多个转换函数之间用空格隔开 转换函数的顺序会影响转换的效果,比如:先旋转会影响坐标轴方向 点击查看完整源代码 未旋转和位移 先位移再旋转 x 正轴向右,y 正轴向下 先旋转,改变了 x 轴和 y 轴方方向,x 正轴向左,y 正轴向上。然后再向 x 正轴(左)位移 200px 先向 x 轴正方向(右)位移 200px,再旋转 注: 当我们需要同时位移和旋转时,需要注意位移和旋转的书写顺序,因为写旋转会影响到元素的坐标轴方向。 # 12、transform 属性注意事项 TIP 在前面 transform 中定义的转换,在后续的效果中也要带上,否则会出现不按预期效果显示。 渲染效果: # 常见的错误写法 鼠标未移入时效果 上面代码 .box:hover .item 选择器中,不同代码,鼠标移入后效果 transform: scale(0.5); transform: scale(0.5) translate(-50%, -50%); transform: translate(-50%, -50%) scale(0.5); 只有缩小,前面的位移丢掉了 先缩小,后面位移是相对缩小后的宽高而言 先位移,水平垂直居中,再缩小 # 三、2D 转换实战应用 TIP 深入浅出,2D 转换 在企业项目中的实战应用。 # 1、旋转的三角形 点击查看完整源代码 # 2、鼠标滑动图片放大 点击查看完整源代码 # 3、渐进式淡入动画 # 四、3D 转换 TIP 在学习 3D 转换前,我们先看下下面的 3D 的坐标图,这样会更有利于我们后期对 3D 动画的理解。 在 3D 动画中,元素是围绕 3D 坐标X,Y,Z轴来变换的。 我们来看下面这个图 注: 元素的左上角坐标为(0,0,0) x 轴右边为正方向 y 轴的下方为正方向 z 轴垂直电脑屏幕,射向我们的那一头为正方向 在了解了 3D 坐标后, 我们还需要了解一个非常重要的概念:透视 # 1、perspective 透视 TIP 没有透视定义,不成 3D。 透视是指在平面上描绘物体的空间关系的方法或技术。 要在平面上描绘物体的空间立体感,需要遵顺透视原理中的 近大远小 的规则 当一个物体离我们越近,我们看到他会比离我们远的时候要大。 视点 视点是绘画者(或观察者)眼睛的位置 CSS3 中 3D transform 的 透视点(视点) 是在浏览器的前方! 注: 在 css3 中,用perspective属性来设置视距,即模拟人站在离屏幕多远的地方来看电脑上显示的元素 比如perspective:800px;意思就是在离屏幕 800px ( z 轴 800px ) 的地方看到这个元素(元素在 z = 0 的位置)。 如果元素往Z 轴正方向移动,离我们越近,看到物体就越大, 如果元素往Z 轴负方向移动,离我们越远,看到的物体就越小 perspective这个属性要设置在 3D 元素的 父元素上面,表示观察者距离3D 场景的虚拟距离 # 2、translate 3D 位移 TIP translateX(x) 表示水平方向(x 轴)的位移量,与 2D 位移是一样的效果 translateY(y) 表示垂直方向(y 轴)的位移量,与 2D 位移是一样的效果 translateZ(z) 表示垂直于电脑屏幕方向(z 轴)的位移量 translate3d(x,y,z) 表示元素在 x,y,z 三个坐标上的位移量,用来称动元素在 3D 空间中的位置 # 2.1、translateZ(z) TIP 我们要能看到 3D 的效果,必需要配合perspective 属性,perspective属性用来定义透视强度。 注: 当 perspective 的值为 300px,则 300px 就会成为当下显示效果的基准。在这个基准下 translateZ=0 图形大小正常显示 0 图形变大 translateZ>=300 图片不见,相当于图片在你的后面,你肯定是看不见的了 translateZ 图形变小 点击查看完整源代码 perspective:300px; perspective:900px 注: 当perspective:300px;时,向前移动100px,离元素还差200px 发perspective:900px;时,向前移动100px,离元素还差800px 所以当perspective:300px时看到的要比perspective:900px时的更大。 其向后移动 100px,则看到的更小,原理类似 # 2.2、 translate3d(x,y,z) TIP 表示元素在 x,y,z 三个坐标上的位移量,用来移动元素在 3D 空间中的位置 z 的值不能使用百分比,如果使用会被认为是无效属性 点击查看完整源代码 # 3、rotate 3D 旋转 TIP css 中通过给transform添加下以变换函数值来实现 3D 旋转效果 属性值 描述 rotateX(ax) 表示元素绕X轴旋转一定的角度ax 表示度数,可以为正,也可以为负 rotateY(ay) 表示元素绕Y轴旋转一定的角度ay 表示度数,可以为正,也可以为负 rotateZ(az) 表示元素绕Z轴旋转一定的角度ax 表示度数,可以为正,也可以为负 # 3.1、3D 旋转方向 左手法则 我们并不需要死记的方式来记住不同轴上的正角和负角的旋转方向。我们可以通过左手法则来记忆。 x 轴 y 轴 z 轴 大拇指方向指向 x 的正方向,剩下四个手指的方向为 x 轴旋转的正方向,那反过来就是反方向了 大拇指方向指向 Y 的正方向,剩下四个手指的方向为 Y 轴旋转的正方向,那反过来就是反方向了 大拇指方向指向 Z 的正方向,剩下四个手指的方向为 Z 轴旋转的正方向,那反过来就是反方向了 # 3.2、rotateX、rotateY、rotateZ 点击查看完整源代码 # 4、transform-style TIP 设置元素的子元素是位于 3D 空间中还是平面中 值 描述 flat 表示所有子元素在 2D 平面呈现。 preserve-3d 表示所有子元素在 3D 空间中呈现。 点击查看完整源代码 transform-style:flat 效果 transform-style:preserve-3d 效果 # 5、transform-origin TIP transform-origin 属性,设置元素的变换原点,即元素在变换时是以围绕那个点来发生变换的。 x , y 的值,可以长度值(px 或百分比%)还可以是预设的关键词。 z 的值只能是长度值 px transform-origin 默认值为: 50% 50%:这表示变换原点在元素的水平和垂直方向上的中心位置。也就是说,如果不对 transform-origin 进行任何设置,元素的变换(如旋转、缩放等)将以其中心点为基准进行。 0:这表示变换原点在 Z 轴(深度轴)上的位置为 0,即原点位于 2D 平面上,而不是在 3D 空间中偏移。 关于 3D 变换原点设置后对应的效果演示,在下面实战案例《创建长方体》中演示 # 6、perspective 透视对子元素影响 TIP 在所有子元素 li的父元素 ul 中设置视距 perspective:300px 在视觉上,子元素从左往右越来越小,两者间的间距也越来越大 为了让同一父级的所有子元素从左往右倾斜的角度和大小在视觉上呈现一致。我们需要将 perspective:300px 添加到这些子元素上。 我们将perspective:300px 添加到每个 li 元素身上 在li 中创建子元素 div ,然后对该 div 元素做旋转。 # 五、2D 与 3D 简单总结 TIP 深入浅出 CSS3 2D 和 3D 变换的常用属性、函数和重要属性的总结。 # 1、变换函数 TIP 位移变换函数:translate(x,y)、 translateX(x) 、 translateY(y) 、 translateZ(y)x,y 的值为位移长度,单位可以是 px、百分比 z 的值只能为 px 长度单位 translate(x) 同等于 translate(x,0); 缩放变换函数:scale(x,y) 、scaleX(x)、 scaleY()x,y 的值为数字,分别表示宽度和长度的缩放比例 scale(x) 表示 y 的值和 x 一样大,实现宽高等比缩放 斜切变换函数:skew(ax,ay)、 skewX(ax)、 skewY(ax)ax、ay 分表示水平和垂直拉升,使元素与 x. Unable to stop CSS3 transition of transform by translate3d. 1. Halting a translate3d transition. 3. Apply CSS3 Transition To Some Transforms (translate3d) but Not Others (rotate) CSS3 Multiple Transforms: translate() and scale() 0. Setting translate3d independent of other transform. 2. Using translate3d and scale for transformations. HotComments
The translate3d() function in CSS is used to translate, or move, an element in three-dimensional space. The result is a datatype.The translate3d() function results in the transformation, based on the three-dimensional vector [tx, ty, tz]. The coordinates value determine the direction in which the element will move.Possible valuesThe function translate3d() takes the following values as parameter(s).tx: Can be a or value that represents the abscissa (horizontal, x-component) of the translating vector [tx, ty, tz].ty: Can be a or value that represents the ordinate (vertical, y-component) of the translating vector [tx, ty, tz].tz: Can only be a value that represents z-component of the translating vector [tx, ty, tz]. It can not have a value and if given the property containing the transform will be invalid.Syntaxtransform: translate3d(tx, ty, tz);CSS translate3d() - Length ValueFollowing is an example of translate3d() function with the various ways in which length values can be passed to it, i.e., positive and negative values: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: translate3d(20px, 30px, 20px); background-color: yellowgreen; } .translate-3d-negative { transform: translate(-20px, -10px, -30px); background-color: tomato; } no translate() applied translate3d(20px, 30px, 20px) translate3d(-20px, -10px, -30px) CSS translate3d() - Combining x-axis and z-axis valuesFollowing is an example of translate3d() function with length values passed to x-axis and z-axis: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: translate3d(20px, 0, 20px); background-color: yellowgreen; } no translate3d() translate3d(20px, 0, 20px) no translate3d() CSS translate3d() - Combining y-axis and z-axis valuesFollowing is an example of translate3d() function with length values passed to y-axis and z-axis, along with perspective() value: #container { border: 5px solid black; margin: 25px; } #sample { height: 110px; width: 110px; border: 2px solid black; } .translate-3d-length { transform: perspective(580px) translate3d(0, 30px, 50px); background-color: yellowgreen; } no translate3d() translate3d(0, 30px, 50px) no translate3d()
2025-03-26Take values, NOT percentage values. This makes sense because there is nothing relative to which a percentage value provided to translateZ() can resolve.Let's consider some examples.In the following code, we have a element styled as a square.CSSdiv { width: 100px; height: 100px; background-color: blue;}Let's translate it forwards by 100px:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: translateZ(100px);}Surprisingly, when we run the code, there's absolutely no effect.And that's because one thing is missing from the rendering of the square so as to give the impression that it has moved closer to the viewer, i.e. perspective.So, let's incorporate perspective into our example, and see the change it brings:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: perspective(1000px) translateZ(100px);}Note that we're using the perspective() function directly on the , followed by the translateZ() function.Let's see the output:Yup, it works this time.Voila! There's our forwards-translated square, larger in size, thanks to the advent of perspective into the equation.Now, let's shift gears and instead translate the square away from the viewer, using a negative z-value.This time, as per expectation, the square should appear smaller:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: perspective(1000px) translateZ(-500px);}And it indeed does.Remember that if an element is given perspective via the perspective() transform function, the function must come before all other functions. Otherwise, we won't get any perspective effect.This we learnt in the previous chapter, and it's also illustrated below:CSSdiv { width: 100px; height: 100px; background-color: blue; transform: translateZ(-150px) perspective(1000px);}This is because every function is processed in the very order that it appears in transform.The translate3d() functionImagine you want to work with translations in all three axes — x, y and z. One obvious way it to use the individual functions translateX(), translateY() and translateZ().A simple example follows:CSSdiv { width: 50px; height: 50px; background-color: blue; transform: perspective(1000px) translateX(100px) translateY(100px) translateZ(-500px);}However, just as translate() is a conciser way to combine the values of translateX() and translateY(), so do we have a function to combine translation in the three axes.It's the translate3d() function.Here's the syntax of translate3d():The three arguments respectively govern translation in the x, y and z axis.tx can be
2025-04-09Skewed in the x direction. The angle can be represented by deg, rad or grad units. For example, skew(40deg) The skewX() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. skewY() Skews the element along the x axes. Syntax: -webkit-transform: skewY(angle) Where angle represents how much the element should be skewed in the y direction. The angle can be represented by deg, rad or grad units. For example, skew(40deg) The skewY() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translate() Specifies a 2D translation vector. Syntax: -webkit-transform: translate(deltaX [, deltaY]) Where deltaX represents how much the element should be translated in the x direction, and deltaY is the number of units to translate in the y direction. The angle can be represented by a percentage or length. The translate() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translate3d() Specifies a 3D translation vector. Syntax: -webkit-transform: translate3d(deltaX, deltaY, deltaZ) Where deltaX represents how much the element should be translated in the x direction, deltaY is the number of units to translate in the y direction, and deltaZ is the number of units to translate in the z direction. The angle can be represented by a percentage or length. The translate3d() transform function is available on the following: Safari 4.0.3 and later running on Mac OS X v10.6 and later. iOS 2.0 and later. Google Chrome 12.0 and later. translateX() Specifies a translation in the x direction. Syntax: -webkit-transform: translateX(deltaX) Where deltaX represents how much the element should be translated along the x axis. The angle can be represented by a percentage or length. The translateX() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translateY() Specifies a translation in the y direction. Syntax: -webkit-transform: translateY(deltaY) Where deltaY represents how much the element should be translated along the y axis. The angle can be represented by a percentage or length. The translateY() transform function is available on the following: Safari 3.1 and later. iOS 2.0 and later. Google Chrome 1.0 and later. translateZ() Specifies a translation in the z direction. Syntax: -webkit-transform: translateZ(deltaZ) Where deltaZ represents how much the element should be translated along the z
2025-04-25Definition The transform-style CSS property is used to define how child elements are rendered in relation to their parent when 3D transformations are applied. It specifies whether child elements should preserve their 3D transformations or be flattened and rendered in a 2D plane. The transform-style property accepts the following values: flat: This is the default value. Child elements are rendered in a flattened manner, disregarding any 3D transformations applied to their parent. This means that child elements are rendered in a 2D plane, as if the parent’s 3D transformations do not affect them. preserve-3d: Child elements preserve their 3D transformations and are rendered in their own 3D space, respecting the transformations applied to their parent. This allows for the nesting of multiple 3D transformed elements, creating a more realistic 3D scene. Here’s an example: .container { transform-style: preserve-3d;} In this example, the .container class sets the transform-style property to preserve-3d, indicating that child elements within the container should preserve their 3D transformations. It’s important to note that the transform-style property only has an effect when used in conjunction with 3D transformations (transform: translate3d(), transform: rotate3d(), etc.) on parent and child elements. It is primarily used in 3D animations and transitions to create more immersive and realistic effects. When using transform-style: preserve-3d, it’s essential to ensure that the parent and child elements have appropriate 3D transformations set and that their rendering order is considered. Additionally, keep in mind that the preserve-3d value may not be fully supported in older browsers or
2025-04-24Learning outcomes: A quick recap of translation and the z-axisThe translateZ() functionThe translate3d() function IntroductionIn the previous chapter, CSS 3D Transformations — Perspective, we learnt about the role of perspective in 3D transformations in CSS.In particular, we saw the perspective() transform function and the perspective and perspective-origin properties. Perspective on its own won't do anything, as we stated in that chapter, and has to be combined together with an actual transformation.In this chapter, we shall begin by unravelling 3D translations, that are one such kind of transformations in CSS.Specifically, we'll go over the translateZ() and translate3d() functions and consider a good handful of examples, both with the perspective() function and the perspective property.Let's begin.A quick recapBefore we begin, let's quickly recap what are translations in CSS and what is the z-axis.A translation is simply to move an element from one point to another on a canvas. For instance, if we have a square as follows:translating it by 20px to the right and 20px to the bottom would leave us with the following configuration:Remember that the coordinate system of the web works as follows for the x and y axes:The origin is typically the top-left position.Positive x values go rightwards and negative x values go leftwards (just as in the Cartesian co-ordinate system).Positive y values go downwards and negative y values go upwards (opposite to the Cartesian co-ordinate system).The translate(), and the individual translateX() and translateY() functions are used to perform translations of elements in the 2D xy plane.Now talking about the z-axis, it is a virtual axis (in that we can't really visualize it on the screen) that runs from the viewer's eye towards the screen and beyond.Positive z values go towards the viewer.Negative z values go backwards, away from the viewer.And this is essentially our quick recap. Time to discuss the translateZ() function.The translateZ() functionThe translateZ() function is used to translate an element in the z-axis.As stated ealier, positive values bring the element closer to the viewer whereas negative values take it away from the viewer.Syntactically, translateZ() is a little different than translateX() (and translateY()):That is, it can only
2025-04-24