以下是 jQuery任意拖动页面中元素代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery任意拖动页面中元素代码</title>
<style type="text/css">
#wrap{
width: 400px;
height: 470px;
margin: 50px auto;
padding-top: 20px;
border: 1px solid #ccc;
overflow: auto;
overflow-x: hidden;
position: relative;
}
ul{
padding: 0;
margin: 0;
}
li{
padding: 10px;
margin-bottom: 20px;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
font: 14px/1.5 "微软雅黑";
list-style: none;
cursor: move;
}
li:hover{
background-color: #f6f6f6;
}
</style>
</head>
<body>
<div id="wrap">
<ul>
<li>
世界上最美好的相遇是恰好,你洽好青春年少,我恰好青春芳华;世界上最唯美的惊艳是偶然,你偶然出现,我偶然发现,然后清纯的两颗心,慢慢地慢慢地靠近;
</li>
<li>
你飘逸的裙裾逶迤在我寂寞里飞马行空的诗行,<br>
跟随你的优雅舒缓的步履,
</li>
<li>
在相濡以沫心心相印的平凡日子里心香如故,<br>
刻写一个个浪漫和感动记忆的满面皱纹。
</li>
<li>
除了怀念,只剩怀念;<br>
除了无言,只能无言!
</li>
<li>一些事情,你愈是去遮掩愈是容易清晰,原本以为的瞒天过海,结果却是欲盖弥彰。</li>
</ul>
</div>
<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="js/ddsort.js"></script>
<script>
$( '#wrap' ).DDSort({
target: 'li',
floatStyle: {
'border': '1px solid #ccc',
'background-color': '#fff'
}
});
</script>
</body>
</html>
JS代码(ddsort.js):
;
(function( $ ){
/** * Author:https://github.com/Barrior * * DDSort:drag and drop sorting. * @param{
Object}
options * target[string]:可选,jQuery事件委托选择器字符串,默认'li' * cloneStyle[object]:可选,设置占位符元素的样式 * floatStyle[object]:可选,设置拖动元素的样式 * down[function]:可选,鼠标按下时执行的函数 * move[function]:可选,鼠标移动时执行的函数 * up[function]:可选,鼠标抬起时执行的函数 */
$.fn.DDSort = function( options ){
var $doc = $( document ),fnEmpty = function(){
}
,settings = $.extend( true,{
down:fnEmpty,move:fnEmpty,up:fnEmpty,target:'li',cloneStyle:{
'background-color':'#eee'}
,floatStyle:{
//用固定定位可以防止定位父级不是Body的情况的兼容处理,表示不兼容IE6,无妨'position':'fixed','box-shadow':'10px 10px 20px 0 #eee','webkitTransform':'rotate(4deg)','mozTransform':'rotate(4deg)','msTransform':'rotate(4deg)','transform':'rotate(4deg)'}
}
,options );
return this.each(function(){
var that = $( this ),height = 'height',width = 'width';
if( that.css( 'box-sizing' ) == 'border-box' ){
height = 'outerHeight';
width = 'outerWidth';
}
that.on( 'mousedown.DDSort',settings.target,function( e ){
//只允许鼠标左键拖动if( e.which != 1 ){
return;
}
//防止表单元素失效var tagName = e.target.tagName.toLowerCase();
if( tagName == 'input' || tagName == 'textarea' || tagName == 'select' ){
return;
}
var THIS = this,$this = $( THIS ),offset = $this.offset(),disX = e.pageX - offset.left,disY = e.pageY - offset.top,clone = $this.clone().css( settings.cloneStyle ).css( 'height',$this[ height ]() ).empty(),hasClone = 1,//缓存计算thisOuterHeight = $this.outerHeight(),thatOuterHeight = that.outerHeight(),//滚动速度upSpeed = thisOuterHeight,downSpeed = thisOuterHeight,maxSpeed = thisOuterHeight * 3;
settings.down.call( THIS );
$doc.on( 'mousemove.DDSort',function( e ){
if( hasClone ){
$this.before( clone ).css( 'width',$this[ width ]() ).css( settings.floatStyle ).appendTo( $this.parent() );
hasClone = 0;
}
var left = e.pageX - disX,top = e.pageY - disY,prev = clone.prev(),next = clone.next().not( $this );
$this.css({
left:left,top:top}
);
//向上排序if( prev.length && top < prev.offset().top + prev.outerHeight()/2 ){
clone.after( prev );
//向下排序}
else if( next.length && top + thisOuterHeight > next.offset().top + next.outerHeight()/2 ){
clone.before( next );
}
/** * 处理滚动条 * that是带着滚动条的元素,这里默认以为that元素是这样的元素(正常情况就是这样),如果使用者事件委托的元素不是这样的元素,那么需要提供接口出来 */
var thatScrollTop = that.scrollTop(),thatOffsetTop = that.offset().top,scrollVal;
//向上滚动if( top < thatOffsetTop ){
downSpeed = thisOuterHeight;
upSpeed = ++upSpeed > maxSpeed ? maxSpeed:upSpeed;
scrollVal = thatScrollTop - upSpeed;
//向下滚动}
else if( top + thisOuterHeight - thatOffsetTop > thatOuterHeight ){
upSpeed = thisOuterHeight;
downSpeed = ++downSpeed > maxSpeed ? maxSpeed:downSpeed;
scrollVal = thatScrollTop + downSpeed;
}
that.scrollTop( scrollVal );
settings.move.call( THIS );
}
).on( 'mouseup.DDSort',function(){
$doc.off( 'mousemove.DDSort mouseup.DDSort' );
//click的时候也会触发mouseup事件,加上判断阻止这种情况if( !hasClone ){
clone.before( $this.removeAttr( 'style' ) ).remove();
settings.up.call( THIS );
}
}
);
return false;
}
);
}
);
}
;
}
)( jQuery );
CSS代码(htmleaf-demo.css):
@font-face{font-family:'icomoon';src:url('../fonts/icomoon.eot?rretjt');src:url('../fonts/icomoon.eot?#iefixrretjt') format('embedded-opentype'),url('../fonts/icomoon.woff?rretjt') format('woff'),url('../fonts/icomoon.ttf?rretjt') format('truetype'),url('../fonts/icomoon.svg?rretjt#icomoon') format('svg');font-weight:normal;font-style:normal;}
[class^="icon-"],[class*=" icon-"]{font-family:'icomoon';speak:none;font-style:normal;font-weight:normal;font-variant:normal;text-transform:none;line-height:1;/* Better Font Rendering =========== */
-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;}
body,html{font-size:100%;padding:0;margin:0;}
/* Reset */
*,*:after,*:before{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
/* Clearfix hack by Nicolas Gallagher:http://nicolasgallagher.com/micro-clearfix-hack/ */
.clearfix:before,.clearfix:after{content:" ";display:table;}
.clearfix:after{clear:both;}
body{font-weight:500;font-size:1.05em;font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;}
a{color:#2fa0ec;outline:none;text-decoration:none;}
a:hover,a:focus{color:#74777b;text-decoration:none;}
.htmleaf-container{margin:0 auto;}
.bgcolor-1{background:#f0efee;}
.bgcolor-2{background:#f9f9f9;}
.bgcolor-3{background:#e8e8e8;}
/*light grey*/
.bgcolor-4{background:#2f3238;color:#fff;}
/*Dark grey*/
.bgcolor-5{background:#df6659;color:#521e18;}
/*pink1*/
.bgcolor-6{background:#2fa8ec;}
/*sky blue*/
.bgcolor-7{background:#d0d6d6;}
/*White tea*/
.bgcolor-8{background:#3d4444;color:#fff;}
/*Dark grey2*/
.bgcolor-9{background:#ef3f52;color:#fff;}
/*pink2*/
.bgcolor-10{background:#64448f;color:#fff;}
/*Violet*/
.bgcolor-11{background:#3755ad;color:#fff;}
/*dark blue*/
.bgcolor-12{background:#3498DB;color:#fff;}
/*light blue*/
.bgcolor-20{background:#494A5F;color:#D5D6E2;}
/* Header */
.htmleaf-header{padding:1em 190px 1em;letter-spacing:-1px;text-align:center;background:#66677c;}
.htmleaf-header h1{color:#D5D6E2;font-weight:600;font-size:2em;line-height:1;margin-bottom:0;font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;}
.htmleaf-header h1 span{font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;display:block;font-size:60%;font-weight:400;padding:0.8em 0 0.5em 0;color:#c3c8cd;}
/*nav*/
.htmleaf-demo a{color:#fff;text-decoration:none;}
.htmleaf-demo{width:100%;padding-bottom:1.2em;}
.htmleaf-demo a{display:inline-block;margin:0.5em;padding:0.6em 1em;border:3px solid #fff;font-weight:700;}
.htmleaf-demo a:hover{opacity:0.6;}
.htmleaf-demo a.current{background:#1d7db1;color:#fff;}
/* Top Navigation Style */
.htmleaf-links{position:relative;display:inline-block;white-space:nowrap;font-size:1.5em;text-align:center;}
.htmleaf-links::after{position:absolute;top:0;left:50%;margin-left:-1px;width:2px;height:100%;background:#dbdbdb;content:'';-webkit-transform:rotate3d(0,0,1,22.5deg);transform:rotate3d(0,0,1,22.5deg);}
.htmleaf-icon{display:inline-block;margin:0.5em;padding:0em 0;width:1.5em;text-decoration:none;}
.htmleaf-icon span{display:none;}
.htmleaf-icon:before{margin:0 5px;text-transform:none;font-weight:normal;font-style:normal;font-variant:normal;font-family:'icomoon';line-height:1;speak:none;-webkit-font-smoothing:antialiased;}
/* footer */
.htmleaf-footer{width:100%;padding-top:10px;}
.htmleaf-small{font-size:0.8em;}
.center{text-align:center;}
/****/
.related{color:#fff;background:#494A5F;text-align:center;font-size:1.25em;padding:0.5em 0;overflow:hidden;}
.related > a{vertical-align:top;width:calc(100% - 20px);max-width:340px;display:inline-block;text-align:center;margin:20px 10px;padding:25px;font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;}
.related a{display:inline-block;text-align:left;margin:20px auto;padding:10px 20px;opacity:0.8;-webkit-transition:opacity 0.3s;transition:opacity 0.3s;-webkit-backface-visibility:hidden;}
.related a:hover,.related a:active{opacity:1;}
.related a img{max-width:100%;opacity:0.8;border-radius:4px;}
.related a:hover img,.related a:active img{opacity:1;}
.related h3{font-family:"Microsoft YaHei",sans-serif;}
.related a h3{font-weight:300;margin-top:0.15em;color:#fff;}
/* icomoon */
.icon-htmleaf-home-outline:before{content:"\e5000";}
.icon-htmleaf-arrow-forward-outline:before{content:"\e5001";}
@media screen and (max-width:1024px){.htmleaf-header{padding:2em 10% 2em;}
.htmleaf-header h1{font-size:1.4em;}
.htmleaf-links{font-size:1.4em}
}
@media screen and (max-width:960px){.htmleaf-header{padding:2em 10% 2em;}
.htmleaf-header h1{font-size:1.2em;}
.htmleaf-links{font-size:1.2em}
.related h3{font-size:1em;}
.related a h3{font-size:0.8em;}
}
@media screen and (max-width:766px){.htmleaf-header h1{font-size:1.3em;}
.htmleaf-links{font-size:1.3em}
}
@media screen and (max-width:640px){.htmleaf-header{padding:2em 10% 2em;}
.htmleaf-header h1{font-size:1em;}
.htmleaf-links{font-size:1em}
.related h3{font-size:0.8em;}
.related a h3{font-size:0.6em;}
}
CSS代码(normalize.css):
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block;}
audio,canvas,video{display:inline-block;}
audio:not([controls]){display:none;height:0;}
[hidden]{display:none;}
html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;}
body{margin:0;}
a:focus{outline:thin dotted;}
a:active,a:hover{outline:0;}
h1{font-size:2em;margin:0.67em 0;}
abbr[title]{border-bottom:1px dotted;}
b,strong{font-weight:bold;}
dfn{font-style:italic;}
hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0;}
mark{background:#ff0;color:#000;}
code,kbd,pre,samp{font-family:monospace,serif;font-size:1em;}
pre{white-space:pre-wrap;}
q{quotes:"\201C" "\201D" "\2018" "\2019";}
small{font-size:80%;}
sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline;}
sup{top:-0.5em;}
sub{bottom:-0.25em;}
img{border:0;}
svg:not(:root){overflow:hidden;}
figure{margin:0;}
fieldset{border:1px solid #c0c0c0;margin:0 2px;padding:0.35em 0.625em 0.75em;}
legend{border:0;padding:0;}
button,input,select,textarea{font-family:inherit;font-size:100%;margin:0;}
button,input{line-height:normal;}
button,select{text-transform:none;}
button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer;}
button[disabled],html input[disabled]{cursor:default;}
input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0;}
input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box;}
input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
textarea{overflow:auto;vertical-align:top;}
table{border-collapse:collapse;border-spacing:0;}