您的位置:

Queuepop:如何提高用户体验并优化页面性能

一、优化图片

1、使用正确的图片格式

在选择图像格式时需要考虑不只图片质量和大小,还要考虑图像格式。图片格式的选择直接影响页面加载速度,下面列出了最常见的三种图片格式:

  • JPEG(有损压缩,对于复杂的图像,颜色变化较多的图像效果不是很好)
  • PNG(无损压缩,对于透明度的处理比较好)
  • GIF(支持动画)

2、压缩图片

一般来说,我们需要在不影响图片质量的前提下,将图片尽可能的压缩,以减少加载时间。

//html代码实例
<img src="example.jpg" alt="A beautiful example"/>

二、CSS和JavaScript文件的加载位置

1、CSS和JavaScript文件的加载位置对于性能的影响

CSS和JavaScript文件的加载位置的影响会更高。这个时候,请使用defer属性,在页面渲染完毕后,再开始加载解析这些标签。这样能让DOM加载速度更快,进而让页面响应时间更快。

2、优化JavaScript文件

尽量减少JavaScript文件的体积,压缩文件和用精炼的代码能有效的减少文件大小。

//html代码实例
<head>
  <link href="example.css" rel="stylesheet">

  <script src="example.js" defer></script>
  <script src="example2.js" defer></script>
</head>

三、减小页面大小

1、减小DOM

使用合适的不冗余的HTML标签

2、减少HTTP请求

减少所需资源的HTTP请求数量能够大大提高性能。

3、避免重复的代码

当有一段代码被使用多次时,可以将其封装为一个函数并将其放置在共享.js文件中,以避免在文档中重复出现。

4、服务端渲染

如果您使用的是显示用户信息的动态页面,可以使用服务器端渲染后,将HTML文件传递给浏览器以减少页面大小。

//html代码实例
<!doctype html>
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <div>Hello World!</div>
  </body>
</html>

四、Lazy loading

Lazy Loading是一种在展示内容时,根据用户的滚动来加载可见图像的方法。这是一种提高页面性能和用户体验的简单但强大的技术。Lazy Loading的主要目标是加载进入视图的图片,而不是页面上的所有图片。这可以大大减少首次加载页面时的负载时间。

//html代码实例
<img loading="lazy" src="example.jpg" alt="A beautiful example">

五、使用后端缓存技术

使用后端缓存技术能够大大减少执行时间和资源的使用,提高用户体验和页面性能。

//php代码实例
<?php
//输出编译并缓存的HTML
function output_cached_html($cache_path, $ttl) {
    // 检查页面是否已经缓存
    if(file_exists($cache_path) && time() - filemtime($cache_path) <= $ttl) {
        // 缓存还未过期
        readfile($cache_path);
        return true;
    } else {
        // 缓存已经过期或没有找到
        return false;
    }
}

//写入更新后的HTML
function write_html_to_cache($cache_path, $html) {
    // 将HTML写入新的缓存文件
    file_put_contents($cache_path, $html);
}
?>
Queuepop:如何提高用户体验并优化页面性能

2023-05-19
如何提高页面的可读性和用户体验

2023-05-19
如何优化网页样式提高用户体验

2023-05-12
如何优化网页样式,提高用户体验并增强页面趣味性?

2023-05-12
如何优化网页的可访问性和用户体验

2023-05-12
前端如何优化网页用户体验

2023-05-12
如何优化网站中的可见性并提升用户体验

2023-05-12
如何优化网页的用户体验

2023-05-12
如何优化网页样式以提高用户体验

2023-05-12
如何优化网站文本大小,提升用户体验和搜索排名

2023-05-12
jQuery优化策略:提升网页性能和用户体验

2023-05-18
优化网页文本颜色,提高用户阅读体验

2023-05-12
如何优化网页文本颜色,提高用户体验

2023-05-12
如何优化网页的可访问性和用户体验

2023-05-12
如何提高网页的可读性与用户体验?

2023-05-12
优化网页性能的技巧,提升用户体验

2023-05-12
如何提高网页的可读性和用户体验

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)