简体   繁体   English

如何拖动DIV,使其宽度和高度为负值?

[英]How to drag a DIV so its width and height are negative values?

I'm a bit stumped here. 我在这里有些困惑。 I am developing a feedback utility that will allow the user to "draw" boxes on a web page to highlight problem areas. 我正在开发一种反馈实用程序,该实用程序将允许用户在网页上“绘制”框以突出显示问题区域。 Right now I have an overlay DIV that fills the screen and jQuery allows you to draw red outlined DIVs by clicking and dragging. 现在,我有一个覆盖屏幕的DIV,而jQuery允许您通过单击和拖动来绘制红色轮廓的DIV。

Here is the JS: 这是JS:

{
    var $feedbackOverlay = jQuery('#feedbackOverlay');
    var $original = { top: 0, left:0 };
    $feedbackOverlay.bind('mousedown', function (e)
    {
        jQuery('<div id="currentHighlight"></div>')
            .css('width', '1px')
            .css('height', '1px')
            .css('border', 'solid 3px #ff0000')
            .css('border-radius', '5px')
            .css('position', 'absolute')
            .css('left', e.pageX)
            .css('top', e.pageY)
            .css('z-index', '8000001')
            .appendTo('body');

        $original = { top: e.pageY, left: e.pageX };
    });
    $feedbackOverlay.bind('mousemove', function (e)
    {
        var $currentHighlight = jQuery('#currentHighlight');
        if ($currentHighlight.length > 0)
        {
            var $pos = { top: e.pageY, left: e.pageX };

            if($pos.top < $original.top) $currentHighlight.css('top', $pos.top);
            if ($pos.left < $original.left) $currentHighlight.css('left', $pos.left);

            $currentHighlight.height(Math.abs($pos.top - $original.top));
            $currentHighlight.width(Math.abs($pos.left - $original.left));
        }
    });
    $feedbackOverlay.bind('mouseup', function (e)
    {
        var $currentHighlight = jQuery('#currentHighlight');
        $currentHighlight.removeAttr('id');
    });
    var $feedbackInstructions = jQuery('#feedbackInstructions');
    $feedbackInstructions.fadeIn(1000, function ()
    {
        setTimeout(function ()
        {
            $feedbackInstructions.fadeOut(1000);
        }, 3000);
    });

    $feedbackOverlay.height(jQuery(document).height());
});

Here is a jsFiddle for the above: 这是上面的jsFiddle:

http://jsfiddle.net/Chevex/RSYTq/ http://jsfiddle.net/Chevex/RSYTq/

The problem is that I can't drag the boxes up or left. 问题是我不能向上或向左拖动框。 The first click puts the top left corner where the mouse clicked. 第一次单击将鼠标左上角放在单击鼠标的位置。 After that subsequent dragging will change the width of the box. 之后,后续拖动将更改框的宽度。 Letting go of the mouse completes the box and you may then start drawing another one. 放开鼠标即可完成该框,然后您就可以开始绘制另一个了。 If you try to drag the DIV left or up while drawing it's width will remain at 0 but won't go negative. 如果在绘图时尝试向左或向上拖动DIV,则其宽度将保持为0,但不会变为负数。

在这里您可以找到有效的解决方案: http : //jsfiddle.net/RSYTq/34/

There's no such thing aa negative width - these are not coorindinates. 没有负宽度的东西-这些不是协调的。 You need to reposition and recalculate the corner positions relative to the corner that's not being moved. 您需要重新定位并重新计算相对于未移动的角的角位置。

Something like this will get you closer to what you want: http://jsfiddle.net/RSYTq/18/ 这样的事情将使您更接近您想要的东西: http : //jsfiddle.net/RSYTq/18/

Doesn't quite handle move up and to the left and then switching to moving down and to the right quite right yet but it gives you the idea. 还没有完全处理向上和向左移动,然后切换到非常向右下移和向右移动的方法,但是它可以为您提供想法。

听起来您需要检查单击源(x,y)是否大于当前鼠标位置,然后交换左上角用于CSS的那个。

You would need to track the original start point somewhere (variables, data attributes on #currentHighlight , wherever you want), and check for width or height < 0. When so, set the #currentHighlight left / top CSS to be offset by original + (e.pageX - $currentHighlight.position().left) (for example). 您需要在某处跟踪原始起点(变量, #currentHighlight上的数据属性,无论您想要的位置),并检查width或height <0。在这种情况下,将#currentHighlight left / top CSS设置为original + (e.pageX - $currentHighlight.position().left) )。 Then set the #currentHighlight width/height to the same difference (but positive: (e.pageX - $currentHighlight.position().left) * -1 ). 然后将#currentHighlight宽度/高度设置为相同的差值(但为正数: (e.pageX - $currentHighlight.position().left) * -1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM