gpt4 book ai didi

javascript - 添加/删除小部件以及调整大小不起作用

转载 作者:行者123 更新时间:2023-12-03 02:23:45 27 4
gpt4 key购买 nike

我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件)

我的js代码如下

<script type="text/javascript">
var gridster;

gridster = $(".gridster ul").gridster({
widget_base_dimensions: [100, 100],
widget_margins: [5, 5],
helper: 'clone',
resize: {
enabled: true
}


}).data('gridster');



$(document).on( "click",".delete-button", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.remove_widget($(this).parent());
});



$(document).on( "click",".add-button", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.add_widget('<li><button class="delete-button" style="float:
right;">-</button><h3></h3></li>',1,1);
});

以上代码不起作用

工作代码中没有这个

resize: {
enabled: true
}

最佳答案

引用解决方案here ,已使用 ressized()

更新了您的代码

还添加了 css 文件 jquery.gridster.css

$(function() {
var gridster;
var grid_size = 100;
var grid_margin = 5;
gridster = $(".gridster ul").gridster({
widget_base_dimensions: [100, 100],
widget_margins: [5, 5],
helper: 'clone',
resize: {
enabled: true
}


}).data('gridster');



$(document).on("click", ".delete-button", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.remove_widget($(this).parent());

});



$(document).on("click", ".add-button", function() {
gridster
.add_widget('<li><button class="delete-button" style="float: right;">-</button><h3></h3></li>', 1, 1);
gridster.resizable({
grid: [grid_size + (grid_margin * 2), grid_size + (grid_margin * 2)],
animate: false,
minWidth: grid_size,
minHeight: grid_size,
containment: '#layouts_grid ul',
autoHide: true,
stop: function(event, ui) {
var resized = $(this);
setTimeout(function() {
resizeBlock(resized);
}, 300);
}
});
});
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.gridster/0.5.6/jquery.gridster.css" rel="stylesheet"/>
<link href="https://dsmorse.github.io/gridster.js/demos/assets/css/demo.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.gridster/0.5.6/jquery.gridster.min.js"></script>
<h1>Add and Remove Widget Dynamically</h1>

<p>When you remove a widget,the widget below it will always remain in the same column.Widget from next column wont come in place of widget removed.Gridster is designed that way</p>
<a href="https://github.com/ducksboard/gridster.js/issues/338" target="_blank"> Click to know more</a>


<div class="gridster">
<ul>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>1</h3>
</li>
<li data-row="1" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>2</h3>
</li>
<li data-row="1" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>3</h3>
</li>
<li data-row="1" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>4</h3>
</li>
<li data-row="2" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>5</h3>
</li>
<li data-row="2" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>6</h3>
</li>
<li data-row="2" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>7</h3>
</li>
<li data-row="2" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>8</h3>
</li>
<li data-row="3" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>9</h3>
</li>
<li data-row="3" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>10</h3>
</li>
<li data-row="3" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>11</h3>
</li>
<li data-row="3" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
<h3>12</h3>
</li>



</div>


<button class="add-button" style="float:top;">Add Widget</button>

关于javascript - 添加/删除小部件以及调整大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047142/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com