利用JQuery+EasyDrag 实现弹出可拖动的Div同时向Div传值

2016-02-19 15:06 16 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的利用JQuery+EasyDrag 实现弹出可拖动的Div同时向Div传值,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - Web开发 】

 

打包下载

原来我们要写一个客户端的特效,要写一两天的JavaScript,然后再调试一两天,才可以看见端倪。现在我们只要使用JQuery和他的 plugin,就可以任意的实现我们脑海中的特效,感谢他们的编写者对人类的贡献(一百个西红柿砸过来。。。。。。。。。。。。。。)。

  我今天实现的需求是一个需要从列表页面中选择要导出到word中的列,然后在将选中列的内容导出到word中,同时为了增加通用性,列的个数不是固定的,也就是说这张表格可能是4列,也可能是5列,待选择的列数目不固定。例如:有下面的一张表格,然后我们要打印除薪水外的其他列。  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

姓名

年龄

性别

薪水

张三19男10000张三19男10000张三19男10000

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

我的设计是先用后台代码循环这个表格的表头,组成下面的字符串

1-Name--2-Age--3-Sex--4-Salary,将这个字符串存储在hiddenfield中,然后由JavaScript读取,动态在弹出Div中添加checkbox对应的html,

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)
然后在选择之后将选择的值组成对应的字符串,例如:选择Name、Age、Sex,就组成,1-Name--2-Age--3Sex,存放在另外的一个hiddenfield中,在后台代码读取这个选中的字符串,将表格中相应的列导出到word中。

同时为了使这个弹出页面可以拖动,使用了EasyDrag jQuery Plugin,可以从http://fromvega.com/wordpress/2007/07/14/easydrag-jquery-plugin/下载。

这个插件很好用,也很简单,

实现拖动效果.

代码如下:
$(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还可以指定可拖动的区域,比如只能通过标题拖动整个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" /
titleNew 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 +
"/labelbr");

}
else {

$(".fieldset1").append("input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /label for='Checkbox" +
index.toString() +
"'" +
token.text +
"/labelbr");
}
}
});
$(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 +
"/labelbr");

}
else {

$(".fieldset1").append("input id='Checkbox" +
index.toString() +
"' value='" +
token.value +
"' type='checkbox' /label for='Checkbox" +
index.toString() +
"'" +
token.text +
"/labelbr");
}
}
}
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
h1New 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"
h4Title/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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1608856.html

延伸阅读
标签: Web开发
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head meta http-equiv="Content-Type" content="text/html; charset=gb2312" / titlejquery 弹出公告/title script type="text/javascript" src="http://ajax.googleapi...
标签: Web开发
这个程序是个用来制作DIV圆角的开源javascript代码实现,效果和图像制作圆角一样的.它简单,易用,不用修改任何图像就能做到不同半径圆角.... 用这个代码你可以自由定制自己的DIV,不再是方形了.完全可以实现圆形个性DIV 用法说明: 以下说明将以一个半径为20像素圆角的DIV为例. ------------------------------A 解压您下...
标签: Web开发
//jQuery Alert Dialogs Plugin Version 1.0 //插件下载地址:http://abeautifulsite.net/notebook/87 自身的原方法为: 代码如下: // Usage: // jAlert( message, [title, callback] ) // jConfirm( message, [title, callback] ) // jPrompt( message, [value, title, callback] ) 1.新加一个multicheckbox 的公共方法: // Public...
标签: Web开发
现在编写xhtml,我们强烈不推荐此种方法,应该是id,class综合应用。此文章只是讲述一种思维,并非建站过程中的方法! 用标准件的方式来组装网页DIV布局。我把class分为2种:布局class;风格class。 布局class是骨架,风格class是衣服。 举个例子:比如布局中的左栏。首先它的属性有:是左栏,宽度,背景颜色,字体颜色等。...
标签: Web开发
经过分析,网上的参考,终于搞定了~~ jQuery插件代码: 代码如下: jQuery.fn.selectItem = function(targetId) { var _seft = this; var targetId = $(targetId); this.toggle( function() { var A_top = $(this).offset().top + $(this).outerHeight(true); // 1 var A_left = $(this).offset().left; targetId.bgiframe(); targ...

经验教程

790

收藏

50
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部