您的位置:

-webkit-overflow-scrolling属性详解

移动端的滚动条在使用中会遇到卡顿问题,为了解决这个问题,现在有一种很好的解决方案就是使用-webkit-overflow-scrolling属性。本文就通过多个方面对该属性进行详细阐述。

一、介绍

-webkit-overflow-scrolling是一个非常实用的CSS属性,它可以提升移动端滚动条的性能,使滚动更加流畅,而不会卡顿,非常适用于移动端开发。

在使用该属性时需要注意,它只适用于WebKit浏览器(Safari 和 Chrome)

二、使用方法

-webkit-overflow-scrolling可以用于任何可以滚动的元素,比如div、ul、ol等等。

使用该属性非常简单,只需要在元素上添加 "-webkit-overflow-scrolling: touch"即可:

.element {
  -webkit-overflow-scrolling: touch;
}

在设置overflow属性时,最好不要使用“auto”或“scroll”,建议使用“scroll”,才能确保滚动效果更为顺滑

三、性能提升原理

在移动设备上提升overflow滚动性能的关键是滚动事件的处理上。默认情况下,浏览器会等待scroll事件完成后重新渲染元素上的所有内容,尤其是元素中的图片或视频等大量内容,这样就导致了卡顿效果。而使用-webkit-overflow-scrolling属性后,浏览器会对滚动事件进行优化,只会渲染需要更新的元素,减少了不必要的DOM操作和重排,加快了页面的绘制速度,从而提高了滚动的流畅性。

四、使用时需要注意的问题

1、使用-webkit-overflow-scrolling属性时,尽量少嵌套div,并将元素的overflow属性设置为诸如“scroll”或“hidden”等属性,以优化移动端浏览器的性能。

2、该属性对于在position:fixed的元素中的溢出滚动是不起作用的,需要使用JS的hack来临时修复。

3、有些情况下会出现闪屏的情况,可以参考下面的CSS代码进行优化:

.element {
  -webkit-overflow-scrolling: touch;
  overflow-y: auto;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

通过添加-webkit-transform: translateZ(0) ;transform: translateZ(0);的CSS属性,可解决在部分设备(iOS 6及以下)中使用-webkit-overflow-scrolling简历出的白屏闪烁问题。

五、其他

除了使用-webkit-overflow-scrolling属性,还可以通过其他方法来提高移动端滚动的流畅性,比如禁用页面上的所有动画效果,避免滚动时触发DOM操作和reflow(重排)等。

总之,在移动端进行开发时,需要考虑到性能的问题,从而为用户提供更好的体验。

-webkit-overflow-scrolling属性详解

2023-05-23
如何去掉滚动条?

2023-05-20
CSS修改滚动条

2023-05-18
详解 window.scrollTo()

2023-05-18
CSS Sticky - 让页面元素“粘”在页面上的神奇属性

2023-05-17
Bootstrap遮罩的详解

2023-05-23
如何使用CSS Scrollbar Overflow来提高网

2023-05-12
滚动到顶部详解

2023-05-16
CSS Overflow属性的使用

2023-05-12
探讨overflow属性值的各种用法

2023-05-19
CSS HTML圆角表格

2023-05-12
理解iframe属性

2023-05-20
如何优雅地处理CSS文本溢出

2023-05-16
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)