如何写一个花里胡哨的点击按钮(HTML+CSS+JS)

Bookmark

Subscribe

Logout

Add to cart

Pause

Register

Export

样式


样式代码太长了,影响阅读,有需要源码可以关注公z号:前端老实人获取

表现


关键代码,总共没几行,让它动起来,你们直接复制拿去用就行,不懂的话再私下问我吧。

/* eslint-disable */

(function(global, factory) {

typeof exports === ‘object’ && typeof module !== ‘undefined’ ? module.exports = factory(require(‘animejs’)) :

typeof define === ‘function’ && define.amd ? define([‘animejs’], factory) :

(global.Particles = factory(global.anime));

}(this, (function(anime) {

‘use strict’;

/* eslint-enable */

function Particles(element, options) {

this.el = getElement(element);

this.options = extend({color: getCSSValue(this.el, ‘background-color’)}, this.defaults, options);

this.init();

}

Particles.prototype = {

defaults: {

type: ‘circle’,

style: ‘fill’,

canvasPadding: 150,

duration: 1000,

easing: ‘easeInOutCubic’,

direction: ‘left’,

size: function() { return Math.floor((Math.random() * 3) + 1); },

speed: function() { return rand(4); },

particlesAmountCoefficient: 3,

oscillationCoefficient: 20

},

init: function () {

this.particles = [];

this.frame = null;

this.canvas = document.createElement(‘canvas’);

this.ctx = this.canvas.getContext(‘2d’);

this.canvas.className = ‘particles-canvas’;

this.canvas.style = ‘display:none;’;

this.wrapper = document.createElement(‘div’);

this.wrapper.className = ‘particles-wrapper’;

this.el.parentNode.insertBefore(this.wrapper, this.el);

this.wrapper.appendChild(this.el);

this.parentWrapper = document.createElement(‘div’);

this.parentWrapper.className = ‘particles’;

this.wrapper.parentNode.insertBefore(this.parentWrapper, this.wrapper);

this.parentWrapper.appendChild(this.wrapper);

this.parentWrapper.appendChild(this.canvas);

},

loop: function () {

this.updateParticles();

this.renderParticles();

if (this.isAnimating()) {

this.frame = requestAnimationFrame(this.loop.bind(this));

}

},

updateParticles: function () {

var p;

for (var i = 0; i < this.particles.length; i++) {

p = this.particles[i];

if (p.life > p.death) {

this.particles.splice(i, 1);

} else {

p.x += p.speed;

p.y = this.o.oscillationCoefficient * Math.sin(p.counter * p.increase);

p.life++;

p.counter += this.disintegrating ? 1 : -1;

}

}

if (!this.particles.length) {

this.pause();

this.canvas.style.display = ‘none’;

if (is.fnc(this.o.complete)) {

this.o.complete();

}

}

},

renderParticles: function () {

this.ctx.clearRect(0, 0, this.width, this.height);

var p;

for (var i = 0; i < this.particles.length; i++) {

p = this.particles[i];

if (p.life < p.death) {

this.ctx.translate(p.startX, p.startY);

this.ctx.rotate(p.angle * Math.PI / 180);

this.ctx.globalAlpha = this.disintegrating ? 1 - p.life / p.death : p.life / p.death;

this.ctx.fillStyle = this.ctx.strokeStyle = this.o.color;

this.ctx.beginPath();

if ( this.o.type === ‘circle’ ) {

this.ctx.arc(p.x, p.y, p.size, 0, 2 * Math.PI);

}

else if ( this.o.type === ‘triangle’ ) {

this.ctx.moveTo(p.x, p.y);

this.ctx.lineTo(p.x+p.size, p.y+p.size);

this.ctx.lineTo(p.x+p.size, p.y-p.size);

}

else if ( this.o.type === ‘rectangle’ ){

this.ctx.rect(p.x, p.y, p.size, p.size);

}

if ( this.o.style === ‘fill’ ) {

this.ctx.fill();

}

else if ( this.o.style === ‘stroke’ ) {

this.ctx.closePath();

this.ctx.stroke();

}

this.ctx.globalAlpha = 1;

this.ctx.rotate(-p.angle * Math.PI / 180);

this.ctx.translate(-p.startX, -p.startY);

}

}

},

play: function () {

this.frame = requestAnimationFrame(this.loop.bind(this));

},

pause: function () {

cancelAnimationFrame(this.frame);

this.frame = null;

},

addParticle: function (options) {

var frames = this.o.duration * 60 / 1000;

var speed = is.fnc(this.o.speed) ? this.o.speed() : this.o.speed;

this.particles.push({

startX: options.x,

startY: options.y,

x: this.disintegrating ? 0 : speed * -frames,

y: 0,

angle: rand(360),

counter: this.disintegrating ? 0 : frames,

increase: Math.PI * 2 / 100,

life: 0,

death: this.disintegrating ? (frames - 20) + Math.random() * 40 : frames,

speed: speed,

size: is.fnc(this.o.size) ? this.o.size() : this.o.size

});

},

addParticles: function (rect, progress) {

var progressDiff = this.disintegrating ? progress - this.lastProgress : this.lastProgress - progress;

this.lastProgress = progress;

var x = this.options.canvasPadding;

var y = this.options.canvasPadding;

var progressValue = (this.isHorizontal() ? rect.width : rect.height) * progress + progressDiff * (this.disintegrating ? 100 : 220);

if (this.isHorizontal()) {

x += this.o.direction === ‘left’ ? progressValue : rect.width - progressValue;

} else {

y += this.o.direction === ‘top’ ? progressValue : rect.height - progressValue;

}

var i = Math.floor(this.o.particlesAmountCoefficient * (progressDiff * 100 + 1));

if (i > 0) {

while (i–) {

this.addParticle({

x: x + (this.isHorizontal() ? 0 : rect.width * Math.random()),

y: y + (this.isHorizontal() ? rect.height * Math.random() : 0)

});

}

}

if (!this.isAnimating()) {

this.canvas.style.display = ‘block’;

this.play();

}

},

addTransforms: function (value) {

var translateProperty = this.isHorizontal() ? ‘translateX’ : ‘translateY’;

var translateValue = this.o.direction === ‘left’ || this.o.direction === ‘top’ ? value : -value;

this.wrapper.style[transformString] = translateProperty + ‘(’+ translateValue +‘%)’;

this.el.style[transformString] = translateProperty + ‘(’+ -translateValue +‘%)’;

},

disintegrate: function (options) {

if (!this.isAnimating()) {

this.disintegrating = true;

this.lastProgress = 0;

this.setup(options);

var _ = this;

this.animate(function(anim) {

var value = anim.animatables[0].target.value;

_.addTransforms(value);

if (_.o.duration) {

.addParticles(.rect, value / 100, true);

}

});

}

},

integrate: function (options) {

if (!this.isAnimating()) {

this.disintegrating = false;

this.lastProgress = 1;

this.setup(options);

var _ = this;

this.animate(function(anim) {

var value = anim.animatables[0].target.value;

setTimeout(function() {

_.addTransforms(value);

}, _.o.duration);

if (_.o.duration) {

.addParticles(.rect, value / 100, true);

}

});

}

},

setup: function (options) {

this.o = extend({}, this.options, options);

this.wrapper.style.visibility = ‘visible’;

if (this.o.duration) {

this.rect = this.el.getBoundingClientRect();

this.width = this.canvas.width = this.o.width || this.rect.width + this.o.canvasPadding * 2;

this.height = this.canvas.height = this.o.height || this.rect.height + this.o.canvasPadding * 2;

}

},

animate: function (update) {

var _ = this;

anime({

targets: {value: _.disintegrating ? 0 : 100},

value: _.disintegrating ? 100 : 0,

duration: _.o.duration,

easing: _.o.easing,

begin: _.o.begin,

update: update,

complete: function() {

if (_.disintegrating) {

_.wrapper.style.visibility = ‘hidden’;

}

}

});

},

isAnimating: function () {

return !!this.frame;

},

isHorizontal: function () {

return this.o.direction === ‘left’ || this.o.direction === ‘right’;

}

};

// Utils

算法刷题

大厂面试还是很注重算法题的,尤其是字节跳动,算法是问的比较多的,关于算法,推荐《LeetCode》和《算法的乐趣》,这两本我也有电子版,字节跳动、阿里、美团等大厂面试题(含答案+解析)、学习笔记、Xmind思维导图均可以分享给大家学习。

写在最后

最后,对所以做Java的朋友提几点建议,也是我的个人心得:

  1. 疯狂编程

  2. 学习效果可视化

  3. 写博客

  4. 阅读优秀代码

  5. 心态调整

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值