gpt4 book ai didi

javascript - 触摸事件后移动 div

转载 作者:行者123 更新时间:2023-12-01 03:49:18 25 4
gpt4 key购买 nike

我正在尝试在一些矩形 div 的圆上产生运动。我希望如果将一个 div 拖动到另一个 div 附近,则第二个 div 会在用户拖动的第一个 div 内移动。有没有一个插件可以做到这一点,或者我需要生成一些计算 2 个 div 的位置的东西。而且,矩形 div 不仅可以是 2 个,还可以是 10 个 div。

这是我的解决方案。

<html>
<head>
<style type="text/css">
div#dynamic-container{width:400px; height:400px; background-color: lightgoldenrodyellow; position:relative; border: 1px solid black;}
div#innerCircle{width: 380px; height: 380px; position: absolute; left: 10px; top:10px; background-color: lightcoral;
border-radius:190px; -moz-border-radius: 190px; -webkit-border-radius: 190px;}
div.marker{width: 20px; height:20px; background: black; position:absolute; left:195px; cursor: pointer;
-moz-transform:rotate(45deg);
-moz-transform-origin:5px 200px;

transform:rotate(45deg);
transform-origin:5px 200px;

-ms-transform:rotate(45deg);
-ms-transform-origin:5px 200px;

-webkit-transform:rotate(45deg);
-webkit-transform-origin:5px 200px;

z-index:17;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script src="/lib/jquery.overlaps.js" type="text/javascript" charset="utf-8"></script>




<script type="text/javascript">
function rotateAnnotationCropper(offsetSelector, xCoordinate, yCoordinate, cropper){
//alert(offsetSelector.left);

var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
var theta = Math.atan2(y,x)*(180/Math.PI);


var cssDegs = convertThetaToCssDegs(theta);
var rotate = 'rotate(' +cssDegs + 'deg)';
cropper.css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
$('body').on('mouseup', function(event){ $('body').unbind('mousemove')});

}

function convertThetaToCssDegs(theta){
var cssDegs = 90 - theta;
return cssDegs;
}

$(document).ready(function(){


//$('.marker-1').draggable();

var items = $('#dynamic-container').find('.marker');
var i = 0;
var cssDegs = 15;
var rotate = 'rotate(' +cssDegs + 'deg)';

for (i; i<items.length; i++){
if(i == 0){
cssDegs = cssDegs*i;
}else{
cssDegs = cssDegs+20;
}

console.log("cssDegs: "+cssDegs);
rotate = 'rotate(' +cssDegs + 'deg)';
//Imposto i gradi a tutti i punti per differenziarli al document ready
$(items[i]).css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
//items[i].css("background-color","blue");
}

console.log(items);

$('.marker').on('mousedown', function(){
$('body').on('mousemove', function(event){
//rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $('.marker'));
//rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $(event.target));
rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $(this).find('.marker-1'));


var markers = $(this).find('.marker');
var over = overlap(markers[0],$('#container'));
if(markers[0] != over){
console.log($(markers[0]));
console.log(over);
}else{
console.log('not over');
}

});
});

$('body').on('mouseup', function(event){ $('body').unbind('mousemove')});
});

function overlap (div1, div2){
var res = $(div1).overlaps($(div2));
return res;
}

</script>

</head>
<body>
<div id="container">
<div id ="dynamic-container">
<div class ="marker marker-1"></div>
<div class ="marker marker-2"></div>
<div id ="innerCircle"></div>
</div>
</div>
</body>

目前,此代码片段生成一个包含 2 个 div 的圆。可以在圆圈边界上移动。我希望如果用户拖动一个 div,当拖动的 div 接触另一个 div 时,它们会一起移动。我还尝试使用 jQuery Overlaps 库来获取 2 个 div 是否重叠,但我做错了。

提前致谢。您建议的每个库都会被广泛接受

最佳答案

编辑:

更完整的示例将是您的示例工作:

工作中的 JSFiddle: https://jsfiddle.net/mcbo9bs3/

        function rotateAnnotationCropper(offsetSelector, xCoordinate, yCoordinate, cropper){
//alert(offsetSelector.left);

var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
var theta = Math.atan2(y,x)*(180/Math.PI);


var cssDegs = convertThetaToCssDegs(theta);
var rotate = 'rotate(' +cssDegs + 'deg)';
cropper.css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
$('body').on('mouseup', function(event){ $('body').unbind('mousemove')});

}

function convertThetaToCssDegs(theta){
var cssDegs = 90 - theta;
return cssDegs;
}

$(document).ready(function(){


//$('.marker-1').draggable();

var items = $('#dynamic-container').find('.marker');
var i = 0;
var cssDegs = 15;
var rotate = 'rotate(' +cssDegs + 'deg)';

for (i; i<items.length; i++){
if(i == 0){
cssDegs = cssDegs*i;
}else{
cssDegs = cssDegs+20;
}

console.log("cssDegs: "+cssDegs);
rotate = 'rotate(' +cssDegs + 'deg)';
//Imposto i gradi a tutti i punti per differenziarli al document ready
$(items[i]).css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
//items[i].css("background-color","blue");
}

console.log(items);

$('.marker').on('mousedown', function(){
$('body').on('mousemove', function(event){
//rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $('.marker'));
//rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $(event.target));
rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $(this).find('.marker-1'));
var markers = $(this).find('.marker');
});
});

$('body').on('mouseup', function(event){ $('body').unbind('mousemove')});
});

window.setInterval(function() {
$('#result').text(collision($('.marker-1'), $('.marker-2')));
}, 200);


function collision($div1, $div2) {
var x1 = $($div1).offset().left;
var y1 = $($div1).offset().top;
var h1 = $($div1).outerHeight(true);
var w1 = $($div1).outerWidth(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $($div2).offset().left;
var y2 = $($div2).offset().top;
var h2 = $($div2).outerHeight(true);
var w2 = $($div2).outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;

if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}

要检测重叠,此代码应该有效:

function collision($div1, $div2) {
var x1 = $($div1).offset().left;
var y1 = $($div1).offset().top;
var h1 = $($div1).outerHeight(true);
var w1 = $($div1).outerWidth(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $($div2).offset().left;
var y2 = $($div2).offset().top;
var h2 = $($div2).outerHeight(true);
var w2 = $($div2).outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;

if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}

您可以将其应用到您的代码中(替换重叠函数)。

希望对你有帮助!

关于javascript - 触摸事件后移动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com