您的位置:

CSS background-attachment属性详解

CSS的background-attachment属性规定了背景图片的固定方式。在很多情况下,一个网页的背景图片需要随着用户的滚动而移动,但是有些时候,我们只是希望背景图片一直显示在页面的某个位置上,无论用户怎么滚动页面,这时可以使用background-attachment属性来实现这个效果。

一、background-attachment属性的取值

background-attachment属性可以接受的值分为三种,分别为scroll、fixed、local。

1、scroll:默认值。背景图片随着页面的滚动而滚动。

    background-attachment: scroll;

2、fixed:背景图片固定,不随页面的滚动而滚动。

    background-attachment: fixed;

3、local:背景图片随着对象本身的移动而移动。

    background-attachment: local;

二、background-attachment属性的应用

1、使用background-attachment: fixed

fixed的背景图片会一直固定在浏览器的可视区域,无论用户怎样拖动页面。这种方式常用于页面的头部、边栏等区域的背景图片。

    html, body {
        height: 100%;
        margin: 0;
    }
    .header {
        background-image: url("header_bg.jpg");
        background-repeat: no-repeat;
        background-size: cover;
        background-attachment: fixed;
    }

2、使用background-attachment: local

local的背景图片跟随对象的移动而移动,它们自己是永远不会移动的。这种方式常用于和文本相关的背景图片设置,例如使用一个特殊的标签来为某段文本添加一个特定的背景图片。

    .special-text {
        background-image: url("special_bg.jpg");
        background-repeat: no-repeat;
        background-size: cover;
        background-attachment: local;
        padding: 10px;
    }

3、使用background-attachment: scroll

scroll的背景图片随用户滑动而滑动,这种方式是默认的方式,因此通常不需要指定background-attachment属性。

    html, body {
        height: 100%;
        margin: 0;
    }
    .main-content {
        background-image: url("main_bg.jpg");
        background-repeat: no-repeat;
        background-size: cover;
        /*background-attachment: scroll;*/
    }

三、背景图片的优化

在实际项目中,由于背景图片通常都是比较大的,因此需要进行优化以避免影响网页的加载速度和用户体验。

1、合理压缩图片大小

在使用背景图片时,首先需要考虑的是图片的大小。可以使用Photoshop等图片处理软件进行合理地压缩图片,从而减小图片的文件大小。

2、使用图片压缩工具

在压缩图片时,可以使用一些图片压缩工具,比如TinyPNG,它可以非常有效地压缩图片大小,通常可以将图片的大小缩小50%以上,而不会影响图片的清晰度和质量。

3、使用CSS sprites

另外,还可以使用CSS Sprites技术将多个小的背景图片合并成一个大的图片,以减小HTTP请求数量,从而加快页面加载速度。

    .icon-home {
        background: url("icons.png") no-repeat -10px -10px;
    }
    .icon-user {
        background: url("icons.png") no-repeat -36px -10px;
    }
    .icon-cart {
        background: url("icons.png") no-repeat -62px -10px;
    }

总结

通过background-attachment属性,我们可以有效地控制背景图片的滚动方式,将其固定在某个位置上或跟随对象的移动而移动。但是在使用背景图片时,需要注意图片优化,避免影响网页的加载速度和用户体验。

CSS background-attachment属性详解

2023-05-12
CSS中的background-position属性详解

2023-05-12
CSS background-color 属性详解

2023-05-20
CSS Background Repeating

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)