js实现鼠标框选效果

<html>

<head>
    <meta charset="utf-8">
    <title>js实现鼠标框选效果</title>
</head>

<style>
    body {
        padding: 100px;
    }

    .fileDiv {
        float: left;
        width: 100px;
        height: 100px;
        text-align: center;
        line-height: 100px;
        font-size: 12px;
        border: 1px solid #ccc;
        margin-right: 10px;
        margin-bottom: 10px;
    }

    .seled {
        border: 1px solid red;
        background-color: #D6DFF7;
    }

    h3 {
        text-align: center;
        padding-bottom: 50px
    }
</style>

<script type="text/javascript">

    (function () {

        document.onmousedown = function () {

            var selList = [];

            var fileNodes = document.getElementsByTagName("div");

            for (var i = 0; i < fileNodes.length; i++) {

                if (fileNodes[i].className.indexOf("fileDiv") != -1) {

                    fileNodes[i].className = "fileDiv";

                    selList.push(fileNodes[i]);

                }

            }

            var isSelect = true;

            var evt = window.event || arguments[0];

            var startX = (evt.x || evt.clientX);

            var startY = (evt.y || evt.clientY);

            var selDiv = document.createElement("div");

            selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";

            selDiv.id = "selectDiv";

            document.body.appendChild(selDiv);

            selDiv.style.left = startX + "px";

            selDiv.style.top = startY + "px";

            var _x = null;

            var _y = null;

            clearEventBubble(evt);

            document.onmousemove = function (ee) {

                evt = ee

                if (isSelect) {

                    if (selDiv.style.display == "none") {

                        selDiv.style.display = "";

                    }

                    _x = (evt.x || evt.clientX);

                    _y = (evt.y || evt.clientY);

                    selDiv.style.left = Math.min(_x, startX) + "px";

                    selDiv.style.top = Math.min(_y, startY) + "px";

                    selDiv.style.width = Math.abs(_x - startX) + "px";

                    selDiv.style.height = Math.abs(_y - startY) + "px";

                    // ---------------- 关键算法 ---------------------  

                    var _l = selDiv.offsetLeft, _t = selDiv.offsetTop;

                    var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;

                    for (var i = 0; i < selList.length; i++) {

                        var sl = selList[i].offsetWidth + selList[i].offsetLeft;

                        var st = selList[i].offsetHeight + selList[i].offsetTop;

                        if (sl > _l && st > _t && selList[i].offsetLeft < _l + _w && selList[i].offsetTop < _t + _h) {
                            console.log('不存在');
                            if (selList[i].className.indexOf("seled") == -1) {

                                selList[i].className = selList[i].className + " seled";

                            }

                        } else {

                            if (selList[i].className.indexOf("seled") != -1) {

                                selList[i].className = "fileDiv";

                            }

                        }

                    }

                }

                clearEventBubble(evt);

            }

            document.onmouseup = function () {

                isSelect = false;

                if (selDiv) {

                    document.body.removeChild(selDiv);

                    showSelDiv(selList);

                }

                selList = null, _x = null, _y = null, selDiv = null, startX = null, startY = null, evt = null;

            }

        }

    })();

    function clearEventBubble(evt) {

        if (evt.stopPropagation)

            evt.stopPropagation();

        else

            evt.cancelBubble = true;

        if (evt.preventDefault)

            evt.preventDefault();

        else

            evt.returnValue = false;

    }

    function showSelDiv(arr) {

        var count = 0;

        var selInfo = "";

        for (var i = 0; i < arr.length; i++) {

            if (arr[i].className.indexOf("seled") != -1) {

                count++;

                selInfo += arr[i].innerHTML + "\n";

            }

        }

        alert("共选择 " + count + " 个文件,分别是:\n" + selInfo);

    }

</script>

<body>
    <h3>按下鼠标左键不放,框选查看效果</h3>
    <div class="fileDiv">file1</div>

    <div class="fileDiv">file2</div>

    <div class="fileDiv">file3</div>

    <div class="fileDiv">file4</div>

    <div class="fileDiv">file5</div>

    <div class="fileDiv">file6</div>

    <div class="fileDiv">file7</div>

    <div class="fileDiv">file8</div>

    <div class="fileDiv">file9</div>
    <div class="fileDiv">file10</div>
    <div class="fileDiv">file11</div>
    <div class="fileDiv">file12</div>
    <div class="fileDiv">file13</div>
    <div class="fileDiv">file14</div>
    <div class="fileDiv">file15</div>
    <div class="fileDiv">file16</div>
    <div class="fileDiv">file17</div>

</body>

</html>

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 HTML 中的 `<div>` 元素来实现鼠标标签的效果。具体方法如下: 1. 在 HTML 文件中添加一个 `<div>` 元素,设置其宽度、高度和背景色等样式属性,使其覆盖需要进行鼠标的区域。 2. 使用 JavaScript 监听 `<div>` 元素的 `mousedown`、`mousemove` 和 `mouseup` 事件,分别对应鼠标按下、移动和松开的操作。 3. 在 `mousedown` 事件中记录鼠标按下的位置,并设置一个标志位表示正在进行鼠标操作。 4. 在 `mousemove` 事件中检测鼠标是否已经按下,并记录当前鼠标的位置。根据这两个位置计算出鼠标的区域,并设置一个 `<div>` 元素的样式,使其显示为一个半透明的矩形框。 5. 在 `mouseup` 事件中清除标志位,并将框的区域信息传递给后台处理。 以下是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>鼠标标签</title> <style> #select-area { position: absolute; background-color: rgba(255, 255, 0, 0.3); border: 1px dotted #000; pointer-events: none; } </style> </head> <body> <div id="wrapper" style="position: relative;"> <img src="https://picsum.photos/800/600" alt="示例图"> <div id="select-area"></div> </div> <script> const wrapper = document.getElementById('wrapper') const selectArea = document.getElementById('select-area') let isMouseDown = false let startX, startY wrapper.addEventListener('mousedown', e => { isMouseDown = true startX = e.clientX - wrapper.offsetLeft startY = e.clientY - wrapper.offsetTop }) wrapper.addEventListener('mousemove', e => { if (isMouseDown) { const currentX = e.clientX - wrapper.offsetLeft const currentY = e.clientY - wrapper.offsetTop const width = Math.abs(currentX - startX) const height = Math.abs(currentY - startY) const left = Math.min(startX, currentX) const top = Math.min(startY, currentY) selectArea.style.width = `${width}px` selectArea.style.height = `${height}px` selectArea.style.left = `${left}px` selectArea.style.top = `${top}px` } }) wrapper.addEventListener('mouseup', e => { isMouseDown = false const currentX = e.clientX - wrapper.offsetLeft const currentY = e.clientY - wrapper.offsetTop const width = Math.abs(currentX - startX) const height = Math.abs(currentY - startY) const left = Math.min(startX, currentX) const top = Math.min(startY, currentY) console.log(`(${left}, ${top}), (${left + width}, ${top + height})`) }) </script> </body> </html> ``` 上述代码中,我们在一个包含图片的 `<div>` 元素中实现鼠标标签的效果。当用户在图片上按下鼠标并移动时,会在图片上显示一个半透明的矩形框,表示当前的框区域。当用户松开鼠标时,会在控制台输出框区域的左上角和右下角坐标。你可以根据这些坐标信息来实现具体的标签取功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值