【发布时间】:2014-02-26 20:43:29
【问题描述】:
有一些关于如何使用 JQuery 制作可调整大小的 div 的帖子。我正在使用 asp.net 并使用了几篇文章中的代码来执行此操作,这将在下面附加。 我现在需要做的是有一个按钮,当单击它时,我会得到另一个克隆的可拖动和可调整大小的 div,我还需要捕获创建的每个 div 的 x、y 宽度和高度。下面的代码为硬编码捕获这些值,并在一系列 asp.net 文本框中显示值。
为任何帮助和建议干杯
问候 厘米
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs"
Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cloned Divs</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-
ui.css" />
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#drag {
width: 6em;
height: 6em;
padding: 0.5em;
border: 3px solid #ccc;
border-radius: 0 1em 1em 1em;
background-color: #fff;
background-color: rgba(255,255,255,0.5);
}
#Buttons {
width: 50px;
height: 50px;
left: 50px;
position: absolute;
}
</style>
<script>
$(function () {
$("#drag").draggable(
{
containment: $('body'),
drag: function () {
var offset = $(this).offset();
var xPos = offset.left;
var yPos = offset.top;
var myHeight = $("#draggable").height();
$('#posX').text('x: ' + xPos);
$('#posY').text('y: ' + yPos);
$("#<%=txtX.ClientID%>").val(xPos);
$("#<%=txtY.ClientID%>").val(yPos);
}
}).resizable({
resize: function (event, ui) {
var mywidth = $(event.target).width();
var myHeight = $(event.target).height();
$('#width').text('width: ' + mywidth);
$('#height').text('height: ' + myHeight);
$("#<%=txtWidth.ClientID%>").val(mywidth);
$("#<%=txtHeight.ClientID%>").val(myHeight);
}
});
});
$('#btnAdd').click(function () {
var structure = $('<div id="draggable1" class="ui-widget-content"></div>');
$('#body').append(structure);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="drag" class="ui-widget-content">
<ul>
<li id="posX"></li>
<li id="posY"></li>
<li id="width"></li>
<li id="height"></li>
</ul>
</div>
<div id="Buttons">
<asp:Label ID="lblX" runat="server" Text="X: "></asp:Label><asp:TextBox
ID="txtX" runat="server"></asp:TextBox>
<asp:Label ID="lblY" runat="server" Text="Y: "></asp:Label><asp:TextBox
ID="txtY" runat="server"></asp:TextBox>
<asp:Label ID="lblWidth" runat="server" Text="Width: "></asp:Label><asp:TextBox
ID="txtWidth" runat="server"></asp:TextBox>
<asp:Label ID="lblHeight" runat="server" Text="Height: "></asp:Label>
<asp:TextBox ID="txtHeight" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add Div" />
</div>
</form>
</body>
</html>
【问题讨论】:
标签: javascript jquery html asp.net css