JavaScript/jQuery 插件 : snap to grid

标签 javascript jquery jquery-ui jquery-plugins

尝试为 jQuery-select-areas plugin 添加“对齐网格”功能.

Plugin's GitHubPlugin's Demo

one similar plugin ,具有我正在寻找的功能,但它是为 jQueryUI 构建的。老实说,我不确定如何将它们联系起来。 感谢任何想法。

谢谢!

最佳答案

那个插件有一个 changing event您可以收听,事件处理程序将传递给创建的区域。您可以通过更改区域对象的宽度、高度、x 和 y 属性来操纵区域的值。

$("#example").selectAreas({
  onChanging: function(event,currentAreaId,areas){
    //use filter on areas to get currently changing area
    var area = areas.filter(area=>area.id==currentAreaId)[0];
    area.width = 50; //set value to nearest grid interval
    area.height = 50; //set value to nearest grid interval
  }
});

您可以使用以下语句计算最近的网格间隔:

Math.round( value / intervalAmount ) * intervalAmount

演示

function nearestInterval(interval,value){ 
  return Math.round(value / interval)*interval; 
}

$("#example").selectAreas({
  minSize: [30, 30],
  maxSize: [400, 300],
  onChanging: function(event,id,areas){
    var area = areas.filter(area=>area.id==id)[0];
    area.width = nearestInterval( 30, area.width );
    area.height = nearestInterval( 30, area.height );
    area.x = nearestInterval( 30, area.x );
    area.y = nearestInterval( 30, area.y );
  }
});
<link rel="stylesheet" href="https://rawgit.com/360Learning/jquery-select-areas/master/resources/jquery.selectareas.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/360Learning/jquery-select-areas/master/jquery.selectareas.js"></script>

<img id="example" src="https://placebear.com/g/400/250" />

关于JavaScript/jQuery 插件 : snap to grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391098/

相关文章:

javascript - jQuery UI 图标问题 - 我如何只显示图像

css - 为您的整个网站使用 jQuery 主题是错误的吗?

javascript - 如何在ajax javascript中以json格式将表单数据发送到服务器

javascript - 用 jquery 停止 youtube 视频?

javascript - 使用jade导入jQuery(使用node.js + Express)

jquery - Colorbox 使灯箱在滚动时固定

javascript - PhantomJs - 从 page.evaluate + ajax 中获取值(value)(同步版本)

jquery-ui - jQuery UI Sortable,如何确定更新事件中的当前位置和新位置?

javascript - ES6 : React destructure immutable state with and without spread

javascript - React useScript hook 如何处理多个调用