您的位置:

CSS translate 实现元素位置转换

一、CSS translate的概念

CSS3中的transform属性是应用于元素的2D或3D转换。其中有多个属性可以用来控制元素的位置、旋转、缩放、扭曲等效果。其中之一就是translate()

translate() 函数能够将元素从其当前位置移动,并且需要两个值:水平方向的偏移和垂直方向的偏移。

/* 2D translate */
transform: translate(x,y);

/* 3D translate */
transform: translate3d(x,y,z);

其中,x 为水平方向的偏移量,正数向右移动,负数向左移动;y 为垂直方向的偏移量,正数向下移动,负数向上移动;z 为 3D 中的深度偏移量。

二、CSS translate的实现

1. 元素的平移

使用 translate() 实现元素的平移。

.box1 {
	position: relative;
	width: 100px;
	height: 100px;
	background-color: #f00;
	transform: translate(50px, 50px);
}

上述代码中,.box1 的初始位置在页面左上角,使用 transform: translate(50px, 50px); 将其移动到页面右下角。

2. 元素的旋转

使用 translate() 实现元素的旋转。

.box2 {
	position: relative;
	width: 100px;
	height: 100px;
	background-color: #0f0;
	transform: rotate(45deg) translate(50px, 50px) rotate(-45deg);
}

上述代码中,.box2 首先进行 45 度的顺时针旋转,然后使用 translate() 将其移动到指定位置,最后再进行逆时针 45 度的旋转,实现同时旋转和平移的效果。

3. 元素的缩放

使用 translate() 实现元素的缩放。

.box3 {
	position: relative;
	width: 100px;
	height: 100px;
	background-color: #00f;
	transform: scale(2) translate(-25px, -25px);
}

上述代码中,.box3 首先进行 2 倍的缩放,然后使用 translate() 将其向左上移动 25px,并根据父元素的大小水平和垂直居中。

三、CSS translate的应用场景

1. 平移动画

translate() 可以轻松实现元素的平移动画效果。

.box4 {
	position: relative;
	width: 100px;
	height: 100px;
	background-color: #f0f;
	animation: move 2s infinite alternate;
}

@keyframes move {
	0% { transform: translate(0, 0); }
	100% { transform: translate(50px, 50px); }
}

上述代码中,.box4 只需要一个简单的动画,就可以实现向右下方平移的效果。

2. 绝对定位元素的居中

translate() 可以快速实现绝对定位元素的水平居中和垂直居中。

.box5 {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}

上述代码中,.box5 水平和垂直居中,与其父元素保持居中对齐。

3. Banner效果

translate() 可以轻松实现轮播图中的平移效果,可以设置不同的位置和延迟。

.banner {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 200px;
}

.banner img {
	width: 100%;
	height: 100%;
	opacity: 0;
	transform: translateX(-50px);
}

.banner img:nth-child(1) {
	animation: slide 10s infinite;
}

.banner img:nth-child(2) {
	animation: slide 10s infinite 2s;
}

.banner img:nth-child(3) {
	animation: slide 10s infinite 4s;
}

@keyframes slide {
	0% { opacity: 0; transform: translateX(-50px); }
	25% { opacity: 1; transform: translateX(0); }
	75% { opacity: 1; transform: translateX(0); }
	100% { opacity: 0; transform: translateX(50px); }
}

上述代码中,轮播图中的每张图片使用 translateX() 函数进行水平平移,并设置不同的延迟时间,实现轮播的效果。

结语

通过对 translate() 函数的使用和应用,不仅能让我们更方便地实现页面效果,还能大大提高编写效率和开发效果。希望这篇文章能够帮助大家更好地应用 transform 属性。

CSS translate 实现元素位置转换

2023-05-12
Transform Translate实现元素位置移动

2023-05-17
如何使用CSS实现页面元素位置固定?

2023-05-12
使用CSS transform 改变元素的形状和位置

2023-05-12
CSS和JS实现元素移动

2023-05-12
CSS绝对定位元素的指定位置

2023-05-12
CSS移动位置

2023-05-12
CSS Transform XY: 如何在前端开发中优雅地实

2023-05-12
CSS和JavaScript结合实现元素移动

2023-05-12
CSS隐藏元素

2023-05-18
CSS如何实现对象位置的移动

2023-05-12
如何使用CSS固定位置的元素?

2023-05-12
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)