利用JQuery+EasyDrag 實現(xiàn)彈出可拖動的Div,同時向Div傳值,然后返回Div選中的值
原來我們要寫一個客戶端的特效,要寫一兩天的JavaScript,然后再調試一兩天,才可以看見端倪?,F(xiàn)在我們只要使用JQuery和他的 plugin,就可以任意的實現(xiàn)我們腦海中的特效,感謝他們的編寫者對人類的貢獻(一百個西紅柿砸過來。。。。。。。。。。。。。。)。
我今天實現(xiàn)的需求是一個需要從列表頁面中選擇要導出到word中的列,然后在將選中列的內容導出到word中,同時為了增加通用性,列的個數(shù)不是固定的,也就是說這張表格可能是4列,也可能是5列,待選擇的列數(shù)目不固定。例如:有下面的一張表格,然后我們要打印除薪水外的其他列?! ?/P>
姓名 |
年齡 |
性別 |
薪水 |
張三 | 19 | 男 | 10000 |
張三 | 19 | 男 | 10000 |
張三 | 19 | 男 | 10000 |
我的設計是先用后臺代碼循環(huán)這個表格的表頭,組成下面的字符串
1-Name--2-Age--3-Sex--4-Salary,將這個字符串存儲在hiddenfield中,然后由JavaScript讀取,動態(tài)在彈出Div中添加checkbox對應的html,
然后在選擇之后將選擇的值組成對應的字符串,例如:選擇Name、Age、Sex,就組成,1-Name--2-Age--3Sex,存放在另外的一個hiddenfield中,在后臺代碼讀取這個選中的字符串,將表格中相應的列導出到word中。
同時為了使這個彈出頁面可以拖動,使用了EasyDrag jQuery Plugin,可以從http://fromvega.com/wordpress/2007/07/14/easydrag-jquery-plugin/下載。
這個插件很好用,也很簡單,
實現(xiàn)拖動效果.
$(document).ready( function()
{
$("#divPanel").easydrag();
}
);
Html 代碼
<div id="divPanel" style="width:300px;height:300px;background:white;border:1px solid #000000;position:absolute;left:5px;top:50px" >
<div id="divTitle" style="width:100%;height:25px;background:lavender">
Title
</div>
<div style="width:100%">
</div>
</div>
EasyDrag還可以指定可拖動的區(qū)域,比如只能通過標題拖動整個div,我們JS可以這樣寫
$(document).ready ( function()
{
$("#divPanel").easydrag();
$("#divPanel").setHandler("divTitle");
}
);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
<style type="text/css">
.pop-box {
z-index: 9999;
margin-bottom:3px;
display:none;
position:absolute;
background:#ffffff;
border:solid 1px #6e8bde;
}
.pop-box h4{
color:#ffffff;
cursor:default;
height:18px;
font-size:14px;
font-weight:bold;
text-align:left;
padding-left:8px;
padding-top:4px;
padding-bottom:2px;
}
.pop-box-body{
clear:both;
margin:4px;
padding:2px;
}
</style>
<script type="text/javascript" src="script/jquery.js">
</script>
<script type="text/javascript" src="script/jquery.easydrag.js"></script>
<script typ="text/javascript">
$(document).ready(function(){
var text = "1-Name--2-Age--3-Sex--4-Salary";
var tokenGroup = new Array();
tokenGroup = text.split("--");
$(".optionDiv").append("<fieldset class='fieldset1'><legend class='legend1'>閫夋嫨瑕佸鍑哄埌Word涓殑鍒?/legend></fieldset>");
var obj = new Object();
for (obj in tokenGroup) {
//alert(obj);
//alert(Number(obj));
var index = Number(obj) + 1;
//alert(index);
var token = new Object();
token.value = tokenGroup[obj].split("-")[0];
token.text = tokenGroup[obj].split("-")[1];
//alert("value:"+token.value+" text:"+token.text);
if (index == 1) {
$(".legend1").after("<input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /><label for='Checkbox" +
index.toString() +
"'>" +
token.text +
"</label><br>");
}
else {
$(".fieldset1").append("<input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /><label for='Checkbox" +
index.toString() +
"'>" +
token.text +
"</label><br>");
}
}
});
$(document).ready(function(){
$(".btnSelect").click(function(){
var select = "";
$(".fieldset1 input").each(function(i){
if (this.checked) {
if (select == "")
select = (i + 1).toString() + "-" + $(this).next().text();
else
select += "--" + (i + 1).toString() + "-" + $(this).next().text();
}
});
$(".selected").val(select);
});
$("#btnClose").click(function(){
var select = "";
$(".fieldset1 input").each(function(i){
if (this.checked) {
if (select == "")
select = (i + 1).toString() + "-" + $(this).next().text();
else
select += "--" + (i + 1).toString() + "-" + $(this).next().text();
}
});
$(".selected").val(select);
});
});
$(document).ready(function(){
$(".pop-box").easydrag();
});
function loadText(){
var text = $(".hiddenfield1").val();
var tokenGroup = new Array();
tokenGroup = text.split("--");
$(".pop-box-body").html("");
$(".pop-box-body").append("<fieldset class='fieldset1'><legend class='legend1'>閫夋嫨瑕佸鍑哄埌Word涓殑鍒?/legend></fieldset>");
var obj = new Object();
for (obj in tokenGroup) {
//alert(obj);
//alert(Number(obj));
var index = Number(obj) + 1;
//alert(index);
var token = new Object();
token.value = tokenGroup[obj].split("-")[0];
token.text = tokenGroup[obj].split("-")[1];
//alert("value:"+token.value+" text:"+token.text);
if (index == 1) {
$(".legend1").after("<input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /><label for='Checkbox" +
index.toString() +
"'>" +
token.text +
"</label><br>");
}
else {
$(".fieldset1").append("<input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /><label for='Checkbox" +
index.toString() +
"'>" +
token.text +
"</label><br>");
}
}
}
function popupDiv(div_id){
var div_obj=$("#"+div_id);
var windowWidth=document.documentElement.clientWidth;
var windowHeight=document.documentElement.clientHeight;
var popupHeight=div_obj.height();
var popupWidth=div_obj.width();
$("<div id='mask'></div>").addClass("mask").width(windowWidth*0.99)
.height(windowHeight*0.99).click(function(){
hideDiv(div_id);
}).appendTo("body").fadeIn(200);
div_obj.css({"position":"absolute"})
.animate({left:windowWidth/2-popupWidth/2,top:windowHeight/2-popupHeight/2,opacity:"show"},"show");
loadText();
}
function hideDiv(div_id){
$("#mask").remove();
$("#"+div_id).animate({left:0,top:0,opacity:"hide"},"slow");
}
</script>
</head>
<body>
<h1>New Web Project Page</h1>
<input class="hiddenfield1" type="hidden" value="1-Name--2-Age--3-Sex--4-Salary">
<input type="button" id="btnPopup" name="btnPopup" onclick="popupDiv('pop-div')" class="btnPopup" value="PopupDiv">
<div class="pop-box" style="width:300px" id="pop-div">
<h4>Title</h4>
<div class="pop-box-body">
<p></p>
</div>
<div class="butonPanel" style="text-align:right;">
<input value="Close" id="btnClose" onclick="hideDiv('pop-div');" type="button">
</div>
</div>
<!--<div class="optionDiv"></div>-->
<fieldset>
<legend>
</legend>
</fieldset>
<input type="button" id="button1" name="button1" class="btnSelect" value="selected">
<br>
<textarea class="selected" rows="5" cols="50">
</textarea>
</body>
</html>
源代碼下載
- jquery div拖動效果示例代碼
- jQuery拖動div、移動div、彈出層實現(xiàn)原理及示例
- jQuery實現(xiàn)單擊彈出Div層窗口效果(可關閉可拖動)
- jQuery 可以拖動的div實現(xiàn)代碼 腳本之家修正版
- jQuery實現(xiàn)DIV層淡入淡出拖動特效的方法
- jquery拖動改變div大小
- jQuery實現(xiàn)的鼠標拖動浮層功能示例【拖動div等任何標簽】
- jquery實現(xiàn)可拖動DIV自定義保存到數(shù)據(jù)的實例
- jQuery實現(xiàn)Div拖動+鍵盤控制綜合效果的方法
- jQuery動態(tài)添加可拖動元素完整實例(附demo源碼下載)
- jQuery拖動元素并對元素進行重新排序
- jquery實現(xiàn)兩個div中的元素相互拖動的方法分析
相關文章
基于jQuery的固定表格頭部的代碼(IE6,7,8測試通過)
目前只能算個不完整的腳本,不過一般的僅僅需要表頭凍結就可以使用了2010-05-05jQuery Animation實現(xiàn)CSS3動畫示例介紹
jQuery Animation的工作原理是通過將元素的CSS樣式從一個狀態(tài)改變?yōu)榱硪粋€狀態(tài),下面以一個實例為大家詳細介紹下具體的實現(xiàn),感興趣的朋友可以參考下2013-08-08jQuery實現(xiàn)返回頂部功能適合不支持js的瀏覽器
a標簽指向錨點top,可以在頂部防止一個a name=top的錨點,這樣在瀏覽器不支持js時也可以實現(xiàn)返回頂部的效果了2014-08-08