Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

模拟multiple select,实现不按ctrl单击选中以及拖动选择 #9

Open
liusaint opened this issue Oct 9, 2017 · 0 comments

Comments

@liusaint
Copy link
Owner

liusaint commented Oct 9, 2017

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用ul,li模拟multiple select。实现拖拽多选,不按ctrl,单击选中</title>
    <link rel="stylesheet" href="select.css">
</head>
<body>
    <div class="title">原生select</div>
    <select name="" class="ori_select" multiple="">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
    </select>
    <div class="title">模拟select</div>
    <ul class="ul_select">
        <li value="1">1</li>
        <li value="2">2</li>
        <li value="3">3</li>
        <li value="4">4</li>
        <li value="5">5</li>
        <li value="6">6</li>
        <li value="7">7</li>
        <li value="8">8</li>
        <li value="9">9</li>
        <li value="10">10</li>
        <li value="11">11</li>
        <li value="12">12</li>
        <li value="13">13</li>
        <li value="14">14</li>
        <li value="15">15</li>
    </ul>
    
    <script src="http://apps.bdimg.com/libs/jquery/1.8.1/jquery.min.js"></script>
    <script src="select.js"></script>
</body>
</html>
$(function() {
    
    /*模拟select功能 begin*/
    (function() {


        var $startLi, //移动的初始节点
        $currentLi, //移动的当前节点
        currentIndex, //当前节点的索引
        startIndex, //初始索引
        maxIndex, //本次移动的最大索引
        minIndex, //本次移动最小索引
        isMoving;//是否在移动中
        
        //避免重复绑定,先解绑事件。
        $('body').off('.ul_select_event');
        
        //数据同步。仅仅是从模拟的同步到原生的。
        function setSelectVal() {
            $(".ori_select").val('');
            $('.ul_select .choosed').each(function(index, el) {
                var val = $.trim($(this).attr('value'));
                $('.ori_select option[value="' + val + '"]').prop('selected', true);
            });
        }

        //不使用ctrl,单选的时候选中选项
        $("body").on('click.ul_select_event', '.ul_select li', function(event) {
            $(this).toggleClass('choosed');
            setSelectVal();
        });

        //鼠标按下去的事件。
        $("body").on('mousedown.ul_select', '.ul_select li', function(event) {
            
            //mousedown的时候,部分数据需要初始化。
            $startLi = $(this);
            startIndex = maxIndex = minIndex = $startLi.index();

            //延时一点。绑定。一定程度避免点击事件跟移动事件的冲突。
            setTimeout(function() {
                $(".ul_select li").on('mousemove', function(event) {

                    isMoving = true;

                    $currentLi = $(this);
                    currentIndex = $currentLi.index();

                    if (currentIndex > maxIndex) {
                        maxIndex = currentIndex;
                    }

                    if (currentIndex < minIndex) {
                        minIndex = currentIndex;
                    }

                    for (var i = minIndex; i <= maxIndex; i++) {
                        $('.ul_select li').eq(i).removeClass('choosed');
                    }
                   
                    if (currentIndex >= startIndex) {

                        for (var i = startIndex; i <= currentIndex; i++) {
                            $('.ul_select li').eq(i).addClass('choosed');
                        }

                    } else {                        

                        for (var i = startIndex; i >= currentIndex; i--) {
                            $('.ul_select li').eq(i).addClass('choosed');
                        }

                    }

                })
            }, 50)

        });

        //结束移动状态
        $("body").on('mouseup.ul_select_event', function(event) {
            $(".ul_select li").off('mousemove');
            if (isMoving) {
                isMoving = false;
                setSelectVal();
            }

        });

    })();
    /*模拟select功能 end*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant