这是我所做到的...我将所有内容缩小,以便更好地查看div
的位置,并为此添加了滚动条。
重要点:
Math.sin
/ Math.cos
需要弧度制而非角度制
- CSS围绕元素的中点旋转,而不是
(0, 0)
(这适用于主要的div
和盒子div
;首先将-width/2
、-height/2
翻译,然后旋转,最后再翻译回来)
- 使用
parseInt(x,10)
确保您正在使用基数10
最终代码:http://jsfiddle.net/pimvdb/dXKJH/10/。由于.css
在旋转元素时存在一些问题,因此此解决方案还需要在HTML中硬编码位置和旋转。
$("#div-1").rotate("-10deg");
$("#div-2").rotate("10deg");
$("#div-3").rotate("15deg");
$("#div-4").rotate("75deg");
$("#main").click(function() {
console.log($('body').scrollLeft(), $('body').scrollTop());
});
$("a").click(function(e){
goTo($(this).attr("id").substring(4,5));
return false;
});
function n(x) {
return parseInt(x, 10);
}
function sin(x) {
return Math.sin(x / 180 * Math.PI);
}
function cos(x) {
return Math.cos(x / 180 * Math.PI);
}
function rotate(x, y, a) {
var x2 = cos(a) * x - sin(a) * y;
var y2 = sin(a) * x + cos(a) * y;
return [x2, y2];
}
var offsets = [null,
[0,100,-10],
[100,200, 10],
[300,100, 15],
[400,100, 75]].map(function(v) {
if(!v) return v;
var rotated = rotate(-50, -50, v[2]);
rotated[0] += v[0] + 50;
rotated[1] += v[1] + 50;
return rotated.concat(v[2]);
});
function goTo(num){
var obj = $("#div-" + num);
var angle = -n(obj.rotate());
var pointX = offsets[num][0] - 500;
var pointY = offsets[num][1] - 500;
var rotated = rotate(pointX, pointY, angle);
var newX = rotated[0] + 500;
var newY = rotated[1] + 500;
$("#main").animate({rotate: angle + "deg"}, 1000);
$("body").animate({scrollLeft: newX,
scrollTop: newY}, 1000)
}
Math.sin
/cos
需要弧度而不是角度。 - pimvdb