国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

js實(shí)現(xiàn)商品拋物線加入購物車特效

來源:懂視網(wǎng) 責(zé)編:小OO 時間:2020-11-27 20:25:52
文檔

js實(shí)現(xiàn)商品拋物線加入購物車特效

parapola.js。/*。== coordTarget.x) { params.progress(x.y);window.requestAnimationFrame(step);} else { // 運(yùn)動結(jié)束,回調(diào)執(zhí)行 params.complete();flagMove = true;} };window.requestAnimationFrame(step);flagMove = false;return this;};// 初始化方法 exports.init = function() { this.position().mark().move();};}return exports;};/*使用。
推薦度:
導(dǎo)讀parapola.js。/*。== coordTarget.x) { params.progress(x.y);window.requestAnimationFrame(step);} else { // 運(yùn)動結(jié)束,回調(diào)執(zhí)行 params.complete();flagMove = true;} };window.requestAnimationFrame(step);flagMove = false;return this;};// 初始化方法 exports.init = function() { this.position().mark().move();};}return exports;};/*使用。
本文實(shí)例為大家分享了js實(shí)現(xiàn)商品拋物線加入購物車動畫代碼,供大家參考,具體內(nèi)容如下

js實(shí)現(xiàn)商品拋物線加入購物車特效

parapola.js

/*!
 * by zhangxinxu(.com) 2012-12-27
 * you can visit http://www.zhangxinxu.com/wordpress/?p=3855 to get more infomation
 * under MIT license
*/
var funParabola = function(element, target, options) {
 /*
 * 網(wǎng)頁模擬現(xiàn)實(shí)需要一個比例尺
 * 如果按照1像素就是1米來算,顯然不合適,因?yàn)轫撁鎰硬粍泳蛶装傧袼? * 頁面上,我們放兩個物體,200~800像素之間,我們可以映射為現(xiàn)實(shí)世界的2米到8米,也就是100:1
 * 不過,本方法沒有對此有所體現(xiàn),因此不必在意
 */
 
 var defaults = {
 speed: 166.67, // 每幀移動的像素大小,每幀(對于大部分顯示屏)大約16~17毫秒
 curvature: 0.001, // 實(shí)際指焦點(diǎn)到準(zhǔn)線的距離,你可以抽象成曲率,這里模擬扔物體的拋物線,因此是開口向下的
 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;
 }
 }); 
 }
 
 // 根據(jù)兩點(diǎn)坐標(biāo)以及曲率確定運(yùn)動曲線函數(shù)(也就是確定a, b的值)
 /* 公式: y = a*x*x + b*x + c;
 */
 var a = params.curvature, b = 0, c = 0;
 
 // 是否執(zhí)行運(yùn)動的標(biāo)志量
 var flagMove = true;
 
 if (element && target && element.nodeType == 1 && target.nodeType == 1) {
 var rectElement = {}, rectTarget = {};
 
 // 移動元素的中心點(diǎn)位置,目標(biāo)元素的中心點(diǎn)位置
 var centerElement = {}, centerTarget = {};
 
 // 目標(biāo)元素的坐標(biāo)位置
 var coordElement = {}, coordTarget = {};
 
 // 標(biāo)注當(dāng)前元素的坐標(biāo)
 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)";
 }
 
 // 四邊緣的坐標(biāo)
 rectElement = element.getBoundingClientRect();
 rectTarget = target.getBoundingClientRect();
 
 // 移動元素的中心點(diǎn)坐標(biāo)
 centerElement = {
 x: rectElement.left + (rectElement.right - rectElement.left) / 2 + scrollLeft,
 y: rectElement.top + (rectElement.bottom - rectElement.top) / 2 + scrollTop
 };
 
 // 目標(biāo)元素的中心點(diǎn)位置
 centerTarget = {
 x: rectTarget.left + (rectTarget.right - rectTarget.left) / 2 + scrollLeft,
 y: rectTarget.top + (rectTarget.bottom - rectTarget.top) / 2 + scrollTop 
 };
 
 // 轉(zhuǎn)換成相對坐標(biāo)位置
 coordElement = {
 x: 0,
 y: 0 
 };
 coordTarget = {
 x: -1 * (centerElement.x - centerTarget.x),
 y: -1 * (centerElement.y - centerTarget.y) 
 };
 
 /*
 * 因?yàn)榻?jīng)過(0, 0), 因此c = 0
 * 于是:
 * y = a * x*x + b*x;
 * y1 = a * x1*x1 + b*x1;
 * y2 = a * x2*x2 + b*x2;
 * 利用第二個坐標(biāo):
 * b = (y2+ a*x2*x2) / x2
 */
 // 于是
 b = (coordTarget.y - a * coordTarget.x * coordTarget.x) / coordTarget.x; 
 
 return this;
 }; 
 
 // 按照這個曲線運(yùn)動
 exports.move = function() {
 // 如果曲線運(yùn)動還沒有結(jié)束,不再執(zhí)行新的運(yùn)動
 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;
 
 // 標(biāo)記當(dāng)前位置,這里有測試使用的嫌疑,實(shí)際使用可以將這一行注釋
 element.setAttribute("data-center", [Math.round(x), Math.round(y)].join());
 
 // x, y目前是坐標(biāo),需要轉(zhuǎn)換成定位的像素值
 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 {
 // 運(yùn)動結(jié)束,回調(diào)執(zhí)行
 params.complete();
 flagMove = true; 
 }
 };
 window.requestAnimationFrame(step);
 flagMove = false;
 
 return this;
 };
 
 // 初始化方法
 exports.init = function() {
 this.position().mark().move();
 };
 }
 
 return exports;
};
 
