花了好长时间整的JS,个性化页面拖曳功能
$(document).ready(function() {
if (typeof(console) == 'undefined') {
console = {
debug : function() {
}
}
}
var cmd = console;
var sDrag = $('#sampleDivForDrag');
var fDrag = $('#fordrag'); // proxy
$("div", $("#lay_tab")).each(function() {
if ($(this).attr("id")) {
$(this).click(function() {
$("div", $("#lay_tab")).each(function() {
$(this).removeClass('on')
});
var id = $(this).attr("id");
if (id == 'lay2') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd2_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay2td' + i);
});
} else if (id == 'lay5') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd4_' + i);
$(this).attr('class', 'c3t' + (i + 1) + ' lay5td' + i);
});
} else if (id == 'lay4') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd4_' + i);
$(this).attr('class', 'c3t' + (i + 1) + ' lay4td' + i);
});
} else if (id == 'lay3') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay3td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
} else if (id == 'lay1') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay1td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
} else if (id == 'lay0') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c1t' + (i + 1) + ' lay0td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
}
$(this).addClass("on");
});
}
});
// return;
var model = $("div[rel]='drag'");
var rels = {};
var tds = {};
cmd.debug(model);
$.each(model, function() {
var tar = $(this);
$(this).find('table:first').each(function() {
$(this).drag("init", function() {
$('#layout >tbody >tr >td:visible').each(function(i) {
var id = $(this).attr('class');
p = $(this).offset();
tds[id] = jQuery.extend(p, {
w : $(this).width(),
h : $(this).height()
});
$(this).css('background-color',i+'f666'+i);
});
$("div[rel]='drag':visible").each(function() {
var id = $(this).attr('id');
p = $(this).offset();
rels[id] = jQuery.extend(p, {
w : $(this).width(),
h : $(this).height()
});
//$(this).append($(this).offset());
});
var p = $(this).offset();
fDrag.css('left', p.left);
fDrag.css('top', p.top);
fDrag.show();
cmd.debug('init');
}).drag("start", function() {
cmd.debug('start');
// fDrag.append($(this).parent());
var w =$(this).parent().width();
fDrag.append($(this).parent().clone());
$(this).parent().hide();
$(this).parent().before(sDrag);
sDrag.show();
fDrag.css('width',w);
return fDrag;
}).drag(function(e, dd) {
$(dd.proxy).css({
top : dd.offsetY,
left : dd.offsetX,
cursor : 'move'
});
var x = e.pageX, y = e.pageY;
// check div
for (dd in rels) {
var d = rels[dd];
if ((d.left < x && x < (d.left + d.w))
&& (d.top < y && y < (d.top + d.h))) {
// cmd.debug(d);
sDrag.remove();
if (y < (d.top+ d.h / 2))
$('#' + dd + '').before(sDrag);
else
$('#' + dd + '').after(sDrag);
return true;
}
}
// check td
for (td in tds) {
var t = tds[td];
if ((t.left < x && x < (t.left + t.w))
&& (t.top < y && y < (t.top + t.h))) {
// cmd.debug(td);
sDrag.remove();
$('td[class=\"' + td + '\"]').append(sDrag);
return true;
}
}
return true;
}).drag("end", function(ev, dd) {
sDrag.hide();
$(document.body).append(sDrag.clone());
var r = $(this).parent().offset();
$(this).css('left', r.left);
$(this).css('top', r.top);
sDrag.replaceWith($(this).parent().show());
fDrag.empty();
fDrag.css({
left : 0,
top : 0
});
});
$(this).css('cursor', 'move');
});
});
cmd.debug(rels);
cmd.debug(tds);
});
$(document).ready(function() {
if (typeof(console) == 'undefined') {
console = {
debug : function() {
}
}
}
var cmd = console;
var sDrag = $('#sampleDivForDrag');
var fDrag = $('#fordrag'); // proxy
$("div", $("#lay_tab")).each(function() {
if ($(this).attr("id")) {
$(this).click(function() {
$("div", $("#lay_tab")).each(function() {
$(this).removeClass('on')
});
var id = $(this).attr("id");
if (id == 'lay2') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd2_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay2td' + i);
});
} else if (id == 'lay5') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd4_' + i);
$(this).attr('class', 'c3t' + (i + 1) + ' lay5td' + i);
});
} else if (id == 'lay4') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd4_' + i);
$(this).attr('class', 'c3t' + (i + 1) + ' lay4td' + i);
});
} else if (id == 'lay3') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay3td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
} else if (id == 'lay1') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c2t' + (i + 1) + ' lay1td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
} else if (id == 'lay0') {
$('#layout >tbody >tr >td').each(function(i) {
$(this).attr('id', 'modtd_' + i);
$(this).attr('class', 'c1t' + (i + 1) + ' lay0td' + i);
});
// $('#modtd_0').addClass('c2t1 lay2td0')
}
$(this).addClass("on");
});
}
});
// return;
var model = $("div[rel]='drag'");
var rels = {};
var tds = {};
cmd.debug(model);
$.each(model, function() {
var tar = $(this);
$(this).find('table:first').each(function() {
$(this).drag("init", function() {
$('#layout >tbody >tr >td:visible').each(function(i) {
var id = $(this).attr('class');
p = $(this).offset();
tds[id] = jQuery.extend(p, {
w : $(this).width(),
h : $(this).height()
});
$(this).css('background-color',i+'f666'+i);
});
$("div[rel]='drag':visible").each(function() {
var id = $(this).attr('id');
p = $(this).offset();
rels[id] = jQuery.extend(p, {
w : $(this).width(),
h : $(this).height()
});
//$(this).append($(this).offset());
});
var p = $(this).offset();
fDrag.css('left', p.left);
fDrag.css('top', p.top);
fDrag.show();
cmd.debug('init');
}).drag("start", function() {
cmd.debug('start');
// fDrag.append($(this).parent());
var w =$(this).parent().width();
fDrag.append($(this).parent().clone());
$(this).parent().hide();
$(this).parent().before(sDrag);
sDrag.show();
fDrag.css('width',w);
return fDrag;
}).drag(function(e, dd) {
$(dd.proxy).css({
top : dd.offsetY,
left : dd.offsetX,
cursor : 'move'
});
var x = e.pageX, y = e.pageY;
// check div
for (dd in rels) {
var d = rels[dd];
if ((d.left < x && x < (d.left + d.w))
&& (d.top < y && y < (d.top + d.h))) {
// cmd.debug(d);
sDrag.remove();
if (y < (d.top+ d.h / 2))
$('#' + dd + '').before(sDrag);
else
$('#' + dd + '').after(sDrag);
return true;
}
}
// check td
for (td in tds) {
var t = tds[td];
if ((t.left < x && x < (t.left + t.w))
&& (t.top < y && y < (t.top + t.h))) {
// cmd.debug(td);
sDrag.remove();
$('td[class=\"' + td + '\"]').append(sDrag);
return true;
}
}
return true;
}).drag("end", function(ev, dd) {
sDrag.hide();
$(document.body).append(sDrag.clone());
var r = $(this).parent().offset();
$(this).css('left', r.left);
$(this).css('top', r.top);
sDrag.replaceWith($(this).parent().show());
fDrag.empty();
fDrag.css({
left : 0,
top : 0
});
});
$(this).css('cursor', 'move');
});
});
cmd.debug(rels);
cmd.debug(tds);
});