您的位置:

如何有效利用compositionstart提升网站用户体验

一、理解compositionstart事件:

CompositionStart事件是当用户开始使用输入方法编辑器(如中文输入)时触发的事件,也就是在用户开始输入之前,浏览器会发出compositionstart事件。


    document.addEventListener("compositionstart", function() {
        //TODO 在这里处理compositionstart事件
    });

利用这个事件,我们可以知道用户即将开始输入,然后就可以取消掉原来的防抖或者节流等优化函数,以便用户更好地输入。

二、为什么要使用compositionstart事件:

使用compositionstart事件可以提升网站的用户体验,主要表现在以下几个方面:

1、取消防抖和节流等优化函数:

当用户正在输入时,我们要尽可能减少程序的干扰,尤其是页面上各种DOM操作、网络请求等,以确保用户输入的流畅性。因此,在compositionstart事件发生时取消一些页面的优化函数,更加符合用户的需求。

2、隐藏输入框和菜单等元素:

有些网站在用户输入时会弹出各种菜单、输入提示框等,这些元素虽然可以提供用户便捷的操作,但也会占用页面的空间并且会造成输入的不流畅。此时我们可以在compositionstart事件中隐藏这些元素,让用户有更好的输入体验。

3、输入文本的实时预处理和提醒:

在compositionstart事件和compositionend事件之间,浏览器会触发多个input事件(如oninput或onpropertychange事件),我们可以利用这些事件来实现实时预处理或者输入提示功能,以便用户更好地输入。

三、实现实时输入预处理与提示:

下面是一个简单的例子,实时打印用户正在输入的内容,并在用户输入的内容中包含"你好"时弹出提示框:


    let textarea = document.getElementById("input-box");

    let temp = "";
    let timer = null;
    let flag = false;

    textarea.addEventListener("compositionstart", function() {
        clearTimeout(timer);
        flag = true;
    });

    textarea.addEventListener("compositionend", function() {
        flag = false;
        if (temp !== "") {
            alert("你好!"); //alert只是示例,用于展示效果
        }
        temp = "";
    });

    textarea.addEventListener("input", function() {
        if (flag) return;
        clearTimeout(timer);
        timer = setTimeout(function() {
            temp += textarea.value;
            console.log(temp);
        }, 100);
    });

四、小结:

CompositionStart事件可以帮助我们更好地优化网站的用户体验,提高用户的输入效率。通过理解CompositionStart事件,我们可以顺应用户输入的习惯,取消一些防抖和节流等优化函数,隐藏一些输入框和菜单等元素,并且实时预处理和提醒用户的输入文本,以达到更好的用户体验。

如何有效利用compositionstart提升网站用户体验

2023-05-22
如何利用Selenium Action Chains提升网站

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

2023-05-12
使用HTML多选下拉框提升网站用户体验

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

2023-05-12
使用TSMap提升网站流量和用户体验

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