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

x33g5p2x  于2022-03-02 转载在 HTML5  
字(11.2k)|赞(0)|评价(0)|浏览(541)

xdm社区活动开启了,快来我们的前端社区领奖品
Echarts开发者社区:https://bbs.csdn.net/forums/echarts
Element开发者社区:https://bbs.csdn.net/forums/element
一共52个奖品,不信还抽不中你?

雷迪森安的乡亲们,欢迎来到老实人的前端课堂,你是不是常常会因为自己写的代码朴实和无华而感到伤心难过呢,不要悲伤不要难过,这我们就来写点帅气逼人的代码。内容太干,建议收藏起来慢慢看,最后我还会教大家如何免费部署上线哦~

正片

小轮播图滑动滚播,好不好看你说了算。
视频演示:https://www.bilibili.com/video/BV1w341157rq/

结构就两行

  1. <main>
  2. <div class="grid">
  3. <div class="grid__item theme-1">
  4. <button class="action"><svg class="icon icon--rewind">
  5. <use xlink:href="#icon-rewind"></use>
  6. </svg></button>
  7. <button class="particles-button">Send</button>
  8. </div>
  9. <div class="grid__item theme-2">
  10. <button class="action"><svg class="icon icon--rewind">
  11. <use xlink:href="#icon-rewind"></use>
  12. </svg></button>
  13. <button class="particles-button">Upload</button>
  14. </div>
  15. <div class="grid__item theme-3">
  16. <button class="action"><svg class="icon icon--rewind">
  17. <use xlink:href="#icon-rewind"></use>
  18. </svg></button>
  19. <button class="particles-button">Delete</button>
  20. </div>
  21. <div class="grid__item theme-4">
  22. <button class="action"><svg class="icon icon--rewind">
  23. <use xlink:href="#icon-rewind"></use>
  24. </svg></button>
  25. <button class="particles-button">Submit</button>
  26. </div>
  27. <div class="grid__item theme-5">
  28. <button class="action"><svg class="icon icon--rewind">
  29. <use xlink:href="#icon-rewind"></use>
  30. </svg></button>
  31. <button class="particles-button">refresh</button>
  32. </div>
  33. <div class="grid__item theme-6">
  34. <button class="action"><svg class="icon icon--rewind">
  35. <use xlink:href="#icon-rewind"></use>
  36. </svg></button>
  37. <button class="particles-button">Bookmark</button>
  38. </div>
  39. <div class="grid__item theme-7">
  40. <button class="action"><svg class="icon icon--rewind">
  41. <use xlink:href="#icon-rewind"></use>
  42. </svg></button>
  43. <button class="particles-button">Subscribe</button>
  44. </div>
  45. <div class="grid__item theme-8">
  46. <button class="action"><svg class="icon icon--rewind">
  47. <use xlink:href="#icon-rewind"></use>
  48. </svg></button>
  49. <button class="particles-button">Logout</button>
  50. </div>
  51. <div class="grid__item theme-9">
  52. <button class="action"><svg class="icon icon--rewind">
  53. <use xlink:href="#icon-rewind"></use>
  54. </svg></button>
  55. <button class="particles-button">Add to cart</button>
  56. </div>
  57. <div class="grid__item theme-10">
  58. <button class="action"><svg class="icon icon--rewind">
  59. <use xlink:href="#icon-rewind"></use>
  60. </svg></button>
  61. <button class="particles-button">Pause</button>
  62. </div>
  63. <div class="grid__item theme-11">
  64. <button class="action"><svg class="icon icon--rewind">
  65. <use xlink:href="#icon-rewind"></use>
  66. </svg></button>
  67. <button class="particles-button">Register</button>
  68. </div>
  69. <div class="grid__item theme-12">
  70. <button class="action"><svg class="icon icon--rewind">
  71. <use xlink:href="#icon-rewind"></use>
  72. </svg></button>
  73. <button class="particles-button">Export</button>
  74. </div>
  75. </div>
  76. </main>
  77. <script src='js/anime.min.js'></script>
  78. <script src='js/particles.js'></script>
  79. <script src='js/demo.js'></script>

