您的位置:

利用滚轮控制网页内容,让用户体验更流畅

一、滚轮事件介绍

滚轮事件是指鼠标滚轮被滚动时所触发的事件。在页面上,滚轮事件可以用来实现一些交互效果,如上下滚动网页内容、缩放图片等。

在JS中可以通过addEventListener方法来监听滚轮事件,代码如下:

let scrollFunc = function(e){
    e = e || window.event;
    if (e.wheelDelta){//判断浏览器IE,谷歌滑轮事件               
        if (e.wheelDelta > 0) { 
            //当滑轮向上滚动时             
        }              
        if (e.wheelDelta < 0) { 
            //当滑轮向下滚动时             
        }              
    } else if (e.detail){//Firefox滑轮事件 
        if (e.detail> 0) { 
            //当滑轮向下滚动时 
        } 
        if (e.detail< 0) { 
            //当滑轮向上滚动时 
        } 
    } 
}
//给页面绑定滑轮滚动事件
if(document.addEventListener){
    document.addEventListener('DOMMouseScroll',scrollFunc,false);
}//W3C
window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome/Safari

二、滚轮滚动控制网页内容

利用滚轮事件控制网页内容滚动是比较常见的交互效果。可以通过监听滚轮事件,判断滚轮滚动的方向,然后对网页内容进行相应的滚动。

实现方法是获取页面的scrollTop属性,然后通过改变scrollTop的值来控制网页滚动。例如,当滑轮向下滚动时,将scrollTop的值加上一个固定值,当滑轮向上滚动时,将scrollTop的值减去一个固定值。

代码示例:

let scrollStep = 50;
let scrollDelta = 0;

function scrollPage(delta){
    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    scrollTop -= delta > 0 ? scrollStep : -scrollStep;
    if(scrollTop < 0) scrollTop = 0;
    document.documentElement.scrollTop = document.body.scrollTop = scrollTop;
}

let scrollFunc = function(e){
    e = e || window.event;
    if (e.wheelDelta){
        scrollDelta = e.wheelDelta;
    } else if (e.detail){
        scrollDelta = e.detail * -1;
    }
    if (scrollDelta){
        scrollPage(scrollDelta);
    }
    e.preventDefault && e.preventDefault();
    e.returnValue = false;
}

if (document.addEventListener){
    document.addEventListener('DOMMouseScroll', scrollFunc, false);
}
window.onmousewheel = document.onmousewheel = scrollFunc;

三、滚轮控制动画效果

除了控制网页滚动,滚轮事件还可以用来实现一些动画效果。例如,当用户滑动滚轮时,图片可以做出相应的缩放或移动效果。

实现方法是通过CSS3的transform属性来控制元素的缩放、旋转等效果,同时通过监听滚轮事件,动态改变transform属性的值达到动画效果的实现。

代码示例:

let scale = 1;
let scaleStep = 0.1;

function zoomPage(delta){
    scale += delta > 0 ? -scaleStep : scaleStep;
    if(scale < 0.5) scale = 0.5;
    document.body.style.transform = 'scale(' + scale + ')';
}

let scrollFunc = function(e){
    e = e || window.event;
    let delta = e.wheelDelta || e.detail * -1;
    if (delta){
        zoomPage(delta);
    }
    e.preventDefault && e.preventDefault();
    e.returnValue = false;
}

if (document.addEventListener){
    document.addEventListener('DOMMouseScroll', scrollFunc, false);
}
window.onmousewheel = document.onmousewheel = scrollFunc;

四、总结

通过对滚轮事件的使用,可以实现一些很有创意的交互效果,让用户的体验更加流畅。在实际开发过程中,开发人员需要根据具体需求来选择对应的实现方式,并且需要注意兼容性的处理。

利用滚轮控制网页内容,让用户体验更流畅

2023-05-16
Android Wheelview:构建流畅的自定义滚轮控件

一、WheelView是什么? WheelView是一个自定义滚轮控件,用于替代Android原生的滚轮控件。它可以让用户更加直观、流畅地进行滚动选择操作。WheelView支持多种样式、自定义滚轮内

2023-12-08
让您的Android App滚动得更流畅

2023-05-14
UC下一页:提高用户体验的必备利器

2023-05-21
Banner轮播图:优化网站视觉效果和用户体验的利器

2023-05-21
如何提高网页滚动的用户体验

2023-05-17
jsfocus方法:如何让你的网站更具吸引力和用户体验

2023-05-17
让你的Android应用界面更流畅:使用ViewPager2

2023-05-14
优化用户体验,实现界面滚动--Python Scroolba

2023-05-13
让你的Android设备拥有更流畅的性能体验

2023-05-14
Webkit技术提升页面效果,让用户体验更佳

2023-05-17
提升网页交互体验,让您的网站更生动:学习使用React动画

2023-05-19
美化Web页面滚动条,让用户界面更流畅

2023-05-17
提高网站用户体验,使用swiper轮播插件

2023-05-23
Vue实现侧滑无缝滚动,提升用户体验

2023-05-16
如何让你的网页凭借内容和用户体验获得更高的点击率?

2023-05-20
如何利用CSS的padding-bottom属性提升您网页的

2023-05-12
提高用户体验的Android选择器控件——Pickervie

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