欧美中文字幕第一页-欧美中文字幕一区-欧美中文字幕一区二区三区-欧美中文字幕在线-欧美中文字幕在线播放-欧美中文字幕在线视频

JavaScript實現的拋物線運動效果 – WEB前端開發

我是創始人李巖:很抱歉!給自己產品做個廣告,點擊進來看看。  

最近做購物車功能,看到天貓上的購物車有元素拋物線運動效果,所以也想湊熱鬧實現一個。

網上搜索了一下,看了一下張鑫旭的《 小折騰:JavaScript與元素間的拋物線軌跡運動 》,原理張鑫旭已經講的很清楚了,多說了也沒什么意思,就是數學公式。不過看代碼個人覺得有點變扭,那不是我的習慣,所以自己重新寫了一個。

先看demo: http://www.css88.com/demo/parabola/index.html

如何使用:

運動位移的元素必須設置為 position: absolute ,通過絕對定位控制 lefttop 來實現的;

首先你可以 new 一個對象:

		var bool = new Parabola({
		el: "#boll",
		offset: [500, 100],
		curvature: 0.005,
		duration: 3000,
		callback: function () {
		alert("完成后回調")
		},
		stepCallback: function (x, y) {
		console.log(x, y);
		$("<div>").appendTo("body").css({
		"position": "absolute",
		"top": this.elOriginalTop + y,
		"left": this.elOriginalLeft + x,
		"background-color": "#CDCDCD",
		"width": "5px",
		"height": "5px",
		"border-radius": "5px"
		});
		}
		});
	

參數說明:

參數名 數據類型 默認值 描述
el jQuery||String(選擇器) null 必須填寫的參數,要移動的元素,可以是jQuery對象或選擇器
offset Array [0, 0] 表示移動元素在X,Y軸的偏移位置,設置了targetEl參數后,該參數將失效
targetEl jQuery||String(選擇器) null 終點元素,這時就會自動獲取該元素的left、top值,來表示移動元素在X,Y軸的偏移位置;設置了這個參數,offset將失效
duration Number 500 運動的時間,默認500毫秒
curvature Number 0.001 拋物線曲率,就是彎曲的程度,越接近于0越像直線,默認0.001
callback Function null 運動后執行的回調函數,this指向該對象
stepCallback Function null 運動過程中執行的回調函數,this指向該對象,接受x,y參數,分別表示X,Y軸的偏移位置。
autostart Boolean false 是否自動開始運動,默認為false

方法:

.reset()

重置元素的位置

.start()

開始執行動畫

.stop()

停止動畫

.setOptions(options)

重置options參數

先看demo: http://www.css88.com/demo/parabola/index.html

