这是我的代码:
HTML
<div id="canvas">
<div id="dragme"></div>
</div>
CSS
#canvas {
width:500px;
height:250px;
border:1px solid #444;
zoom:0.7;
}
#dragme {
width:100px;
height:50px;
background:#f30;
}
JS
$(function(){
$('#dragme').draggable({containment:'parent'})
})
使用css zoom
属性时遇到了一个重大问题。目标可拖动div的位置与光标位置不协调。
有没有干净简单的解决方案?我应该可以动态更改缩放。
答案 0 :(得分:20)
您无需设置缩放属性。我刚刚将差异添加到由于zoom属性而发生的draggable的位置。希望它有所帮助。
小提琴
JS
var zoom = $('#canvas').css('zoom');
var canvasHeight = $('#canvas').height();
var canvasWidth = $('#canvas').width();
$('#dragme').draggable({
drag: function(evt,ui)
{
// zoom fix
ui.position.top = Math.round(ui.position.top / zoom);
ui.position.left = Math.round(ui.position.left / zoom);
// don't let draggable get outside the canvas
if (ui.position.left < 0)
ui.position.left = 0;
if (ui.position.left + $(this).width() > canvasWidth)
ui.position.left = canvasWidth - $(this).width();
if (ui.position.top < 0)
ui.position.top = 0;
if (ui.position.top + $(this).height() > canvasHeight)
ui.position.top = canvasHeight - $(this).height();
}
});
答案 1 :(得分:8)
上面的解决方案对我没有用。所以我找到了自己的。如果其他人也有同样的问题,我想分享它。
var zoom = $('#canvas').css('zoom');
$('#dragme').draggable({
drag: function(evt,ui)
{
var factor = (1 / zoom) - 1
ui.position.top += Math.round((ui.position.top - ui.originalPosition.top) * factor)
ui.position.left += Math.round((ui.position.left - ui.originalPosition.left) * factor)
}
});
答案 2 :(得分:2)
我使用了tuze和Yusuf Demirag的解决方案,并在网格上添加了一些东西:
<!DOCTYPE html>
<meta charset="utf-8">
<head>
</head>
<body>
<svg id="chart"></svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var NSW = "NSW";
var QLD = "QLD";
var width = 600;
var height = 400;
var years = [];
var getStat = function(year, volatility, basis) {
return {
d: year,
x: basis,
vol: volatility,
value: 45 * Math.pow(basis, year),
high: 45 * Math.pow(basis+volatility, year),
low: 45 * Math.pow(basis-volatility, year),
}
}
for(i = 0; i < 25; i++) {
years.push(i);
}
var data = years.map(function(year){ return [getStat(year, 0.03, 1.08),getStat(year, 0.02, 1.08), getStat(year, 0.01, 1.08)]; }); // generate bogus data
var set_one = data.map(function(d) { return d[0];});
var set_two = data.map(function(d) { return d[1];});
var set_three = data.map(function(d) { return d[2];});
var chart = d3.select("#chart").attr("width", width).attr("height", height).append("g");
var x = d3.scale.linear().domain([0, years.length]).range([0, width]);
var y = d3.scale.linear().domain([0, d3.max(data, function(d){ return Math.max(d[0].high, d[1].high); })]).range([height, 0]);
var area = d3.svg.area().x(function(d,i) { return x(i); })
.y0(function(d, i){ return y(d.low)}) //FUNCTION FOR BASE-Y
.y1(function(d, i){ return y(d.high * 0.99);}); //FUNCTION FOR TOP-Y
chart
.selectAll("path.area")
.data([set_one,set_two,set_three]) // !!! here i can pass both arrays in.
.enter()
.append("path")
.attr("fill", "rgba(0,0,0,0.5)")
.attr("class", function(d,i) { return [NSW,QLD,"T"][i]; })
.attr("d", area);
</script>
它有一个小错误(它有时不能达到一个确切的值,例如:160px),但它可以满足我的需要。
希望它有所帮助。