【问题标题】:JavaScript/jQuery plugin: snap to gridJavaScript/jQuery 插件:对齐网格
【发布时间】:2018-07-17 22:37:43
【问题描述】:

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

Plugin's GitHubPlugin's Demo

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

谢谢!

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-plugins


    【解决方案1】:

    那个插件有一个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" />

    【讨论】:

    • 谢谢你,帕特里克!这很好用。我想现在我知道去哪里找了。
    猜你喜欢
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多