通过定义 draggable 区域的边界来约束每个 draggable 的运动。设置 axis 选项来限制 draggable 的路径为 x 轴或者 y 轴
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>draggable</title> <link rel="stylesheet" href="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.css"> <style> .draggable{ width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } #draggable1,#draggable2{ margin-bottom: 20px; } #draggable1{ cursor: e-resize; } #draggable2{ cursor:n-resize; } h3{ clear:left; } </style> </head> <body> <h3>沿着X轴约束运动:</h3> <div id="draggable1" class="draggable ui-widget-content"> <p>只能水平拖拽</p> </div> <h3>沿着Y轴约束运动:</h3> <div id="draggable2" class="draggable ui-widget-content"> <p>只能垂直拖拽</p> </div> <script src="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/external/jquery/jquery.js" type="text/javascript" ></script> <script src="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script> <script> $("#draggable1").draggable({axis:"x"}); $("#draggable2").draggable({axis:"y"}); </script> </body> </html>
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1898394
原文地址:http://suyanzhu.blog.51cto.com/8050189/1898394