在没给大家分享实现代码之前,先给大家展示下效果图:
具体实现代码如下所示:
index.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>iNettuts - Welcome!</title> <link href="https://www.jb51.net/inettuts.css" type="text/css" /> </head> <body> <div> <h1>iNettuts</h1> </div> <div> <ul> <li> <div> <h3>简介窗口</h3> </div> <div> <p>如果你活着,早晚都会死;如果你死了,你就永远活着。</p> </div> </li> <li> <div> <h3>窗口标题</h3> </div> <div> <p>世界上本没有路,有了腿便有了路。</p> </div> </li> </ul> <ul> <li> <div> <h3>窗口标题</h3> </div> <div> <p>一个人只能全力以赴,等待命运去揭晓答案。</p> </div> </li> <li> <div> <h3>窗口标题</h3> </div> <div> <p>望着沧茫大海,令我得到片刻解脱;不怀缅过去,也不展望未来。</p> </div> </li> </ul> <ul> <li> <div> <h3>窗口标题</h3> </div> <div> <p>就像这些樱花,每个生命都会凋零。每吸一口气,每喝一杯茶,每杀一个人都能体悟人生,这就是武士精神。</p> </div> </li> <li> <div> <h3>窗口标题</h3> </div> <div> <p>人应竭尽所能,然后听天由命。</p> </div> </li> </ul> </div> <script type="text/javascript" src="https://www.jb51.net/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/jquery-ui-1.7.2.custom.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/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="#">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="#">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/>') .append('<ul><li><label>改变标题吗?</label><input value="' + $('h3',this).text() + '"/></li>') .append((function(){ var colorList = '<li><label>可用的颜色:</label><ul>'; $(thisWidgetSettings.colorClasses).each(function () { colorList += '<li/>'; }); return colorList + '</ul>'; })()) .append('</ul>') .insertAfter($(settings.handleSelector,this)); } //折叠 if (thisWidgetSettings.collapsible) { $('<a href="#">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 + '" 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