jQuery UI中图片可resizable调整大小但无法draggable拖拽是什么原因?
图片拖拽功能失效原因及修复方案
失效核心原因
- 加载了两个不同版本的jQuery,版本冲突导致jQuery UI提供的draggable方法调用异常
- 可拖拽容器嵌套在
.div1内部,若.div1存在溢出隐藏、定位限制等CSS规则,会导致拖拽效果被截断 - 内层图片的resizable事件与外层容器的draggable事件存在冒泡冲突,部分场景下会拦截拖拽触发逻辑
修复后完整代码
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="home.css" > <!-- 仅保留1个兼容jQuery UI的jQuery版本,删除重复加载的3.5.1版本 --> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"/> <style> /* 可选:给拖拽容器加最小宽高限制,避免resizable缩到无法触发拖拽 */ #draggableHelper { display: inline-block; min-width: 50px; min-height: 50px; } #image { width: 100%; height: 100%; } </style> </head> <body> <div class="row"> <!-- 按需求将拖拽容器调整为.row的直接子元素,独立放置 --> <div id="draggableHelper"> <img id="image" src="images/jimslogo2.png"/> </div> <div class="div1"> <h1> About Us </h1> <p class="card-text"> general description </p> </div> <div class="div2"> <span class="dot"> <div> <div id="rotator"></div> </div> <p class="textScale"> <b>Featured Products </b> </p> </span> </div> </div> <script type="text/javascript"> $(document).ready(function(){ // 可选添加containment: '.row' 限制拖拽范围仅在.row容器内 $('#draggableHelper').draggable(); // 添加cancel参数规避事件冲突,同时可按需开启比例锁定 $('#image').resizable({ cancel: false, aspectRatio: true }); }); </script> </body> </html>
内容的提问来源于stack exchange,提问作者Adam Gong
相关产品推荐
相关产品推荐

