商品 | 价格 | 图片 |
---|
<script></p>
<p>var liElements = document.querySelectorAll('li');<br> var tbodyElements = document.querySelector('.table tbody');</p>
<p>liElements.forEach(function (li) {<br> soldOut(li);<br> });</p>
<p>function soldOut(li) {<br> /*<br> * 以其中给一个li来做逻辑处理<br> * */<br> var buttonElement = li.querySelector('button');<br> var text1Element = li.querySelector('.text1');<br> var maskElement = li.querySelector('.mask');<br> var soldOutElement = li.querySelector('.sold_out');<br> var timeSpanElement = li.querySelectorAll('.time span');</p>
<p>var goodsName = li.querySelector('.goods_name').innerHTML;<br> var goodsPrice = li.querySelector('.goods_price').innerHTML;<br> var goodsImg = li.querySelector('.img').src;</p>
<p>/*<br> * 点击按钮获取倒计时的时间<br> * */<br> buttonElement.onclick = function() {<br> var seconds = parseInt(text1Element.value);</p>
<p>/*<br> * 把seconds变成时分秒的格式,并计算倒计时<br> * */<br> var timer = setInterval(function () {<br> seconds--;</p>
<p>if (seconds >= 0) {<br> var timeArr = seconds2HMS(seconds).split('');</p>
<p>timeArr.forEach(function (item, index) {<br> timeSpanElement[index].innerHTML = item;<br> });<br> } else {<br> //时间到了<br> clearInterval(timer);<br> timeOver();<br> add2List();<br> }</p>
<p><br> }, 1000);</p>
<p><br> };</p>
<p>function timeOver() {<br> maskElement.style.display = 'block';<br>// maskElement.style.opacity = '0.5';<br> animation(maskElement, {<br> opacity: 0.5<br> }, 500);</p>
<p>soldOutElement.style.display = 'block';<br> animation(soldOutElement, {<br> width: 198,<br> height: 198,<br> left: 61,<br> top: 76,<br> opacity: 1<br> }, 500, 'bounceOut');</p>
<p>setTimeout(function () {<br> shake(li, 'left', 20, 2);<br> }, 200);<br> }</p>
<p>/*<br> * 添加当前的商品信息到表格列表中<br> * */<br> function add2List() {</p>
<p>tbodyElements.innerHTML += '<tr><td>'+goodsName+'</td><td>'+goodsPrice+'</td><td><img src="'+goodsImg+'"/></td></tr>';</p>
<p>var trElements = tbodyElements.querySelectorAll('tr');<br> //获取到最后一个tr,也就是当前最新一次添加的元素<br>// console.log(trElements[trElements.length - 1]);<br>// animation(trElements[trElements.length - 1], {<br>// opacity: 1<br>// }, 100);</p>
<p>trElements.forEach(function (tr) {<br> animation(tr, {<br> opacity: 1<br> }, 100);<br> }, 1000);</p>
<p>}<br> }</p>
<p><br> /*<br> * 把秒钟转成时分秒格式<br> * */<br> function seconds2HMS(seconds) {</p>
<p>var H = addZero(parseInt(seconds / 3600));<br> var M = addZero(parseInt(seconds % 3600 / 60));<br> var S = addZero(parseInt(seconds % 60));</p>
<p>return H + M + S;<br> }</p>
<p>function addZero(v) {<br> return (v < 10 ? '0' : '') + v;<br> }</p>
<p>/*<br> * 颤抖吧!<br> * */<br> function shake(element, attr, range, step) {</p>
<p>/*<br> * 获取元素在颤抖之前的位置<br> * */<br> var originValue = getCss(element, attr);<br> /*<br> * 表示+还是-<br> * */<br> var flag = true;</p>
<p>var timer = setInterval(function () {</p>
<p>if (flag) {<br> //先负<br> element.style[attr] = originValue - range + 'px';<br> } else {<br> element.style[attr] = originValue + range + 'px';<br> //自减<br> range -= step;</p>
<p>if (range <= 0) {<br> element.style[attr] = originValue + 'px';<br> clearInterval(timer);<br> }<br> }</p>
<p>flag = !flag;</p>
<p>}, 16);</p>
<p>}</p>
<p>function getCss(element, attr) {<br> return parseFloat(element.currentStyle ? element.currentStyle[attr] : getComputedStyle(element)[attr]);<br> }<br> </script>
function animation(ele, attrs, duration, fx, callback) {
if (ele.timer) {
return;
}
var d = duration || 1000;
var obj = {};
for (var attr in attrs) {
obj[attr] = {};
obj[attr].b = parseFloat(getComputedStyle(ele)[attr]); obj[attr].c = attrs[attr] - obj[attr].b;
}
var fx = fx || 'linear';
var callback = callback || function(){};
var startTime = Date.now();
ele.timer = setInterval(function () {
var t = Date.now() - startTime;
if (t >= d) {
t = d;
}
for (var attr in attrs) {
var value = Tween[fx](t, obj[attr].b, obj[attr].c, d);
if (attr == 'opacity') {
ele.style[attr] = value;
} else {
ele.style[attr] = value + 'px';
}
}
if (t == d) {
clearInterval(ele.timer);
ele.timer = null;
callback();
}
}, 16);
}
var Tween = {
linear: function (t, b, c, d){ //匀速
return c*t/d + b;
},
easeIn: function(t, b, c, d){ //加速曲线
return c*(t/=d)*t + b; //t/=d t = t / d
},
easeOut: function(t, b, c, d){ //减速曲线
return -c *(t/=d)*(t-2) + b;
},
easeBoth: function(t, b, c, d){ //加速减速曲线
if ((t/=d/2) < 1) {
return c/2*t*t + b;
}
return -c/2 * ((--t)*(t-2) - 1) + b;
},
easeInStrong: function(t, b, c, d){ //加加速曲线
return c*(t/=d)*t*t*t + b;
},
easeOutStrong: function(t, b, c, d){ //减减速曲线
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeBothStrong: function(t, b, c, d){ //加加速减减速曲线
if ((t/=d/2) < 1) {
return c/2*t*t*t*t + b;
}
return -c/2 * ((t-=2)*t*t*t - 2) + b;
},
elasticIn: function(t, b, c, d, a, p){ //正弦衰减曲线(弹动渐入)
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p/4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
elasticOut: function(t, b, c, d, a, p){ //正弦增强曲线(弹动渐出)
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
},
elasticBoth: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d/2) == 2 ) {
return b+c;
}
if (!p) {
p = d*(0.3*1.5);
}
if ( !a || a < Math.abs(c) ) {
a = c;
var s = p/4;
}
else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
if (t < 1) {
return - 0.5*(a*Math.pow(2,10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
}
return a*Math.pow(2,-10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
},
backIn: function(t, b, c, d, s){ //回退加速(回退渐入)
if (typeof s == 'undefined') {
s = 1.70158;
}
return c*(t/=d)*t*((s+1)*t - s) + b;
},
backOut: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 3.70158; //回缩的距离
}
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
backBoth: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 1.70158;
}
if ((t /= d/2 ) < 1) {
return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
}
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
},
bounceIn: function(t, b, c, d){ //弹球减振(弹球渐出)
return c - Tween['bounceOut'](d-t, 0, c, d) + b;
},
bounceOut: function(t, b, c, d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
}
return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
},
bounceBoth: function(t, b, c, d){
if (t < d/2) {
return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
}
return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
}
};
Atas ialah kandungan terperinci 商品下架特效制作:定时器,倒计时,抖动特效代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!