jQuery实现拖拽可编辑模块功能代码
发布时间 - 2026-01-10 22:28:09 点击率:次在没给大家分享实现代码之前,先给大家展示下效果图:
具体实现代码如下所示:
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>iNettuts - Welcome!</title>
<link href="inettuts.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="head">
<h1>iNettuts</h1>
</div>
<div id="columns">
<ul id="column1" class="column">
<li class="widget color-green" id="intro">
<div class="widget-head">
<h3>简介窗口</h3>
</div>
<div class="widget-content">
<p>如果你活着,早晚都会死;如果你死了,你就永远活着。</p>
</div>
</li>
<li class="widget color-red">
<div class="widget-head">
<h3>窗口标题</h3>
</div>
<div class="widget-content">
<p>世界上本没有路,有了腿便有了路。</p>
</div>
</li>
</ul>
<ul id="column2" class="column">
<li class="widget color-blue">
<div class="widget-head">
<h3>窗口标题</h3>
</div>
<div class="widget-content">
<p>一个人只能全力以赴,等待命运去揭晓答案。</p>
</div>
</li>
<li class="widget color-yellow" id="dingzh">
<div class="widget-head">
<h3>窗口标题</h3>
</div>
<div class="widget-content">
<p>望着沧茫大海,令我得到片刻解脱;不怀缅过去,也不展望未来。</p>
</div>
</li>
</ul>
<ul id="column3" class="column">
<li class="widget color-orange">
<div class="widget-head">
<h3>窗口标题</h3>
</div>
<div class="widget-content">
<p>就像这些樱花,每个生命都会凋零。每吸一口气,每喝一杯茶,每杀一个人都能体悟人生,这就是武士精神。</p>
</div>
</li>
<li class="widget color-white">
<div class="widget-head">
<h3>窗口标题</h3>
</div>
<div class="widget-content">
<p>人应竭尽所能,然后听天由命。</p>
</div>
</li>
</ul>
</div>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="inettuts.js"></script>
</body>
</html>
inettuts.js
var iNettuts = {
jQuery : $,
settings : {
columns : '.column',
widgetSelector: '.widget',
handleSelector: '.widget-head',
contentSelector: '.widget-content',
widgetDefault : {
movable: true,
removable: true,
collapsible: true,
editable: true,
colorClasses : ['color-yellow', 'color-red', 'color-blue', 'color-white', 'color-orange', 'color-green']
},
widgetIndividual : {
//个别的模块
intro : {
movable: false,
removable: false,
collapsible: false,
editable: false
},
dingzh : {
movable: false,
removable: false,
collapsible: true
}
}
},
//初始化
init : function () {
this.attachStylesheet('inettuts.js.css');
this.addWidgetControls();
this.makeSortable();
},
getWidgetSettings : function (id) {
var $ = this.jQuery,
settings = this.settings;
//判断ID模块是否定义过
return (id&&settings.widgetIndividual[id]) ? $.extend({},settings.widgetDefault,settings.widgetIndividual[id]) : settings.widgetDefault;
},
//动态追加元素
addWidgetControls : function () {
var iNettuts = this,
$ = this.jQuery,
settings = this.settings;
//设置选择器环境
//默认情况下,选择器从文档根部对 DOM 进行搜索。不过,可以为 $() 设置可选的 context 参数。
//如果我们希望在一个 .column类属性 的对象中 中搜索一个.widget类属性的 元素,可以限定下面的搜索:
$(settings.widgetSelector, $(settings.columns)).each(function () {
//遍历匹配的结果
var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
//移除窗体元素
if (thisWidgetSettings.removable) {
$('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
//阻止事件冒泡
e.stopPropagation();
}).click(function () {
if(confirm('这个小部件将被删除,确定吗?')) {
$(this).parents(settings.widgetSelector).animate({
opacity: 0
},function () {
$(this).wrap('<div/>').parent().slideUp(function () {
//删除
//remove() 方法移除被选元素,包括所有文本和子节点。
//该方法不会把匹配的元素从 jQuery 对象中删除,因而可以在将来再使用这些匹配的元素。
$(this).remove();
});
});
}
return false;
}).appendTo($(settings.handleSelector, this));
}
//编辑窗体元素
if (thisWidgetSettings.editable) {
$('<a href="#" class="edit">EDIT</a>').mousedown(function (e) {
e.stopPropagation();
}).toggle(function () {
$(this).css({backgroundPosition: '-66px 0', width: '55px'})
.parents(settings.widgetSelector)
.find('.edit-box').show().find('input').focus();
return false;
},function () {
$(this).css({backgroundPosition: '', width: ''})
.parents(settings.widgetSelector)
.find('.edit-box').hide();
return false;
}).appendTo($(settings.handleSelector,this));
$('<div class="edit-box" style="display:none;"/>')
.append('<ul><li class="item"><label>改变标题吗?</label><input value="' + $('h3',this).text() + '"/></li>')
.append((function(){
var colorList = '<li class="item"><label>可用的颜色:</label><ul class="colors">';
$(thisWidgetSettings.colorClasses).each(function () {
colorList += '<li class="' + this + '"/>';
});
return colorList + '</ul>';
})())
.append('</ul>')
.insertAfter($(settings.handleSelector,this));
}
//折叠
if (thisWidgetSettings.collapsible) {
$('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function (e) {
e.stopPropagation();
}).toggle(function () {
$(this).css({backgroundPosition: '-38px 0'})
.parents(settings.widgetSelector)
.find(settings.contentSelector).hide();
return false;
},function () {
$(this).css({backgroundPosition: '-52px 0'})
.parents(settings.widgetSelector)
.find(settings.contentSelector).show();
return false;
}).prependTo($(settings.handleSelector,this));
}
});
$('.edit-box').each(function () {
$('input',this).keyup(function () {
$(this).parents(settings.widgetSelector).find('h3').text( $(this).val().length>20 ? $(this).val().substr(0,20)+'...' : $(this).val() );
});
$('ul.colors li',this).click(function () {
var colorStylePattern = /\bcolor-[\w]{1,}\b/,
thisWidgetColorClass = $(this).parents(settings.widgetSelector).attr('class').match(colorStylePattern)
if (thisWidgetColorClass) {
$(this).parents(settings.widgetSelector)
.removeClass(thisWidgetColorClass[0])
.addClass($(this).attr('class').match(colorStylePattern)[0]);
}
return false;
});
});
},
attachStylesheet : function (href) {
var $ = this.jQuery;
return $('<link href="' + href + '" rel="stylesheet" type="text/css" />').appendTo('head');
},
//排序窗体布局
makeSortable : function () {
var iNettuts = this,
$ = this.jQuery,
settings = this.settings,
$sortableItems = (function () {
var notSortable = '';
$(settings.widgetSelector,$(settings.columns)).each(function (i) {
//判断是否具有可移动属性
if (!iNettuts.getWidgetSettings(this.id).movable) {
if(!this.id) {
this.id = 'widget-no-id-' + i;
}
notSortable += '#' + this.id + ',';
}
});
return $('> li:not(' + notSortable + ')', settings.columns);
})();
$sortableItems.find(settings.handleSelector).css({
cursor: 'move'
}).mousedown(function (e) {
$sortableItems.css({width:''});
$(this).parent().css({
width: $(this).parent().width() + 'px'
});
}).mouseup(function () {
if(!$(this).parent().hasClass('dragging')) {
$(this).parent().css({width:''});
} else {
$(settings.columns).sortable('disable');
}
});
$(settings.columns).sortable({
items: $sortableItems,
connectWith: $(settings.columns),
handle: settings.handleSelector,
placeholder: 'widget-placeholder',
forcePlaceholderSize: true,
revert: 300,
delay: 100,
opacity: 0.8,
containment: 'document',
start: function (e,ui) {
$(ui.helper).addClass('dragging');
},
stop: function (e,ui) {
$(ui.item).css({width:''}).removeClass('dragging');
$(settings.columns).sortable('enable');
}
});
}
};iNettuts.init();
inettuts.css
/* Reset */
body,img,p,h1,h2,h3,h4,h5,h6,ul,ol {margin:0; padding:0; list-style:none; border:none;}
/* End Reset */
body {font-size:0.8em; font-family:Arial,Verdana,Sans-Serif; background: #fff;}
a {color:white;}
/* Colors */
.color-yellow {background:#f2bc00;}
.color-red {background:#dd0000;}
.color-blue {background:#148ea4;}
.color-white {background:#dfdfdf;}
.color-orange {background:#f66e00;}
.color-green {background:#8dc100;}
.color-yellow h3,
.color-white h3,
.color-green h3
{color:#000;}
.color-red h3,
.color-blue h3,
.color-orange h3
{color:#FFF;}
/* End Colors */
/* Head section */
#head {
background: #fff url(img/head-bg.png) repeat-x;
height: 100px;
}
#head h1 {
line-height: 100px;
color: #FFF;
text-align: center;
background: url(img/inettuts.png) no-repeat center;
text-indent: -9999em
}
/* End Head Section */
/* Columns section */
#columns .column {
float: left;
width: 33.3%;
/* Min-height: */
min-height: 400px;
height: auto !important;
height: 400px;
}
#columns .column-dingzh {
float: left;
width: 33.3%;
/* Min-height: */
min-height: 400px;
height: auto !important;
height: 400px;
}
/* Column dividers (background-images) : */
#columns #column1 { background: url(img/column-bg-left.png) no-repeat right top; }
#columns #column3 { background: url(img/column-bg-right.png) no-repeat left top; }
#columns #column1 .widget { margin: 30px 35px 0 25px; }
#columns #column3 .widget { margin: 30px 25px 0 35px; }
#columns .widget {
margin: 30px 20px 0 20px;
padding: 2px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
#columns .widget .widget-head {
color: #fff;
overflow: hidden;
width: 100%;
height: 30px;
line-height: 30px;
}
#columns .widget .widget-head h3 {
padding: 0 5px;
float: left;
}
#columns .widget .widget-content {
background: #333 url(img/widget-content-bg.png) repeat-x;
padding: 0 5px;
color: #DDD;
-moz-border-radius-bottomleft: 2px;
-moz-border-radius-bottomright: 2px;
-webkit-border-bottom-left-radius: 2px;
-webkit-border-bottom-right-radius: 2px;
line-height: 1.2em;
overflow: hidden;
}
#columns .widget .widget-content p {
padding: 0.8em 0;
border-bottom: 1px solid #666;
}
#columns .widget .widget-content img {
float: right;
margin: 10px;
border: 1px solid #FFF;
}
#columns .widget .widget-content pre {
padding: 0.5em 5px;
color: #EEE;
font-size: 12px;
}
#columns .widget .widget-content ul {
padding: 5px 0 5px 20px;
list-style: disc;
}
#columns .widget .widget-content ul li {padding: 3px 0;}
#columns .widget .widget-content ul.images {
padding: 7px 0 0 0;
list-style: none;
height: 1%;
}
#columns .widget .widget-content ul.images li {
display: inline;
float: left;
}
#columns .widget .widget-content ul.images img {
display: inline;
float: left;
margin: 0 0 7px 7px;
}
/* End Columns section */
inettuts.js.css
/* JS-Enabled CSS */
.widget-head a.remove {
float: right;
display: inline;
background: url(img/buttons.gif) no-repeat -24px 0;
width: 14px;
height: 14px;
margin: 8px 4px 8px 0;
text-indent: -9999em;
outline: none;
}
.widget-head a.edit {
float: right;
display: inline;
background: url(img/buttons.gif) no-repeat;
width: 24px;
height: 14px;
text-indent: -9999em;
margin: 8px 4px 8px 4px;
outline: none;
}
.widget-head a.collapse {
float: left;
display: inline;
background: url(img/buttons.gif) no-repeat -52px 0;
width: 14px;
height: 14px;
text-indent: -9999em;
margin: 8px 0 8px 4px;
outline: none;
}
.widget-placeholder { border: 2px dashed #999;}
#column1 .widget-placeholder { margin: 30px 35px 0 25px; }
#column2 .widget-placeholder { margin: 30px 20px 0 20px; }
#column3 .widget-placeholder { margin: 30px 25px 0 35px; }
.edit-box {
overflow: hidden;
background: #333 url(img/widget-content-bg.png) repeat-x;
margin-bottom: 2px;
padding: 10px 0;
}
.edit-box li.item {
padding: 10px 0;
overflow: hidden;
float: left;
width: 100%;
clear: both;
}
.edit-box label {
float: left;
width: 30%;
color: #FFF;
padding: 0 0 0 10px;
}
.edit-box ul.colors li {
width: 20px;
height: 20px;
border: 1px solid #EEE;
float: left;
display: inline;
margin: 0 5px 0 0;
cursor: pointer;
}
# jquery实现拖拽功能
# jQuery 移动端拖拽(模块化开发
# 触摸事件
# webpack)
# 通过JQuery
# JQueryUI和Jsplumb实现拖拽模块
# 如果你
# 移除
# 象中
# 也不
# 选择器
# 就像
# 你就
# 世界上
# 这就是
# 都能
# 死了
# 望着
# 遍历
# 类属
# 听天由命
# 竭尽所能
# 将被
# 可选
# 所示
# 会把
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
jQuery validate插件功能与用法详解
Laravel如何编写单元测试和功能测试?(PHPUnit示例)
音乐网站服务器如何优化API响应速度?
HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
Android Socket接口实现即时通讯实例代码
如何用花生壳三步快速搭建专属网站?
百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏
Laravel如何使用Telescope进行调试?(安装和使用教程)
JavaScript如何实现错误处理_try...catch如何捕获异常?
如何快速搭建高效可靠的建站解决方案?
Laravel Admin后台管理框架推荐_Laravel快速开发后台工具
零服务器AI建站解决方案:快速部署与云端平台低成本实践
如何在自有机房高效搭建专业网站?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区
如何快速生成专业多端适配建站电话?
利用JavaScript实现拖拽改变元素大小
做企业网站制作流程,企业网站制作基本流程有哪些?
Windows10如何更改计算机工作组_Win10系统属性修改Workgroup
潮流网站制作头像软件下载,适合母子的网名有哪些?
如何快速辨别茅台真假?关键步骤解析
深圳网站制作的公司有哪些,dido官方网站?
Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言
如何在IIS中新建站点并配置端口与物理路径?
Laravel Session怎么存储_Laravel Session驱动配置详解
Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】
如何用PHP快速搭建CMS系统?
网站制作企业,网站的banner和导航栏是指什么?
免费网站制作appp,免费制作app哪个平台好?
谷歌Google入口永久地址_Google搜索引擎官网首页永久入口
个人摄影网站制作流程,摄影爱好者都去什么网站?
如何用低价快速搭建高质量网站?
PHP正则匹配日期和时间(时间戳转换)的实例代码
Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】
如何在Windows 2008云服务器安全搭建网站?
Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
Laravel如何使用withoutEvents方法临时禁用模型事件
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
如何用PHP快速搭建高效网站?分步指南
如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】
Laravel如何创建和注册中间件_Laravel中间件编写与应用流程
如何解决hover在ie6中的兼容性问题
如何在阿里云虚拟服务器快速搭建网站?
网站优化排名时,需要考虑哪些问题呢?
Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法
如何在阿里云虚拟主机上快速搭建个人网站?
北京网站制作的公司有哪些,北京白云观官方网站?

