Emlog评论列表JS实现打字特效

教程简介

在别人博客看到评论打字特效 觉得好看 网上找了教程发代码跟大家分享 这个效果原本是Atom编辑器上的一个插件 现在只要在网页上引用JS脚本 我们的网站也能拥有如此有逼格的效果

教程一步

以下代码保存为JS文件 命为什么名字都可以

  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define([], factory);
  6. else if(typeof exports === 'object')
  7. exports["POWERMODE"] = factory();
  8. else
  9. root["POWERMODE"] = factory();
  10. })(this, function() {
  11. return (function(modules) { // webpackBootstrap
  12. var installedModules = {};
  13. function __webpack_require__(moduleId) {
  14. if(installedModules[moduleId])
  15. return installedModules[moduleId].exports;
  16. var module = installedModules[moduleId] = {
  17. exports: {},
  18. id: moduleId,
  19. loaded: false
  20. };
  21. modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. module.loaded = true;
  23. return module.exports;
  24. }
  25. __webpack_require__.m = modules;
  26. __webpack_require__.c = installedModules;
  27. __webpack_require__.p = "";
  28. return __webpack_require__(0);
  29. })
  30. ([
  31. function(module, exports, __webpack_require__) {
  32. 'use strict';
  33. var canvas = document.createElement('canvas');
  34. canvas.width = window.innerWidth;
  35. canvas.height = window.innerHeight;
  36. canvas.style.cssText = 'position:fixed;top:0;left:0;pointer-events:none;z-index:999999';
  37. window.addEventListener('resize', function () {
  38. canvas.width = window.innerWidth;
  39. canvas.height = window.innerHeight;
  40. });
  41. document.body.appendChild(canvas);
  42. var context = canvas.getContext('2d');
  43. var particles = [];
  44. var particlePointer = 0;
  45. POWERMODE.shake = true;
  46. function getRandom(min, max) {
  47. return Math.random() * (max - min) + min;
  48. }
  49. function getColor(el) {
  50. if (POWERMODE.colorful) {
  51. var u = getRandom(0, 360);
  52. return 'hsla(' + getRandom(u - 10, u + 10) + ', 100%, ' + getRandom(50, 80) + '%, ' + 1 + ')';
  53. } else {
  54. return window.getComputedStyle(el).color;
  55. }
  56. }
  57. function getCaret() {
  58. var el = document.activeElement;
  59. var bcr;
  60. if (el.tagName === 'TEXTAREA' ||
  61. (el.tagName === 'INPUT' && el.getAttribute('type') === 'text')) {
  62. var offset = __webpack_require__(1)(el, el.selectionStart);
  63. bcr = el.getBoundingClientRect();
  64. return {
  65. x: offset.left + bcr.left,
  66. y: offset.top + bcr.top,
  67. color: getColor(el)
  68. };
  69. }
  70. var selection = window.getSelection();
  71. if (selection.rangeCount) {
  72. var range = selection.getRangeAt(0);
  73. var startNode = range.startContainer;
  74. if (startNode.nodeType === document.TEXT_NODE) {
  75. startNode = startNode.parentNode;
  76. }
  77. bcr = range.getBoundingClientRect();
  78. return {
  79. x: bcr.left,
  80. y: bcr.top,
  81. color: getColor(startNode)
  82. };
  83. }
  84. return { x: 0, y: 0, color: 'transparent' };
  85. }
  86. function createParticle(x, y, color) {
  87. return {
  88. x: x,
  89. y: y,
  90. alpha: 1,
  91. color: color,
  92. velocity: {
  93. x: -1 + Math.random() * 2,
  94. y: -3.5 + Math.random() * 2
  95. }
  96. };
  97. }
  98. function POWERMODE() {
  99. {
  100. var caret = getCaret();
  101. var numParticles = 5 + Math.round(Math.random() * 10);
  102. while (numParticles--) {
  103. particles[particlePointer] = createParticle(caret.x, caret.y, caret.color);
  104. particlePointer = (particlePointer + 1) % 500;
  105. }
  106. }
  107. {
  108. if (POWERMODE.shake) {
  109. var intensity = 1 + 2 * Math.random();
  110. var x = intensity * (Math.random() > 0.5 ? -1 : 1);
  111. var y = intensity * (Math.random() > 0.5 ? -1 : 1);
  112. document.body.style.marginLeft = x + 'px';
  113. document.body.style.marginTop = y + 'px';
  114. setTimeout(function() {
  115. document.body.style.marginLeft = '';
  116. document.body.style.marginTop = '';
  117. }, 75);
  118. }
  119. }
  120. };
  121. POWERMODE.colorful = false;
  122. function loop() {
  123. requestAnimationFrame(loop);
  124. context.clearRect(0, 0, canvas.width, canvas.height);
  125. for (var i = 0; i < particles.length; ++i) {
  126. var particle = particles[i];
  127. if (particle.alpha <= 0.1) continue;
  128. particle.velocity.y += 0.075;
  129. particle.x += particle.velocity.x;
  130. particle.y += particle.velocity.y;
  131. particle.alpha *= 0.96;
  132. context.globalAlpha = particle.alpha;
  133. context.fillStyle = particle.color;
  134. context.fillRect(
  135. Math.round(particle.x - 1.5),
  136. Math.round(particle.y - 1.5),
  137. 3, 3
  138. );
  139. }
  140. }
  141. requestAnimationFrame(loop);
  142. module.exports = POWERMODE;
  143. },
  144. function(module, exports) {
  145. (function () {
  146. var properties = [
  147. 'direction',
  148. 'boxSizing',
  149. 'width',
  150. 'height',
  151. 'overflowX',
  152. 'overflowY',
  153. 'borderTopWidth',
  154. 'borderRightWidth',
  155. 'borderBottomWidth',
  156. 'borderLeftWidth',
  157. 'borderStyle',
  158. 'paddingTop',
  159. 'paddingRight',
  160. 'paddingBottom',
  161. 'paddingLeft',
  162. 'fontStyle',
  163. 'fontVariant',
  164. 'fontWeight',
  165. 'fontStretch',
  166. 'fontSize',
  167. 'fontSizeAdjust',
  168. 'lineHeight',
  169. 'fontFamily',
  170. 'textAlign',
  171. 'textTransform',
  172. 'textIndent',
  173. 'textDecoration',
  174. 'letterSpacing',
  175. 'wordSpacing',
  176. 'tabSize',
  177. 'MozTabSize'
  178. ];
  179. var isFirefox = window.mozInnerScreenX != null;
  180. function getCaretCoordinates(element, position, options) {
  181. var debug = options && options.debug || false;
  182. if (debug) {
  183. var el = document.querySelector('#input-textarea-caret-position-mirror-div');
  184. if ( el ) { el.parentNode.removeChild(el); }
  185. }
  186. var div = document.createElement('div');
  187. div.id = 'input-textarea-caret-position-mirror-div';
  188. document.body.appendChild(div);
  189. var style = div.style;
  190. var computed = window.getComputedStyle? getComputedStyle(element) : element.currentStyle; // currentStyle for IE < 9
  191. style.whiteSpace = 'pre-wrap';
  192. if (element.nodeName !== 'INPUT')
  193. style.wordWrap = 'break-word';
  194. style.position = 'absolute';
  195. if (!debug)
  196. style.visibility = 'hidden';
  197. properties.forEach(function (prop) {
  198. style[prop] = computed[prop];
  199. });
  200. if (isFirefox) {
  201. if (element.scrollHeight > parseInt(computed.height))
  202. style.overflowY = 'scroll';
  203. } else {
  204. style.overflow = 'hidden';
  205. }
  206. div.textContent = element.value.substring(0, position);
  207. if (element.nodeName === 'INPUT')
  208. div.textContent = div.textContent.replace(/\s/g, "\u00a0");
  209. var span = document.createElement('span');
  210. span.textContent = element.value.substring(position) || '.'; // || because a completely empty faux span doesn't render at all
  211. div.appendChild(span);
  212. var coordinates = {
  213. top: span.offsetTop + parseInt(computed['borderTopWidth']),
  214. left: span.offsetLeft + parseInt(computed['borderLeftWidth'])
  215. };
  216. if (debug) {
  217. span.style.backgroundColor = '#aaa';
  218. } else {
  219. document.body.removeChild(div);
  220. }
  221. return coordinates;
  222. }
  223. if (typeof module != "undefined" && typeof module.exports != "undefined") {
  224. module.exports = getCaretCoordinates;
  225. } else {
  226. window.getCaretCoordinates = getCaretCoordinates;
  227. }
  228. }());
  229. }
  230. ])
  231. });
  232. ;

教程二步

模板的header.php footer.php 其中任意的文件添加以下代码就行

  1. <script type="text/javascript" src="这里填写你刚建立的JS的路径/你输入的名称.js"></script>
  2. <script>POWERMODE.colorful = true;POWERMODE.shake = false;document.body.addEventListener('

猜你喜欢

转载自www.cnblogs.com/ggcg135/p/12040539.html