html5的拖放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数字排序</title>
    <script type="text/javascript">

        function initPond(obj) {

            if(obj.getAttribute('flag')=='off'){
                obj.setAttribute('flag','on');
                obj.innerText = '重新开始';
            } else {
                //清除元素
                cleanNum();
            }

            // 定义一个数组有10个元素
            var len = 10;
            var arr = [];
            arr.length = len;
            for(var i=0;i<arr.length;i++){
                arr[i] = i+1;
            }
            // 对数组中有序的元素打乱顺序
            mixture(arr);
            // 创建div,放到pond中
            var pond = document.getElementById('pond');
            arr.forEach(function (val) {
                var div = document.createElement('div');
                div.innerText=val;
                div.className = 'num';
                div.setAttribute('draggable','true');
                div.setAttribute('id','num_'+val);
                div.setAttribute('ondragstart','drag(event);');
                pond.append(div);
            });


        }

        // 设置拖动的对象
        function drag(ev){
            ev.dataTransfer.setData("Text",ev.target.id);
        }

        // 设置元素允许拖元素到上面
        function allowDrop(ev) {
            ev.preventDefault();
        }

        // 拖动结束的动作
        function drop(ev) {
//            ev.preventDefault();
//            var toElement = ev.toElement;
            var moveOjb = document.getElementById(ev.dataTransfer.getData("Text"));
            if(ev.currentTarget.hasChildNodes()){
                document.getElementById('pond').append(ev.currentTarget.children[0]);
            }

            // check drop right or false,if false  change border red
            ev.currentTarget.appendChild(moveOjb);
            if( parseInt(moveOjb.innerText)==parseInt(ev.currentTarget.id.replace('place',''))){
                document.getElementById(ev.currentTarget.id).className='numCount'
            }else {
                document.getElementById(ev.currentTarget.id).className='redSolid';
            }

            // checkNum
            var num = checkNum();
            if(num==100){
                alert('恭喜你全部正确');
            }
        }

        function dropToPond(ev) {
            document.getElementById('pond').append(document.getElementById(ev.dataTransfer.getData("Text")));
        }
        
        /**
         * 打乱数组的排序
         * @param arr
         */
        function mixture(arr) {

            var arrMinture = [];
            var length = arr.length;
            for(var i=0;i<length;i++){
                var index = Math.floor(Math.random()*arr.length);
                arrMinture[i] = arr[index];
                arr.splice(index,1);
            }

            arrMinture.forEach(function (value) {
               arr.push(value);
            });
        }

        function checkNum() {
            var num = 0;
            document.getElementsByClassName('place')[0].childNodes.forEach(function (div) {
                if(div.className=='numCount'){
                    num+=10;
                }
            });
            return num;
        }

        function cleanNum() {
            var num = document.getElementsByClassName('num');
            var len = num.length;
            for(var i=len-1;i>=0;i--){
                num[i].remove();
            }
        }
    </script>
    <style type="text/css">
        a{
            text-decoration: none;
        }
        #pond{
            border: 2px dotted #ff006e;
            width: 280px;
            height: 100px;
        }

        .place>div{
            width: 35px;
            height: 35px;
            display: inline-block;
            border: 2px solid #46baff;
        }

        .num{
            display: inline-block;
            width: 30px;
            height: 30px;
            background-color: darkgrey;
            text-align: center;
            line-height: 30px;
            margin: 2px;
        }

        .redSolid{
            border: 2px solid red !important;
        }
    </style>
</head>
<body>
<h2>请给10个数字排序</h2>
<div class="place">
    <div id="place01" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place02" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place03" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place04" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place05" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place06" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place07" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place08" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place09" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
    <div id="place10" οndragοver="allowDrop(event);" οndrοp="drop(event);"></div>
</div>
<div id="pond"  οndragοver="allowDrop(event);" οndrοp="dropToPond(event);"></div>
<div><a href="javascript:void(0);" οnclick="initPond(this);" flag="off">开始</a></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值