本文实例讲述了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="/UploadFiles/2021-04-02/drag.js">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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。