您的位置:

CSS Scrollbar样式化:提升页面用户体验

在当今互联网时代,用户体验已经成为了各大网站和应用的核心竞争力之一。而在网站或应用的页面滚动中,滚动条是不可或缺的一部分,然而默认的滚动条样式太过单调,无法满足用户的审美需求,更不能提升用户体验。因此,我们需要对滚动条进行样式化处理,以此来提升用户体验。本文就介绍如何通过CSS对滚动条进行样式化。

一、精美的滚动条样式

让我们看看下面的代码:

/*火狐的滚动条*/ 
  * {
      scrollbar-base-color: #6CA6CD;
      scrollbar-track-color: #fff;
      scrollbar-arrow-color: #fff;
      scrollbar-face-color: #6CA6CD;
  }
  area,button,input,textarea {
      scrollbar-face-color: #6CA6CD;
      scrollbar-highlight-color: #f0f8ff;
      scrollbar-3dlight-color: #f0f8ff;
      scrollbar-shadow-color: #AADCFA;
      scrollbar-darkshadow-color: #95B9C7;
  }

/*IE的滚动条*/ 
  * {
      scrollbar-track-color: #fff; 
      scrollbar-face-color: #6CA6CD;
      scrollbar-highlight-color:#f0f8ff;
      scrollbar-3dlight-color: #f0f8ff;
      scrollbar-shadow-color: #AADCFA;
      scrollbar-darkshadow-color: #95B9C7;
  }
  area,button,input,textarea {
      scrollbar-base-color: #6CA6CD;
      scrollbar-arrow-color: #fff;
  }

以上代码定义了对Firefox和Internet Explorer两种浏览器的滚动条进行不同的样式设置。可以看到,白色和蓝色是主要的颜色组合。这样的滚动条看起来清新、时尚且不突兀。

二、滚动条颜色渐变过渡

除了用单一的颜色样式,我们还可以引入 gradient() 函数来实现颜色的过渡效果。以下是代码示例:

  /*火狐的滚动条*/ 
  *{
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: transparent transparent;
  }

  *::-webkit-scrollbar {
    width: 8px;
  }

  *::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    background-color: #f5f5f5;
  }

  *::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
    background-image: linear-gradient(-225deg, #e3fdf5 0%, #fddfdf 52%, #e3fdf5 100%);
  }

以上代码用 gradient() 函数定义了滚动条颜色的过渡效果,可以更加平滑过渡,使得滚动条看起来更加顺滑和自然。代码中通过webkit的前缀完成兼容。代码适用于chrome浏览器和safari浏览器和新版本edge。老旧版本使用IE内核,需要为其添加-ms的样式属性。

三、隐藏或显示滚动条

有时,页面上的滚动条可能会干扰页面的布局和排版。在这些情况下,可以使用如下方法,将滚动条隐藏:

  /*Firefox*/
  * {
    overflow: -moz-hidden-unscrollable;
  }

  /*Chrome和Safari*/
  *::-webkit-scrollbar {
    display: none;
  }

  /*IE和Edge*/
  *{
    -ms-overflow-style: none; 
    scrollbar-width: none; 
  }

以上代码分别用于Firefox、Chrome、Safari、IE和Edge浏览器,以实现隐藏滚动条的功能。如需重新显示,则需要用到相应的 overflow -ms-overflow-style 属性来设置。

四、自定义滚动条的宽度和高度

我们通过设置宽度和高度的值,来定义自己的滚动条样式。以下是示例代码:

  /*火狐的滚动条*/ 
  *{
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: transparent transparent;
  }

  *::-webkit-scrollbar {
    height: 18px;
    width: 8px;
  }

  *::-webkit-scrollbar-track {
    background-color: rgba(255, 255, 255, .2);
    -webkit-border-radius: 10px;
    border-radius: 10px;
  }

  *::-webkit-scrollbar-thumb {
    -webkit-border-radius: 10px;
    border-radius: 10px;
    background-color: rgba(255, 255, 255, .5);
  }

以上代码演示了如何自定义滚动条的宽度和高度,其中, -webkit-scrollbar 属性被用于定义滚动条的宽度和高度, -webkit-border-radius 属性被用于定义滚动条工作区的边缘半径。

五、耀眼的滚动条

我们也可以通过自定义很多颜色值,来让滚动条变得动感。以下是示例代码:

  /*火狐的滚动条*/ 
  *{
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
    }

  *::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}
*::-webkit-scrollbar-track {
  background-color: white;
  border-radius: 10px;
}
*::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background-image: -webkit-gradient(linear,left top,left bottom,from(#f2f2f2),to(#8f8f8f));
  background-image: linear-gradient(180deg, #8f8f8f, #f2f2f2);
  -webkit-transition: all .2s ease-in-out;
  transition: all .2s ease-in-out;
}

*::-webkit-scrollbar-thumb:hover {
  background-image: -webkit-gradient(linear,left top,left bottom,from(#555),to(#f2f2f2));
  background-image: linear-gradient(180deg, #f2f2f2, #555);
}

以上代码定义了耀眼的滚动条,其中用到了 gradient() 函数来实现颜色过渡的效果。当鼠标悬停在滚动条上时,颜色会有所变化,这是一种交互模式,为用户带来了一定的趣味性。同样的方式,在chrome和safari浏览器中启用,可以通过webkit的前缀来处理。

六、结语

本文介绍了如何通过CSS对滚动条进行样式化,让页面更加美观,用户体验更加流畅。滚动条虽然不是页面重点,但关键的时刻,它是重要的角色。CSS让我们的滚动条不仅兼顾了功能性,同时也能够呈现出对用户有吸引力的人性化设计。

CSS Scrollbar样式化:提升页面用户体验

2023-05-12
提高网站用户体验的CSS滚动条样式

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

2023-05-12
CSS隐藏滚动条样式

2023-05-16
使用CSS美化网页滚动条

2023-05-18
如何利用Shadow CSS提升网站用户体验

2023-05-12
从多个角度全面解析隐藏滚动条CSS

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