样式

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

表现

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

  1. /* eslint-disable */
  2. (function(global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('animejs')) :
  4. typeof define === 'function' && define.amd ? define(['animejs'], factory) :
  5. (global.Particles = factory(global.anime));
  6. }(this, (function(anime) {
  7. 'use strict';
  8. /* eslint-enable */
  9. function Particles(element, options) {
  10. this.el = getElement(element);
  11. this.options = extend({color: getCSSValue(this.el, 'background-color')}, this.defaults, options);
  12. this.init();
  13. }
  14. Particles.prototype = {
  15. defaults: {
  16. type: 'circle',
  17. style: 'fill',
  18. canvasPadding: 150,
  19. duration: 1000,
  20. easing: 'easeInOutCubic',
  21. direction: 'left',
  22. size: function() { return Math.floor((Math.random() * 3) + 1); },
  23. speed: function() { return rand(4); },
  24. particlesAmountCoefficient: 3,
  25. oscillationCoefficient: 20
  26. },
  27. init: function () {
  28. this.particles = [];
  29. this.frame = null;
  30. this.canvas = document.createElement('canvas');
  31. this.ctx = this.canvas.getContext('2d');
  32. this.canvas.className = 'particles-canvas';
  33. this.canvas.style = 'display:none;';
  34. this.wrapper = document.createElement('div');
  35. this.wrapper.className = 'particles-wrapper';
  36. this.el.parentNode.insertBefore(this.wrapper, this.el);
  37. this.wrapper.appendChild(this.el);
  38. this.parentWrapper = document.createElement('div');
  39. this.parentWrapper.className = 'particles';
  40. this.wrapper.parentNode.insertBefore(this.parentWrapper, this.wrapper);
  41. this.parentWrapper.appendChild(this.wrapper);
  42. this.parentWrapper.appendChild(this.canvas);
  43. },
  44. loop: function () {
  45. this.updateParticles();
  46. this.renderParticles();
  47. if (this.isAnimating()) {
  48. this.frame = requestAnimationFrame(this.loop.bind(this));
  49. }
  50. },
  51. updateParticles: function () {
  52. var p;
  53. for (var i = 0; i < this.particles.length; i++) {
  54. p = this.particles[i];
  55. if (p.life > p.death) {
  56. this.particles.splice(i, 1);
  57. } else {
  58. p.x += p.speed;
  59. p.y = this.o.oscillationCoefficient * Math.sin(p.counter * p.increase);
  60. p.life++;
  61. p.counter += this.disintegrating ? 1 : -1;
  62. }
  63. }
  64. if (!this.particles.length) {
  65. this.pause();
  66. this.canvas.style.display = 'none';
  67. if (is.fnc(this.o.complete)) {
  68. this.o.complete();
  69. }
  70. }
  71. },
  72. renderParticles: function () {
  73. this.ctx.clearRect(0, 0, this.width, this.height);
  74. var p;
  75. for (var i = 0; i < this.particles.length; i++) {
  76. p = this.particles[i];
  77. if (p.life < p.death) {
  78. this.ctx.translate(p.startX, p.startY);
  79. this.ctx.rotate(p.angle * Math.PI / 180);
  80. this.ctx.globalAlpha = this.disintegrating ? 1 - p.life / p.death : p.life / p.death;
  81. this.ctx.fillStyle = this.ctx.strokeStyle = this.o.color;
  82. this.ctx.beginPath();
  83. if ( this.o.type === 'circle' ) {
  84. this.ctx.arc(p.x, p.y, p.size, 0, 2 * Math.PI);
  85. }
  86. else if ( this.o.type === 'triangle' ) {
  87. this.ctx.moveTo(p.x, p.y);
  88. this.ctx.lineTo(p.x+p.size, p.y+p.size);
  89. this.ctx.lineTo(p.x+p.size, p.y-p.size);
  90. }
  91. else if ( this.o.type === 'rectangle' ){
  92. this.ctx.rect(p.x, p.y, p.size, p.size);
  93. }
  94. if ( this.o.style === 'fill' ) {
  95. this.ctx.fill();
  96. }
  97. else if ( this.o.style === 'stroke' ) {
  98. this.ctx.closePath();
  99. this.ctx.stroke();
  100. }
  101. this.ctx.globalAlpha = 1;
  102. this.ctx.rotate(-p.angle * Math.PI / 180);
  103. this.ctx.translate(-p.startX, -p.startY);
  104. }
  105. }
  106. },
  107. play: function () {
  108. this.frame = requestAnimationFrame(this.loop.bind(this));
  109. },
  110. pause: function () {
  111. cancelAnimationFrame(this.frame);
  112. this.frame = null;
  113. },
  114. addParticle: function (options) {
  115. var frames = this.o.duration * 60 / 1000;
  116. var speed = is.fnc(this.o.speed) ? this.o.speed() : this.o.speed;
  117. this.particles.push({
  118. startX: options.x,
  119. startY: options.y,
  120. x: this.disintegrating ? 0 : speed * -frames,
  121. y: 0,
  122. angle: rand(360),
  123. counter: this.disintegrating ? 0 : frames,
  124. increase: Math.PI * 2 / 100,
  125. life: 0,
  126. death: this.disintegrating ? (frames - 20) + Math.random() * 40 : frames,
  127. speed: speed,
  128. size: is.fnc(this.o.size) ? this.o.size() : this.o.size
  129. });
  130. },
  131. addParticles: function (rect, progress) {
  132. var progressDiff = this.disintegrating ? progress - this.lastProgress : this.lastProgress - progress;
  133. this.lastProgress = progress;
  134. var x = this.options.canvasPadding;
  135. var y = this.options.canvasPadding;
  136. var progressValue = (this.isHorizontal() ? rect.width : rect.height) * progress + progressDiff * (this.disintegrating ? 100 : 220);
  137. if (this.isHorizontal()) {
  138. x += this.o.direction === 'left' ? progressValue : rect.width - progressValue;
  139. } else {
  140. y += this.o.direction === 'top' ? progressValue : rect.height - progressValue;
  141. }
  142. var i = Math.floor(this.o.particlesAmountCoefficient * (progressDiff * 100 + 1));
  143. if (i > 0) {
  144. while (i--) {
  145. this.addParticle({
  146. x: x + (this.isHorizontal() ? 0 : rect.width * Math.random()),
  147. y: y + (this.isHorizontal() ? rect.height * Math.random() : 0)
  148. });
  149. }
  150. }
  151. if (!this.isAnimating()) {
  152. this.canvas.style.display = 'block';
  153. this.play();
  154. }
  155. },
  156. addTransforms: function (value) {
  157. var translateProperty = this.isHorizontal() ? 'translateX' : 'translateY';
  158. var translateValue = this.o.direction === 'left' || this.o.direction === 'top' ? value : -value;
  159. this.wrapper.style[transformString] = translateProperty + '('+ translateValue +'%)';
  160. this.el.style[transformString] = translateProperty + '('+ -translateValue +'%)';
  161. },
  162. disintegrate: function (options) {
  163. if (!this.isAnimating()) {
  164. this.disintegrating = true;
  165. this.lastProgress = 0;
  166. this.setup(options);
  167. var _ = this;
  168. this.animate(function(anim) {
  169. var value = anim.animatables[0].target.value;
  170. _.addTransforms(value);
  171. if (_.o.duration) {
  172. _.addParticles(_.rect, value / 100, true);
  173. }
  174. });
  175. }
  176. },
  177. integrate: function (options) {
  178. if (!this.isAnimating()) {
  179. this.disintegrating = false;
  180. this.lastProgress = 1;
  181. this.setup(options);
  182. var _ = this;
  183. this.animate(function(anim) {
  184. var value = anim.animatables[0].target.value;
  185. setTimeout(function() {
  186. _.addTransforms(value);
  187. }, _.o.duration);
  188. if (_.o.duration) {
  189. _.addParticles(_.rect, value / 100, true);
  190. }
  191. });
  192. }
  193. },
  194. setup: function (options) {
  195. this.o = extend({}, this.options, options);
  196. this.wrapper.style.visibility = 'visible';
  197. if (this.o.duration) {
  198. this.rect = this.el.getBoundingClientRect();
  199. this.width = this.canvas.width = this.o.width || this.rect.width + this.o.canvasPadding * 2;
  200. this.height = this.canvas.height = this.o.height || this.rect.height + this.o.canvasPadding * 2;
  201. }
  202. },
  203. animate: function (update) {
  204. var _ = this;
  205. anime({
  206. targets: {value: _.disintegrating ? 0 : 100},
  207. value: _.disintegrating ? 100 : 0,
  208. duration: _.o.duration,
  209. easing: _.o.easing,
  210. begin: _.o.begin,
  211. update: update,
  212. complete: function() {
  213. if (_.disintegrating) {
  214. _.wrapper.style.visibility = 'hidden';
  215. }
  216. }
  217. });
  218. },
  219. isAnimating: function () {
  220. return !!this.frame;
  221. },
  222. isHorizontal: function () {
  223. return this.o.direction === 'left' || this.o.direction === 'right';
  224. }
  225. };
  226. // Utils
  227. var is = {
  228. arr: function (a) { return Array.isArray(a); },
  229. str: function (a) { return typeof a === 'string'; },
  230. fnc: function (a) { return typeof a === 'function'; }
  231. };
  232. function stringToHyphens(str) {
  233. return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  234. }
  235. function getCSSValue(el, prop) {
  236. if (prop in el.style) {
  237. return getComputedStyle(el).getPropertyValue(stringToHyphens(prop)) || '0';
  238. }
  239. }
  240. var t = 'transform';
  241. var transformString = (getCSSValue(document.body, t) ? t : '-webkit-' + t);
  242. function extendSingle(target, source) {
  243. for (var key in source)
  244. target[key] = is.arr(source[key]) ? source[key].slice(0) : source[key];
  245. return target;
  246. }
  247. function extend(target) {
  248. if (!target) target = {};
  249. for (var i = 1; i < arguments.length; i++)
  250. extendSingle(target, arguments[i]);
  251. return target;
  252. }
  253. function rand(value) {
  254. return Math.random() * value - value / 2;
  255. }
  256. function getElement(element) {
  257. return is.str(element) ? document.querySelector(element) : element;
  258. }
  259. return Particles;
  260. })));

这里同学们重点学习下JavaScript编程思想。

更多实用炫酷表白代码可以在搜索公z号:前端老实人获取~

做好的网页效果,如何通过发链接给别人看?

1.1解决部署上线~> 部署上线工具(永久免费使用)

1.不需要买服务器就能部署线上,全世界都能访问你的连接啦, 这里给大家推荐一个程序员必备神器~ 简单易懂, 简直神器 ~ 需要源码或者部署神器可在公z号获取

2.就是把你的代码效果做好了以后, 部署到线上, 把链接发给别人, 就可以让对方通过你的连接点击进去, 就能看到你的网页效果啦, 电脑端和手机端都可以噢! (不然别人看你的网页都要发文件过去,体验感不太好~)

最后

博主为人老实,无偿解答问题,也会录制一些学习视频教同学们知识❤

如果对您有帮助,希望能给个👍/评论/收藏

您的三连~是对我创作最大的动力支持

关注:前端老实人👇领取源码哦~

相关文章

最新文章

更多