gpt4 book ai didi

javascript - 如何使用 jquery-ui 使可拖动的 div 可调整大小

转载 作者:行者123 更新时间:2023-11-30 14:46:55 25 4
gpt4 key购买 nike

我创建了几个小部件,它们应该可以拖动在屏幕上的任何地方并且可以由用户调整大小。我设法使它们可拖动,但无法使它们可调整大小。这是 fiddle :

https://jsfiddle.net/vaxobasilidze/reL880g8/

我做错了什么?我还有其他元素,我通过相同的方式使其可拖动和调整大小,但由于未知原因,这不适用于此小部件。

function foo(){
$('.channel').each(function() {
$(this).draggable({
containment: $('.screens'),
stack: '.channel',
drop: function (event, ui) {

},
drag: function(){
var draggedItemId = $(this).attr('id');
},
stop: function(){
var draggedItemId = $(this).attr('id');
}
}).resizable();
});
}

function makeDraggable(){
var height = $('.channels').height();
$('.ram').draggable({
stack: '.ram',
}).resizable();
}

makeDraggable();
foo();
html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #2c2c2c;
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzJjMmMyYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjM3JSIgc3RvcC1jb2xvcj0iIzNkM2QzZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMxMzEzMTMiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, #2c2c2c), color-stop(37%, #3d3d3d), color-stop(66%, #353535), color-stop(66%, #353535), color-stop(100%, #131313));
background: -webkit-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -o-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -ms-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: linear-gradient(to right, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2c2c2c', endColorstr='#131313', GradientType=1);
}

body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
color: #b8c0c8;
background: rgba(0, 0, 0, 0.3);
}

.ram {
width: 250px;
height: 140px;
min-width: 250px;
min-height: 140px;
border: none;
background: none;
box-shadow: none;
resize: none;
padding: 7px;
border: 1px solid grey;
}

.ram:hover {
resize: both;
}

#ramUsageContainer {
width: 100%;
height: 100%;
position: relative;
}

#ramUsageContainer svg {
-webkit-filter: drop-shadow(0 0 3px #647384 );
filter: drop-shadow(0 0 3px #647384 );
}

.usageHeader {
width: 100%;
text-align: center;
font-size: 24px;
position: absolute;
top: 25%;
}

.usagePercent {
width: 100%;
text-align: center;
font-size: 30px;
position: absolute;
top: 40%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head runat="server">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.1.8/jsPlumb.js"></script>
<body>
<div class="channel elementTable widget ui-draggable ui-draggable-handle Usage ram widgetDropped ui-resizable" name="RAM" id="2" style="left: 564px; top: 284px; z-index: 1003;" oncontextmenu="return false;">
<div id="ramUsageContainer">
<p class="usageHeader">RAM Usage</p>
<p class="usagePercent">65.7%</p>
<svg viewBox="0 0 100 50">
<path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="#353740" stroke-width="6" fill-opacity="0"></path>
<path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="rgb(167,87,0)" stroke-width="6" fill-opacity="0" style="stroke-dasharray: 147.708, 147.708; stroke-dashoffset: 50.6639;"></path>
</svg>
</div>
</div>
</body>

最佳答案

function foo(){
$('.channel').each(function() {
$(this).resizable().draggable({
containment: $('.screens'),
stack: '.channel',
drop: function (event, ui) {

},
drag: function(){
var draggedItemId = $(this).attr('id');
},
stop: function(){
var draggedItemId = $(this).attr('id');
}
});
});
}

function makeDraggable(){
var height = $('.channels').height();
$('.ram').resizable().draggable({
stack: '.ram',
});
}

makeDraggable();
foo();
html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #2c2c2c;
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzJjMmMyYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjM3JSIgc3RvcC1jb2xvcj0iIzNkM2QzZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMxMzEzMTMiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, #2c2c2c), color-stop(37%, #3d3d3d), color-stop(66%, #353535), color-stop(66%, #353535), color-stop(100%, #131313));
background: -webkit-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -o-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: -ms-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
background: linear-gradient(to right, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2c2c2c', endColorstr='#131313', GradientType=1);
}

body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
color: #b8c0c8;
background: rgba(0, 0, 0, 0.3);
}

.ram {
width: 250px;
height: 140px;
min-width: 250px;
min-height: 140px;
border: none;
background: none;
box-shadow: none;
resize: none;
padding: 7px;
border: 1px solid grey;
}

.ram:hover {
resize: both;
}

#ramUsageContainer {
width: 100%;
height: 100%;
position: relative;
}

#ramUsageContainer svg {
-webkit-filter: drop-shadow(0 0 3px #647384 );
filter: drop-shadow(0 0 3px #647384 );
}

.usageHeader {
width: 100%;
text-align: center;
font-size: 24px;
position: absolute;
top: 25%;
}

.usagePercent {
width: 100%;
text-align: center;
font-size: 30px;
position: absolute;
top: 40%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head runat="server">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.1.8/jsPlumb.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"/>

<body>
<div class="channel elementTable widget ui-draggable ui-draggable-handle Usage ram widgetDropped ui-resizable" name="RAM" id="2" style="left: 564px; top: 284px; z-index: 1003;" oncontextmenu="return false;">
<div class="ui-resizable-handle ui-resizable-e"></div>
<div class="ui-resizable-handle ui-resizable-s"></div>
<div id="ramUsageContainer">
<p class="usageHeader">RAM Usage</p>
<p class="usagePercent">65.7%</p>
<svg viewBox="0 0 100 50">
<path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="#353740" stroke-width="6" fill-opacity="0"></path>
<path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="rgb(167,87,0)" stroke-width="6" fill-opacity="0" style="stroke-dasharray: 147.708, 147.708; stroke-dashoffset: 50.6639;"></path>
</svg>
</div>
</div>
</body>

另一个更新是先添加 resizable 然后调用 draggable 就像 $(this).resizable().draggable() 我这样做了更改为代码。您现在可以使用相同的代码。

关于javascript - 如何使用 jquery-ui 使可拖动的 div 可调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48767280/

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