/*! requestAnimationFrame.js
 * by zhangxinxu 2013-09-30
*/
(function() {
 var lastTime = 0;
 var vendors = ['webkit', 'moz'];
 for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
 window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
 window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || // name has changed in Webkit
 window[vendors[x] + 'CancelRequestAnimationFrame'];
 }
 
 if (!window.requestAnimationFrame) {
 window.requestAnimationFrame = function(callback, element) {
 var currTime = new Date().getTime();
 var timeToCall = Math.max(0, 16.7 - (currTime - lastTime));
 var id = window.setTimeout(function() {
 callback(currTime + timeToCall);
 }, timeToCall);
 lastTime = currTime + timeToCall;
 return id;
 };
 }
 if (!window.cancelAnimationFrame) {
 window.cancelAnimationFrame = function(id) {
 clearTimeout(id);
 };
 }
}());

使用:

/* 元素 */
var element = document.getElementById("element"), 
 target = document.getElementById("target");
// 拋物線元素的的位置標(biāo)記
var parabola = funParabola(element, target).mark();
// 拋物線運(yùn)動的觸發(fā)
document.body.onclick = function() {
 element.style.marginLeft = "0px";
 element.style.marginTop = "0px";
 parabola.init();
};

加入購物車實(shí)戰(zhàn):

/* 本demo演示腳本基于ieBetter.js, 項(xiàng)目地址:https://github.com/zhangxinxu/ieBetter.js */
 
// 元素以及其他一些變量
var eleFlyElement = document.querySelector("#flyItem"), eleShopCart = document.querySelector("#shopCart");
var numberItem = 0;
// 拋物線運(yùn)動
var myParabola = funParabola(eleFlyElement, eleShopCart, {
 speed: 400,
 curvature: 0.002, 
 complete: function() {
 eleFlyElement.style.visibility = "hidden";
 eleShopCart.querySelector("span").innerHTML = ++numberItem;
 }
});
// 綁定點(diǎn)擊事件
if (eleFlyElement && eleShopCart) {
 [].slice.call(document.getElementsByClassName("btnCart")).forEach(function(button) {
 button.addEventListener("click", function() {
 // 滾動大小
 var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft || 0,
 scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
 
 eleFlyElement.style.left = event.clientX + scrollLeft + "px";
 eleFlyElement.style.top = event.clientY + scrollTop + "px";
 eleFlyElement.style.visibility = "visible";
 
 // 需要重定位
 myParabola.position().move(); 
 });
 });
}

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

js實(shí)現(xiàn)商品拋物線加入購物車特效

parapola.js。/*。== coordTarget.x) { params.progress(x.y);window.requestAnimationFrame(step);} else { // 運(yùn)動結(jié)束,回調(diào)執(zhí)行 params.complete();flagMove = true;} };window.requestAnimationFrame(step);flagMove = false;return this;};// 初始化方法 exports.init = function() { this.position().mark().move();};}return exports;};/*使用。
推薦度:
標(biāo)簽: js 效果 商品加入
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产 欧美 日本 | 黄色成人在线视频 | 欧美日本三级 | 日韩伦理亚洲欧美在线一区 | 黄网站免费在线观看 | 亚洲精品第一页 | 日韩欧美一区二区三区在线观看 | 亚洲欧美视频一区二区三区 | 精品视频一区二区三区 | 91欧美激情一区二区三区成人 | 久久久xxx| 日韩av线上 | 欧美一区二区二区 | 亚洲一区二区在线成人 | 亚洲视频一区在线 | 亚洲日韩欧美视频 | 国内视频一区二区三区 | 蜜臀91精品国产高清在线观看 | 一区二区成人国产精品 | 国产精品久久久久无码av | 怡红院一区二区三区 | 国产免费资源高清小视频在线观看 | 国产精品视频免费一区二区三区 | 国内偷自第一二三区 | 在线亚洲激情 | 日韩欧美国产电影 | 国产午夜视频在线观看 | 国产精品美女久久久久网站 | 艹久久 | www.日韩在线| 亚洲欧美日韩在线一区 | 日本免费一级视频 | 2019亚洲日韩新视频 | 国产日韩欧美精品 | 欧美另类色 | 日本久久网 | 91麻精品国产91久久久久 | 免费一区二区视频 | 伊人久久综合成人网小说 | 久久久久亚洲 | 婷婷久草 |