var funParabola = function(element, target, options) {
/*
* 网页模仿现实须要一个比例尺
* 假如依据1像素就是1米来算,明显不合适,由于页面动不动就几百像素
* 页面上,我们放两个物体,200~800像素之间,我们能够映照为现实天下的2米到8米,也就是100:1
* 不过,本要领没有对此有所表现,因而没必要在乎
*/
var defaults = {
speed: 166.67, // 每帧挪动的像素大小,每帧(关于大部分显示屏)约莫16~17毫秒
curvature: 0.001, // 现实指焦点到准线的间隔,你能够笼统成曲率,这里模仿扔物体的抛物线,因而是启齿向下的
progress: function() {},
complete: function() {}
};
var params = {}; options = options || {};
for (var key in defaults) {
params[key] = options[key] || defaults[key];
}
var exports = {
mark: function() { return this; },
position: function() { return this; },
move: function() { return this; },
init: function() { return this; }
};
/* 肯定挪动的体式格局
* IE6-IE8 是margin位移
* IE9+运用transform
*/
var moveStyle = "margin", testDiv = document.createElement("div");
if ("oninput" in testDiv) {
["", "ms", "webkit"].forEach(function(prefix) {
var transform = prefix + (prefix? "T": "t") + "ransform";
if (transform in testDiv.style) {
moveStyle = transform;
}
});
}
// 依据两点坐标以及曲率肯定活动曲线函数(也就是肯定a, b的值)
/* 公式: y = a*x*x + b*x + c;
*/
var a = params.curvature, b = 0, c = 0;
// 是不是实行活动的标志量
var flagMove = true;
if (element && target && element.nodeType == 1 && target.nodeType == 1) {
var rectElement = {}, rectTarget = {};
// 挪动元素的中心点位置,目的元素的中心点位置
var centerElement = {}, centerTarget = {};
// 目的元素的坐标位置
var coordElement = {}, coordTarget = {};
// 标注当前元素的坐标
exports.mark = function() {
if (flagMove === false) return this;
if (typeof coordElement.x == "undefined") this.position();
element.setAttribute("data-center", [coordElement.x, coordElement.y].join());
target.setAttribute("data-center", [coordTarget.x, coordTarget.y].join());
return this;
}
exports.position = function() {
if (flagMove === false) return this;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft,
scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 初始位置
if (moveStyle == "margin") {
element.style.marginLeft = element.style.marginTop = "0px";
} else {
element.style[moveStyle] = "translate(0, 0)";
}
// 四边沿的坐标
rectElement = element.getBoundingClientRect();
rectTarget = target.getBoundingClientRect();
// 挪动元素的中心点坐标
centerElement = {
x: rectElement.left + (rectElement.right - rectElement.left) / 2 + scrollLeft,
y: rectElement.top + (rectElement.bottom - rectElement.top) / 2 + scrollTop
};
// 目的元素的中心点位置
centerTarget = {
x: rectTarget.left + (rectTarget.right - rectTarget.left) / 2 + scrollLeft,
y: rectTarget.top + (rectTarget.bottom - rectTarget.top) / 2 + scrollTop
};
// 转换成相对坐标位置
coordElement = {
x: 0,
y: 0
};
coordTarget = {
x: -1 * (centerElement.x - centerTarget.x),
y: -1 * (centerElement.y - centerTarget.y)
};
/*
* 由于经由(0, 0), 因而c = 0
* 因而:
* y = a * x*x + b*x;
* y1 = a * x1*x1 + b*x1;
* y2 = a * x2*x2 + b*x2;
* 应用第二个坐标:
* b = (y2+ a*x2*x2) / x2
*/
// 因而
b = (coordTarget.y - a * coordTarget.x * coordTarget.x) / coordTarget.x;
return this;
};
// 依据这个曲线活动
exports.move = function() {
// 假如曲线活动还没有完毕,不再实行新的活动
if (flagMove === false) return this;
var startx = 0, rate = coordTarget.x > 0? 1: -1;
var step = function() {
// 切线 y'=2ax+b
var tangent = 2 * a * startx + b; // = y / x
// y*y + x*x = speed
// (tangent * x)^2 + x*x = speed
// x = Math.sqr(speed / (tangent * tangent + 1));
startx = startx + rate * Math.sqrt(params.speed / (tangent * tangent + 1));
// 防备过界
if ((rate == 1 && startx > coordTarget.x) || (rate == -1 && startx < coordTarget.x)) {
startx = coordTarget.x;
}
var x = startx, y = a * x * x + b * x;
// 标记当前位置,这里有测试运用的怀疑,现实运用能够将这一行解释
element.setAttribute("data-center", [Math.round(x), Math.round(y)].join());
// x, y现在是坐标,须要转换成定位的像素值
if (moveStyle == "margin") {
element.style.marginLeft = x + "px";
element.style.marginTop = y + "px";
} else {
element.style[moveStyle] = "translate("+ [x + "px", y + "px"].join() +")";
}
if (startx !== coordTarget.x) {
params.progress(x, y);
window.requestAnimationFrame(step);
} else {
// 活动完毕,回调实行
params.complete();
flagMove = true;
}
};
window.requestAnimationFrame(step);
flagMove = false;
return this;
};
// 初始化要领
exports.init = function() {
this.position().mark().move();
};
}
return exports;
}
/* 元素 */
var element = document.getElementById("element"), target = document.getElementById("target");
// 抛物线元素的的位置标记
var parabola = funParabola(element, target).mark();
// 抛物线活动的触发
document.body.onclick = function() {
element.style.marginLeft = "0px";
element.style.marginTop = "0px";
parabola.init();
};
一个不错的抛物线js结果
原文作者:shane_xu
原文地址: https://segmentfault.com/a/1190000004055070
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://segmentfault.com/a/1190000004055070
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。