欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery動態(tài)添加可拖動元素完整實例(附demo源碼下載)

 更新時間:2016年06月21日 09:51:57   作者:cherry  
這篇文章主要介紹了jQuery動態(tài)添加可拖動元素的方法,可實現簡單的點擊添加元素,并且添加的元素可進行拖動操作.涉及jQuery響應鼠標事件動態(tài)操作頁面元素的相關技巧,需要的朋友可以參考下

本文實例講述了jQuery動態(tài)添加可拖動元素的方法。分享給大家供大家參考,具體如下:

運行效果截圖如下:

具體代碼如下:

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 = "添加節(jié)點" 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 Ajax.ashx 高效分頁實現代碼

    Jquery Ajax.ashx 高效分頁實現代碼

    Jquery ,大家都熟悉的一個框架,我對Jquery正在學習中,對其影響最深的當屬 它的選擇器之強,ajax與服務器之間的交談
    2009-10-10
  • JQuery中使文本框獲得焦點的方法實例分析

    JQuery中使文本框獲得焦點的方法實例分析

    這篇文章主要介紹了JQuery中使文本框獲得焦點的方法,實例分析了jQuery針對文本框獲得焦點的技巧,需要的朋友可以參考下
    2015-02-02
  • jQuery學習筆記之創(chuàng)建DOM元素

    jQuery學習筆記之創(chuàng)建DOM元素

    這篇文章主要介紹了jQuery學習筆記之創(chuàng)建DOM元素的相關資料,需要的朋友可以參考下
    2015-01-01
  • jquery通過擴展select控件實現支持enter或focus選擇的方法

    jquery通過擴展select控件實現支持enter或focus選擇的方法

    這篇文章主要介紹了jquery通過擴展select控件實現支持enter或focus選擇的方法,通過jQuery針對select空間增加enter及focus選擇功能分析了jQuery擴展的相關實現技巧,需要的朋友可以參考下
    2015-11-11
  • jquery實現標題字體變換的滑動門菜單效果

    jquery實現標題字體變換的滑動門菜單效果

    這篇文章主要介紹了jquery實現標題字體變換的滑動門菜單效果,通過調用自定義函數實現頁面tab切換及字體樣式同步變換的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • jQuery實現掃雷小游戲

    jQuery實現掃雷小游戲

    這篇文章主要為大家詳細介紹了jQuery實現掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • jQuery中first()方法用法實例

    jQuery中first()方法用法實例

    這篇文章主要介紹了jQuery中first()方法用法,實例分析了first()方法的功能、定義及獲取匹配元素集合中的第一個元素時的使用技巧,需要的朋友可以參考下
    2015-01-01
  • JQuery.validate在ie8下不支持的快速解決方法

    JQuery.validate在ie8下不支持的快速解決方法

    下面小編就為大家?guī)硪黄狫Query.validate在ie8下不支持的快速解決方法。小編覺得挺不錯的,現在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2016-05-05
  • jquery操作select方法匯總

    jquery操作select方法匯總

    這篇文章主要介紹了jquery操作select方法,實例匯總了jQuery操作select的讀取、設置、添加及刪除等技巧,需要的朋友可以參考下
    2015-02-02
  • jquery操作select option 的代碼小結

    jquery操作select option 的代碼小結

    jquery操作select option 的代碼小結,需要的朋友可以參考下。
    2011-06-06

最新評論