JS代碼:

		;
		(function () {
		var _$ = function (_this) {
		return _this.constructor == jQuery ? _this : $(_this);
		};
		// 獲取當前時間
		function now() {
		return +new Date();
		}
		// 轉化為整數
		function toInteger(text) {
		text = parseInt(text);
		return isFinite(text) ? text : 0;
		}
		var Parabola = function (options) {
		this.initialize(options);
		};
		Parabola.prototype = {
		constructor: Parabola,
		/**
		* 初始化
		* @classDescription 初始化
		* @param {Object} options 插件配置 .
		*/
		initialize: function (options) {
		this.options = this.options || this.getOptions(options);
		var ops = this.options;
		if (!this.options.el) {
		return;
		}
		this.$el = _$(ops.el);
		this.timerId = null;
		this.elOriginalLeft = toInteger(this.$el.css("left"));
		this.elOriginalTop = toInteger(this.$el.css("top"));
		// this.driftX X軸的偏移總量
		//this.driftY Y軸的偏移總量
		if (ops.targetEl) {
		this.driftX = toInteger(_$(ops.targetEl).css("left")) - this.elOriginalLeft;
		this.driftY = toInteger(_$(ops.targetEl).css("top")) - this.elOriginalTop;
		} else {
		this.driftX = ops.offset[0];
		this.driftY = ops.offset[1];
		}
		this.duration = ops.duration;
		// 處理公式常量
		this.curvature = ops.curvature;
		// 根據兩點坐標以及曲率確定運動曲線函數(也就是確定a, b的值)
		//a=this.curvature
		/* 公式: y = a*x*x + b*x + c;
		*/
		/*
		* 因為經過(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
		*/
		// 于是
		this.b = ( this.driftY - this.curvature * this.driftX * this.driftX ) / this.driftX;
		//自動開始
		if (ops.autostart) {
		this.start();
		}
		},
		/**
		* 初始化 配置參數 返回參數MAP
		* @param {Object} options 插件配置 .
		* @return {Object} 配置參數
		*/
		getOptions: function (options) {
		if (typeof options !== "object") {
		options = {};
		}
		options = $.extend({}, defaultSetting, _$(options.el).data(), (this.options || {}), options);
		return options;
		},
		/**
		* 定位
		* @param {Number} x x坐標 .
		* @param {Object} y y坐標 .
		* @return {Object} this
		*/
		domove: function (x, y) {
		this.$el.css({
		position: "absolute",
		left: this.elOriginalLeft + x,
		top: this.elOriginalTop + y
		});
		return this;
		},
		/**
		* 每一步執行
		* @param {Data} now 當前時間 .
		* @return {Object} this
		*/
		step: function (now) {
		var ops = this.options;
		var x, y;
		if (now > this.end) {
		// 運行結束
		x = this.driftX;
		y = this.driftY;
		this.domove(x, y);
		this.stop();
		if (typeof ops.callback === 'function') {
		ops.callback.call(this);
		}
		} else {
		//x 每一步的X軸的位置
		x = this.driftX * ((now - this.begin) / this.duration);
		//每一步的Y軸的位置y = a*x*x + b*x + c;   c==0;
		y = this.curvature * x * x + this.b * x;
		this.domove(x, y);
		if (typeof ops.stepCallback === 'function') {
		ops.stepCallback.call(this);
		}
		}
		return this;
		},
		/**
		* 設置options
		*  @param {Object} options 當前時間 .
		*/
		setOptions: function (options) {
		this.reset();
		if (typeof options !== "object") {
		options = {};
		}
		this.options = this.getOptions(options);
		this.initialize('parabola', this.options);
		return this;
		},
		/**
		* 開始
		*/
		start: function () {
		var self = this;
		// 設置起止時間
		this.begin = now();
		this.end = this.begin + this.duration;
		if (this.driftX === 0 && this.driftY === 0) {
		// 原地踏步就別浪費性能了
		return;
		}
		/*timers.push(this);
		Timer.start();*/
		if (!!this.timerId) {
		clearInterval(this.timerId);
		this.stop();
		}
		this.timerId = setInterval(function () {
		var t = now();
		self.step(t);
		}, 13);
		return this;
		},
		/**
		* 重置
		*/
		reset: function (x, y) {
		this.stop();
		x = x ? x : 0;
		y = y ? y : 0;
		this.domove(x, y);
		return this;
		},
		/**
		* 停止
		*/
		stop: function () {
		if (!!this.timerId) {
		clearInterval(this.timerId);
		}
		return this;
		}
		};
		var defaultSetting = {
		el: null,
		//偏移位置
		offset: [0, 0],
		//終點元素,這時就會自動獲取該元素的left、top,設置了這個參數,offset將失效
		targetEl: null,
		//運動的時間,默認500毫秒
		duration: 500,
		//拋物線曲率,就是彎曲的程度,越接近于0越像直線,默認0.001
		curvature: 0.001,
		//運動后執行的回調函數
		callback: null,
		// 是否自動開始,默認為false
		autostart: false,
		//運動過程中執行的回調函數
		stepCallback: null
		};
		window.Parabola = Parabola;
		})();
	

本文被轉載1次

首發媒體 Web前端開發 | 轉發媒體

隨意打賞

提交建議
微信掃一掃,分享給好友吧。
主站蜘蛛池模板: 美女一级大黄录像一片 | 成人影院在线观看视频 | 国产精品原创永久在线观看 | 国产网红在线观看 | 一级特黄一欧美俄罗斯毛片 | 国产成人精品三级91在线影院 | 嘿咻成人免费视频欧美激情 | 奇米影视第四色7777 | 欧美亚洲综合另类成人 | 日韩亚洲欧美综合一区二区三区 | 亚洲国产www | 四虎影视永久费观看在线 | 香蕉伊人网 | 日本在线视频毛片 | a大片久久爱一级 | 国产免费人成在线视频视频 | 久久久免费视频播放 | 国产欧美日本亚洲精品五区 | 四虎免费紧急入口观看 | 欧美成人 一区二区三区 | 国产这里有精品 | 国产高清不卡 | 久色tv| 亚洲欧洲视频在线观看 | 国产色产综合色产在线观看视频 | 国产呦系列免费 | 成人亚洲精品一区 | 99久热在线精品视频播 | 我想看一级播放片一级的 | 亚洲日本在线播放 | 日韩精品国产自在久久现线拍 | 日日夜夜噜噜 | 91成人在线播放 | 天天干天天天天 | 国产黄mmd在线观看免费 | 91尤物在线| 久久综合五月天 | 亚洲乱码一区二区三区国产精品 | 老妇激情毛片免费 | 狠狠色噜噜噜噜狠狠狠狠狠狠奇米 | 这里是九九伊人 |