您的位置:

CSS movebox 实现元素拖拽效果

一、拖拽效果的实现原理

CSS movebox 实现元素拖拽效果,最基本的原理是利用鼠标事件mousemove、mousedown、mouseup 的监听,以及元素的position属性,实现元素的位置跟随鼠标移动,从而完成拖拽效果。

首先需要获取到拖拽元素的位置,鼠标点击元素并移动,元素需要跟随鼠标的移动而进行位置的变化,最后鼠标松开后,移动事件解除,并记录下元素的新位置。

在实现过程中,可以使用JavaScript或jQuery来监听鼠标事件,并通过改变元素的CSS属性来实现位置变化,下面是一个基本的实现示例代码:

  
<script> var movebox = document.getElementById("movebox"); var isMouseDown = false; var offsetX = 0; var offsetY = 0; movebox.addEventListener("mousedown", function(e){ isMouseDown = true; offsetX = e.offsetX; offsetY = e.offsetY; }); document.addEventListener("mousemove", function(e){ if(isMouseDown){ movebox.style.left = e.pageX - offsetX + "px"; movebox.style.top = e.pageY - offsetY + "px"; } }); document.addEventListener("mouseup", function(e){ isMouseDown = false; }); </script>

二、拖拽元素的限制范围

在实际应用中,有时需要对拖拽元素的移动范围进行限制,避免元素越出容器范围。可以通过对元素的位置进行判断,并在一定条件下限定元素的移动范围,下面是一个简单的代码示例:

  
<script> var container = document.getElementById("container"); var movebox = document.getElementById("movebox"); var isMouseDown = false; var offsetX = 0; var offsetY = 0; movebox.addEventListener("mousedown", function(e){ isMouseDown = true; offsetX = e.offsetX; offsetY = e.offsetY; }); document.addEventListener("mousemove", function(e){ if(isMouseDown){ var boxTop = e.pageY - offsetY - container.offsetTop; var boxLeft = e.pageX - offsetX - container.offsetLeft; if(boxTop < 0){ boxTop = 0; } else if(boxTop + movebox.offsetHeight > container.offsetHeight){ boxTop = container.offsetHeight - movebox.offsetHeight; } if(boxLeft < 0){ boxLeft = 0; } else if(boxLeft + movebox.offsetWidth > container.offsetWidth){ boxLeft = container.offsetWidth - movebox.offsetWidth; } movebox.style.left = boxLeft + "px"; movebox.style.top = boxTop + "px"; } }); document.addEventListener("mouseup", function(e){ isMouseDown = false; }); </script>

三、拖拽元素的动画效果

除了基本的拖拽效果,在实际应用中通常会加上拖拽元素的动画效果,可以通过CSS3的transition属性来实现平滑的移动过程。下面是一个简单的代码示例:

  
<script> var container = document.getElementById("container"); var movebox = document.getElementById("movebox"); var isMouseDown = false; var offsetX = 0; var offsetY = 0; movebox.addEventListener("mousedown", function(e){ isMouseDown = true; offsetX = e.offsetX; offsetY = e.offsetY; }); document.addEventListener("mousemove", function(e){ if(isMouseDown){ var boxTop = e.pageY - offsetY - container.offsetTop; var boxLeft = e.pageX - offsetX - container.offsetLeft; if(boxTop < 0){ boxTop = 0; } else if(boxTop + movebox.offsetHeight > container.offsetHeight){ boxTop = container.offsetHeight - movebox.offsetHeight; } if(boxLeft < 0){ boxLeft = 0; } else if(boxLeft + movebox.offsetWidth > container.offsetWidth){ boxLeft = container.offsetWidth - movebox.offsetWidth; } movebox.style.left = boxLeft + "px"; movebox.style.top = boxTop + "px"; } }); document.addEventListener("mouseup", function(e){ isMouseDown = false; }); </script>

四、拖拽元素的兼容性处理

拖拽效果在不同浏览器之间的兼容性问题通常体现在鼠标事件的监听上,需要对不同浏览器的事件兼容性进行处理。可以使用jQuery库的方法,使用兼容性更高的API进行事件绑定,或是使用特定的库来解决兼容性问题,下面是一个使用jQuery的示例代码:

  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> var container = $("#container"); var movebox = $("#movebox"); var isMouseDown = false; var offsetX = 0; var offsetY = 0; movebox.on("mousedown", function(e){ isMouseDown = true; offsetX = e.offsetX; offsetY = e.offsetY; }); $(document).on("mousemove", function(e){ if(isMouseDown){ var boxTop = e.pageY - offsetY - container.offset().top; var boxLeft = e.pageX - offsetX - container.offset().left; if(boxTop < 0){ boxTop = 0; } else if(boxTop + movebox.outerHeight() > container.outerHeight()){ boxTop = container.outerHeight() - movebox.outerHeight(); } if(boxLeft < 0){ boxLeft = 0; } else if(boxLeft + movebox.outerWidth() > container.outerWidth()){ boxLeft = container.outerWidth() - movebox.outerWidth(); } movebox.css({left: boxLeft + "px", top: boxTop + "px"}); } }); $(document).on("mouseup", function(e){ isMouseDown = false; }); </script>

五、总结

CSS movebox 实现元素拖拽效果,实现起来并不困难,依赖于鼠标事件和CSS的position属性来实现位置移动。在实际应用中,可以根据需求在基本的拖拽效果上添加各种特效,实现更好的用户体验,同时需要对不同浏览器的兼容性问题进行一定的处理。

CSS movebox 实现元素拖拽效果

2023-05-12
CSS和JS实现元素移动

2023-05-12
CSS和JavaScript实现元素的移动效果

2023-05-12
js拖拽鼠标cursor(鼠标左键拖拽)

本文目录一览: 1、js鼠标拖动div 2、javascript 拖拽移动滚动条 3、js中如何拖动DIV中的图片? 4、js中sortable怎么获取拖动的东西 5、js 自定义的cursor在鼠标

2023-12-08
jsdraggable:轻松实现拖拽效果

2023-05-20
CSS禁止图片拖动

2023-05-19
CSS hover cursor

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)