Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <div id="container" class="container">
        <div id="box1" class="box" data-id='1' data-index="1" draggable="true">1</div>
        <div id="box2" class="box" data-id='2' data-index="2" draggable="true">2</div>
        <div id="box3" class="box" data-id='3' data-index="3" draggable="true">3</div>
        <div id="box4" class="box" data-id='4' data-index="4" draggable="true">4</div>
        <div id="box5" class="box" data-id='5' data-index="5" draggable="true">5</div>
    </div>
              
            
!

CSS

              
                 *{
            margin: 0;
            padding: 0;
        }

        .container {
            position: relative;
            top: 200px;
            left: 200px;
            width: 900px;
            line-height: 200px;
            background: green;
        }
        .container div {
            display: inline-block;
            width: 150px;
            margin-left: 20px;
            line-height: 100px;
            text-align: center;
            background: yellow;
        }
              
            
!

JS

              
                 var source = null,
            dataIndex,
            dataID,
            text;
        //循环每一个元素
        $('.box').each(function(index, ele) {
            //被拖拽元素进入目标元素阻止元素默认事件
            $(ele).on('dragover', function(e) {
            		console.log(e.target);
                e.preventDefault();
            })

            //被拖拽元素开始移动
            $(ele).on('dragstart', function(e) {
                //获取当前被拖拽元素
                source = $(this);
                console.log(e.target);
                //获取当前被拖拽元素的data-index
                dataIndex = $(this).attr('data-index')
                text = $(this).text()
                dataId = $(this).attr('data-id')
            })

            //目标元素已接受被拖拽元素
            $(ele).on('drop', function(e) {
                e.preventDefault();
                //判断被拖拽元素的data-index是否小于、等于或者大于目标元素的data-index
                if(dataIndex < $(this).attr('data-index')) {
                    //如果小于向后移动
                    moveDown(source, $(this))
                } else if(dataIndex === $(this).attr('data-index')) {
                    //如果等于不执行操作
                    return
                } else {
                    //如果大于向前移动
                    moveUp(source, $(this));
                }
                //目标元素的内容更改为被拖拽元素内容
                // $(this).text(text)
                $(this).attr('data-id',dataId)
            })
        });
        
        
        //向后移动元素
        function moveDown(se, ce) {
            //获取被拖拽元素的上一个元素内容
            // var t = $(se).next().text();
            //获取被拖拽元素的上一个元素data-id
            var d = $(se).next().attr('data-id');
            //把当前拖拽的元素内容更换成它的上一个元素
            // $(se).text(t);
            //把当前拖拽的元素data-id更换成它的下一个元素data-id
            $(se).attr('data-id',d)
            //把当前拖拽元素替换为它的下一个元素
            se = $(se).next();
            //判断当前拖拽元素的title是否和目标元素title相等
            if($(se).attr('data-index') === $(ce).attr('data-index')) {
                //如果当前拖拽元素等于目标元素,不作任何操作
                return false;
            } else {
                //如果不相等,递归,继续向后添加元素,把拖拽对象放到目标对象后面,(直到拖拽对象等于目标对象为止)
                //console.info(se,ce)
                moveDown(se, ce);
            }
        }
        //向前移动元素
        function moveUp(se, ce) {
            //获取被拖拽元素的上一个元素内容
            // var t = $(se).prev().text();
            //获取被拖拽元素的上一个元素data-id
            var d = $(se).prev().attr('data-id');
            //把当前拖拽的元素内容更换成它的上一个元素
            // $(se).text(t);
            //把当前拖拽的元素data-id更换成它的下一个元素data-id
            $(se).attr('data-id',d)
            //把当前拖拽元素替换为它的下一个元素
            se = $(se).prev();
            //判断当前拖拽元素的title是否和目标元素title相等
            if($(se).attr('data-index') === $(ce).attr('data-index')) {
                //如果当前拖拽元素等于目标元素,不作任何操作
                return false;
            } else {
                //如果不相等,递归,继续向后添加元素,把拖拽对象放到目标对象后面,(直到拖拽对象等于目标对象为止)
                //console.info(se,ce)
                moveUp(se, ce);
            }
        }
              
            
!
999px
A Pen is worth a thousand docs.

Console