Code Monkey home page Code Monkey logo

draggable's Introduction

draggable

可拖拽排序、编辑文本、上传图片的多功能编辑器

可添加模块,删除模块

使用HTML5 FileReader获取图片,实现本地预览,并异步上传到七牛服务器,然后返回当前图片地址,替换本地路径

无论上传图片还是编辑文本 核心:记录当前的模块的索引

效果图  

img

添加模块时候,不能字符串拼接html结构,否则不能绑定事件,要采用clone(true)深拷贝,clone结构和事件

     //clone;
    $(".add").click(function(){
        $(".clone").first().clone(true).css({"display":"block"}).appendTo($("#editable"))
    })

       

设置拖动区域

点击删除 判断当前模块是否上传了图片或编辑了文字 如果有 则询问是否删除 点击确认才删除 空模块则直接删除

   // 拖动排序 设置拖动区域
    var editableList = Sortable.create(editable, {
        sort:"true",
        animation: 150,
        handle: '.my-handle',  //设置拖动区域 默认是当前列表 但移动端不适合 
        filter: '.js-remove',  //设置删除的按钮  
        onFilter: function (evt) {
            var el = editableList.closest(evt.item); // get dragged item
            //console.log(typeof el);
            //console.log(evt.item);
            //console.log(evt.item.children);
            //console.log(evt.item.children[1]);
            //console.log(evt.item.children[1].currentSrc);
            //console.log(evt.item.children[5].innerHTML);
            if(evt.item.children[1].currentSrc != "" || evt.item.children[5].innerHTML != ""){
                layer.open({
                    skin:"demo-class",
                    content: '确认删除?',
                    btn: ['确认', '取消'],
                    shadeClose: false,
                    yes: function(){
                        layer.open({content: '确认', time: 1});
                        el && el.parentNode.removeChild(el);
                    }, no: function(){
                        layer.open({content: '您选择了取消', time: 1});
                    }
                })
            }
            else{
                el && el.parentNode.removeChild(el);
            }
        },
    });

     

数据结构转换成app可用的结构

代码已经封装好了 注释也很清楚 详见 create.js

将如下html结构

img              

转化为app可用的数据结构

img  

draggable's People

Contributors

dadtakesmefly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

alexzeng1981

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.