jQuery动态添加可拖动元素完整实例(附demo源码下载) - Web前端
作者:98wpeu发布时间:2026-08-22分类:网页前端技术浏览:8
导读:本文实例讲述了JQuery动态添加可拖动元素的方法。分享给大家供大家参考,具体如下:运行效果截图如下:具体代码如下:index.html:<!DOCtypeH...
本文实例讲述了JQuery动态添加可拖动元素的方法。分享给大家供大家参考,具体如下:
运行效果截图如下:

具体代码如下:
index.html:
<!DOCtype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta HTTP-eqUIv="Content-Type" content="text/html; charset=utf-8"> <title>流程设计器</title> <script type = "text/javascript" src = "jQuery-1.7.2.min.JS"> </script> <script type = "text/JavaScript" src="drag.js"></script> <link type = "text/CSS" href = "PRocessdesigner.css" rel="StyleSheet"></link> </head> <body> <div id = "console"> <div id = "menubar"> <input type = "button" value = "添加节点" hidefocus = "true" id = "addItem"/> </div> <div id = "nodesContAIner"></div> </div> </body> </html>
ProcessDesigner.css:
body {
padding:0;
margin:0
}
#console{
width:500px;
height:300px;
background:#eee;
margin:10px auto;
border:5px solid #000;
}
#menubar{
width:100%;
height:30px;
background:#333;
line-height:30px;
vertical-align:middle;
}
#addItem{
wdith:50px;
height:20px;
color:#fff;
background:#555;
border:0;
line-height:20px;
margin-left:5px;
border-radius:5px;
_margin-top:4px;
}
#nodesContainer{
width:100%;
height:270px;
background:#eee;
}
drag.js:
/**
* @author Administrator
*/
$(function(){
$("#addItem").click(function(){
var obj = document.getElementById("nodesContainer");
createNode(obj);
})
})
function createNode(parentNode){
var left = document.getElementById("nodesContainer").offsetLeft+10;
var top = document.getElementById("nodesContainer").offsetTop+10;
var newNode = document.createElement("div");
newNode.style.position = "absolute";
newNode.style.width = "20px";
newNode.style.height = "20px";
newNode.style.top = top+"px";
newNode.style.left = left+"px";
newNode.style.borderRadius = "50px";
newNode.style.background = "blue";
parentNode.appendChild(newNode);
doDrag(newNode);
}
/*
* @param {Object} obj: If obj is a string,convert it to an obj;
* @param {number} initWidth: Initial Width of obj;
* @param {number} initHeight:Initial Height of obj;
* @param {number} initLeft:Initial Left of obj;
* @param {number} initTop:Initial Top of obj;
*/
function doDrag(obj, initWidth, initHeight, initLeft, initTop){
var posX;
var posY;
var dragable;
if (typeof obj == "String")
obj = document.getElementById(obj);
if(initWidth)obj.style.width = initWidth + "px";
if(initHeight)obj.style.height = initHeight + "px";
if(initLeft)obj.style.left = initLeft + "px";
if(initTop)obj.style.top = initTop + "px";
obj.onmousedown = function(event){
down(event);
}
obj.onmouseup = function(){
up();
}
function down(e){
e = e || window.event;
posX = e.clientX - obj.offsetLeft; //offsetLeft is a readonly property
posY = e.clientY - obj.offsetTop;
dragable = true;
document.onmouSEMove = move;
//$(obj).wrap("<div style = 'position:relative;border:1px solid red;width:300px;height:50px'></div>")
}
function move(ev){
if (dragable) {
ev = ev || window.event;//如果是IE
obj.style.left = (ev.clientX - posX) + "px";
obj.style.top = (ev.clientY - posY) + "px";
}
}
function up(){
//$(obj).unwrap();
dragable = false;
};
}
完整实例代码点击此处本站下载。
更多关于jquery相关内容感兴趣的读者可查看本站专题:《jQuery拖拽特效与技巧总结》、《jQuery表格(table)操作技巧汇总》、《jQuery常用插件及用法总结》、《jquery中Ajax用法总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》
希望本文所述对大家jquery程序设计有所帮助。
相关推荐
- jquery 属性选择器(匹配具有指定属性的元素) - Web前端
- jQuery实现点击弹出背景变暗遮罩效果实例代码 - Web前端
- jQuery中text() val()和html()的区别实例详解 - Web前端
- Jquery 自定义事件实现发布/订阅的简单实例 - Web前端
- jquery实现表格中点击相应行变色功能效果【实例代码】 - Web前端
- jquery实现表格中点击相应行变色功能效果【实例代码】 - Web前端
- jquery实现一个简单的表单验证实例 - Web前端
- jQuery siblings()用法实例详解 - Web前端
- jQuery实现div随意拖动的实例代码(通用代码) - Web前端
- 基于jQuery实现返回顶部实例代码 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 9在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


