以厘米为单位在输入范围内创建标尺点

时间:2018-07-17 14:02:21

标签: javascript html css jquery-ui-draggable

我想创建一个标尺,但是要创建多点标尺,就像在办公室用词一样。

如何创建以厘米或英寸为单位的最小值和最大值,以帮助我提出想法或代码。

$('.x').draggable({
    axis: 'x',
    containment: 'parent',
    start: function() {
        var top = ($(this).offset().top + 5) + 'px';
        var left = ($(this).offset().left + 5) + 'px';
        $('#parent').append('<div id="line" style="left:' + left + ';top:' + top + ';"></div>');
    },
    drag: function() {
        $('div#line').css({
            'top': ($(this).offset().top + 5) + 'px',
            'left': ($(this).offset().left + 5) + 'px',
        });
    },
    stop: function() {
        $('div#line').remove();
    }
});
* {
    margin: 0;
    padding: 0;
}

html,
body {
    width: 100%;
    height: 100%;
    background: #aaa;
    padding: 10px;
    box-sizing: border-box;
}

#parent {
    width: 100%;
    height: 2px;
    box-sizing: border-box;
    display: inline-block;
    background: #eee;
}

#point {
    padding: 0;
    background: red;
    display: inline-block;
    cursor: pointer;
    user-select: none;
    height: 10px;
    width: 10px;
    position: absolute;
    margin-top: -5px;
    margin-left: -5px;
}

#point:before {
    position: absolute;
    content: '';
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid #f00;
    left: 0;
    top: 100%
}

#line {
    position: fixed;
    width: 1px;
    background: red;
    height: 100%;
    top: 0px;
    opacity: .6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<div id="parent">
    <div id="point" class="x"></div>
</div>

0 个答案:

没有答案