当前位置:   article > 正文

【HTML】制作一个跟随鼠标的流畅线条引导页界面(可直接复制源码)

【HTML】制作一个跟随鼠标的流畅线条引导页界面(可直接复制源码)

  目录

前言

HTML部分

CSS部分

JS部分

效果图

总结


前言

        无需多言,本文将详细介绍一段HTML代码,图中线条可跟随鼠标移动,具体内容如下:

开始

        首先新建一个HTML的文本,文本名改为[index.html],创建好后右键用文本文档打开,再把下面相对应代码填入后保存即可。(文本名主要是改后缀)

HTML部分

       下面代码是一个完整的HTML页面,包含了HTML、CSS和JavaScript。首先先对代码各个方面进行分析:

HTML部分

  • <!DOCTYPE html>声明了文档类型和HTML版本,这里是XHTML 1.0 Transitional,它允许使用一些不严格的标签。
  • <html>标签定义了整个页面的内容,xmlns属性指定了命名空间,通常用于XML文档。
  • <head>部分包含了文档的元数据,如<meta>标签定义了字符集为UTF-8,<title>定义了页面标题。
  • <body>标签内包含了页面的所有内容,如文本、图片、视频等。

CSS部分

  • 使用了内联<style>标签定义了页面的样式。
  • 有一个重置样式* { margin: 0; padding: 0; ... },这用于统一不同浏览器的默认样式。
  • 定义了一些动画和过渡效果,如h1, h2的字母逐渐显示效果。
  • 页面背景、文字样式、链接样式等都通过CSS进行了定义。

JavaScript部分

  • 定义了一个Stats函数,用于显示页面的帧率(FPS)和毫秒数(MS)。
  • 定义了OscillatorTendril类,这些类用于生成页面上的动态效果。
  • 使用了requestAnimFrame来创建平滑的动画循环。
  • 通过mousemovetouchstart等事件监听器来响应用户的交互。
  • 有一个letters函数,用于将标题中的每个字母分开,并添加动画效果。
  • 有一个save函数,可能用于保存页面的当前状态为图片。

其他注意事项

  • <canvas>元素被用于绘制动态图形。
  • 页面中有一些用于调试的代码,如settings.debug = true;,这可能在开发过程中有用,但在生产环境中应该被移除或设置为false
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    5. <title>丝滑</title>
    6. <style type="text/css">
    7. * {
    8. margin: 0;
    9. padding: 0;
    10. -webkit-box-sizing: border-box;
    11. -moz-box-sizing: border-box;
    12. -ms-box-sizing: border-box;
    13. -o-box-sizing: border-box;
    14. box-sizing: border-box;
    15. }
    16. html, body {
    17. margin: 0;
    18. padding: 0;
    19. font: 16px/1.4 'Lato', sans-serif;
    20. color: #fefeff;
    21. -webkit-font-smoothing: antialiased;
    22. font-smoothing: antialiased;
    23. }
    24. body {
    25. background: rgb(8,5,16);
    26. overflow:hidden;
    27. -webkit-touch-callout: none;
    28. -webkit-user-select: none;
    29. -khtml-user-select: none;
    30. -moz-user-select: none;
    31. -ms-user-select: none;
    32. user-select: none;
    33. }
    34. h1 {
    35. font: 2.75em 'Cinzel', serif;
    36. font-weight: 400;
    37. letter-spacing: 0.35em;
    38. text-shadow: 0 0 25px rgba(254,254,255,0.85);
    39. }
    40. h2 {
    41. font: 1.45em 'Cinzel', serif;
    42. font-weight: 400;
    43. letter-spacing: 0.5em;
    44. text-shadow: 0 0 25px rgba(254,254,255,0.85);
    45. text-transform: lowercase;
    46. }
    47. [class^="letter"] {
    48. -webkit-transition: opacity 3s ease;
    49. -moz-transition: opacity 3s ease;
    50. transition: opacity 3s ease;
    51. }
    52. .letter-0 { transition-delay: 0.2s; }
    53. .letter-1 { transition-delay: 0.4s; }
    54. .letter-2 { transition-delay: 0.6s; }
    55. .letter-3 { transition-delay: 0.8s; }
    56. .letter-4 { transition-delay: 1.0s; }
    57. .letter-5 { transition-delay: 1.2s; }
    58. .letter-6 { transition-delay: 1.4s; }
    59. .letter-7 { transition-delay: 1.6s; }
    60. .letter-8 { transition-delay: 1.8s; }
    61. .letter-9 { transition-delay: 2.0s; }
    62. .letter-10 { transition-delay: 2.2s; }
    63. .letter-11 { transition-delay: 2.4s; }
    64. .letter-12 { transition-delay: 2.6s; }
    65. .letter-13 { transition-delay: 2.8s; }
    66. .letter-14 { transition-delay: 3.0s; }
    67. h1, h2 {
    68. visibility: hidden;
    69. -webkit-transform: translate3d(0, 0, 0);
    70. -moz-transform: translate3d(0, 0, 0);
    71. transform: translate3d(0, 0, 0);
    72. }
    73. h1.transition-in,
    74. h2.transition-in {
    75. visibility: visible;
    76. }
    77. h1 [class^="letter"],
    78. h2 [class^="letter"] {
    79. opacity: 0;
    80. }
    81. h1.transition-in [class^="letter"],
    82. h2.transition-in [class^="letter"] {
    83. opacity: 1;
    84. }
    85. #container {
    86. display: table;
    87. position: absolute;
    88. z-index: 20;
    89. width: 100%;
    90. height: 100%;
    91. text-align: center;
    92. cursor: none;
    93. left: 15px;
    94. }
    95. #container > div {
    96. display: table-cell;
    97. vertical-align: middle;
    98. }
    99. #container p {
    100. position: absolute;
    101. width: 100%;
    102. left: 0;
    103. bottom: 25px;
    104. font-size: 0.8em;
    105. letter-spacing: 0.1em;
    106. font-weight: 300;
    107. color: #76747a;
    108. -webkit-font-smoothing: subpixel-antialiased;
    109. font-smoothing: subpixel-antialiased;
    110. }
    111. #container p strong {
    112. color: #b3abc5;
    113. font-size: 5em;
    114. }
    115. #container p span {
    116. font-size: 0.75em;
    117. padding: 0 2px;
    118. }
    119. #canvas {
    120. position: absolute;
    121. z-index: 10;
    122. top: 0;
    123. left: 0;
    124. width: 100%;
    125. height: 100%;
    126. cursor: none;
    127. }
    128. #stats {
    129. position: absolute;
    130. z-index: 10;
    131. left: 10px;
    132. top: 10px;
    133. }
    134. .dg.ac {
    135. z-index: 100 !important;
    136. }
    137. #container div p strong a {
    138. color: #999;
    139. font-size: 0.5em;
    140. }
    141. body,td,th {
    142. font-family: Lato, sans-serif;
    143. }
    144. a:link {
    145. text-decoration: none;
    146. }
    147. a:visited {
    148. text-decoration: none;
    149. }
    150. a:hover {
    151. text-decoration: none;
    152. }
    153. a:active {
    154. text-decoration: none;
    155. }
    156. </style>
    157. </head>
    158. <body ondragstart="window.event.returnValue=false" oncontextmenu="window.event.returnValue=false" onselectstart="event.returnValue=false">
    159. <div id="container">
    160. <div>
    161. <h1 id="h1">HTML讨论群,计算机考证讨论群,请加QQ群</h1>
    162. <h2 id="h2">757183195,428886795</h2>
    163. <h3 id="h2">雷神 Leo</h3>
    164. <p>&nbsp;</p>
    165. <p><strong><a href="https://gxguizhiyuan.com/volunteer-pc/#/home/team/info/824313033?isAdd=1">简介</a></strong></p>
    166. <p>&nbsp;</p>
    167. <p>&nbsp;</p>
    168. <p>&nbsp;</p>
    169. <p>&nbsp;</p>
    170. <p>&nbsp;</p>
    171. </div>
    172. </div>
    173. <canvas id="canvas"></canvas>
    174. <audio autoplay="autoplay">
    175. <source src="http://www.ntzyw.cn" type="audio/ogg">
    176. <source src="http://p2014.yaotou.com:8081/2014%2F2014-4%2F2014-4-20%2F2014420203237.m4a" type="audio/mpeg">
    177. </audio>
    178. <script>
    179. var Stats=function(){var e=Date.now(),t=e,i=0,n=1/0,r=0,s=0,o=1/0,a=0,l=0,h=0,c=document.createElement("div");c.id="stats",c.addEventListener("mousedown",function(e){e.preventDefault(),v(++h%2)},!1),c.style.cssText="width:80px;opacity:0.9;cursor:pointer";var u=document.createElement("div");u.id="fps",u.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#002",c.appendChild(u);var d=document.createElement("div");d.id="fpsText",d.style.cssText="color:#0ff;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px",d.innerHTML="FPS",u.appendChild(d);var p=document.createElement("div");for(p.id="fpsGraph",p.style.cssText="position:relative;width:74px;height:30px;background-color:#0ff",u.appendChild(p);74>p.children.length;){var f=document.createElement("span");f.style.cssText="width:1px;height:30px;float:left;background-color:#113",p.appendChild(f)}var m=document.createElement("div");m.id="ms",m.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#020;display:none",c.appendChild(m);var g=document.createElement("div");g.id="msText",g.style.cssText="color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px",g.innerHTML="MS",m.appendChild(g);var y=document.createElement("div");for(y.id="msGraph",y.style.cssText="position:relative;width:74px;height:30px;background-color:#0f0",m.appendChild(y);74>y.children.length;){var f=document.createElement("span");f.style.cssText="width:1px;height:30px;float:left;background-color:#131",y.appendChild(f)}var v=function(e){switch(h=e){case 0:u.style.display="block",m.style.display="none";break;case 1:u.style.display="none",m.style.display="block"}},b=function(e,t){var i=e.appendChild(e.firstChild);i.style.height=t+"px"};return{REVISION:11,domElement:c,setMode:v,begin:function(){e=Date.now()},end:function(){var h=Date.now();return i=h-e,n=Math.min(n,i),r=Math.max(r,i),g.textContent=i+" MS ("+n+"-"+r+")",b(y,Math.min(30,30-30*(i/200))),l++,h>t+1e3&&(s=Math.round(1e3*l/(h-t)),o=Math.min(o,s),a=Math.max(a,s),d.textContent=s+" FPS ("+o+"-"+a+")",b(p,Math.min(30,30-30*(s/100))),t=h,l=0),h},update:function(){e=this.end()}}};
    180. </script>
    181. <script>
    182. ;(function(window) {
    183. var ctx,
    184. hue,
    185. logo,
    186. form,
    187. buffer,
    188. target = {},
    189. tendrils = [],
    190. settings = {};
    191. settings.debug = true;
    192. settings.friction = 0.5;
    193. settings.trails = 20;
    194. settings.size = 50;
    195. settings.dampening = 0.25;
    196. settings.tension = 0.98;
    197. Math.TWO_PI = Math.PI * 2;
    198. // ========================================================================================
    199. // Oscillator
    200. // ----------------------------------------------------------------------------------------
    201. function Oscillator(options) {
    202. this.init(options || {});
    203. }
    204. Oscillator.prototype = (function() {
    205. var value = 0;
    206. return {
    207. init: function(options) {
    208. this.phase = options.phase || 0;
    209. this.offset = options.offset || 0;
    210. this.frequency = options.frequency || 0.001;
    211. this.amplitude = options.amplitude || 1;
    212. },
    213. update: function() {
    214. this.phase += this.frequency;
    215. value = this.offset + Math.sin(this.phase) * this.amplitude;
    216. return value;
    217. },
    218. value: function() {
    219. return value;
    220. }
    221. };
    222. })();
    223. // ========================================================================================
    224. // Tendril
    225. // ----------------------------------------------------------------------------------------
    226. function Tendril(options) {
    227. this.init(options || {});
    228. }
    229. Tendril.prototype = (function() {
    230. function Node() {
    231. this.x = 0;
    232. this.y = 0;
    233. this.vy = 0;
    234. this.vx = 0;
    235. }
    236. return {
    237. init: function(options) {
    238. this.spring = options.spring + (Math.random() * 0.1) - 0.05;
    239. this.friction = settings.friction + (Math.random() * 0.01) - 0.005;
    240. this.nodes = [];
    241. for(var i = 0, node; i < settings.size; i++) {
    242. node = new Node();
    243. node.x = target.x;
    244. node.y = target.y;
    245. this.nodes.push(node);
    246. }
    247. },
    248. update: function() {
    249. var spring = this.spring,
    250. node = this.nodes[0];
    251. node.vx += (target.x - node.x) * spring;
    252. node.vy += (target.y - node.y) * spring;
    253. for(var prev, i = 0, n = this.nodes.length; i < n; i++) {
    254. node = this.nodes[i];
    255. if(i > 0) {
    256. prev = this.nodes[i - 1];
    257. node.vx += (prev.x - node.x) * spring;
    258. node.vy += (prev.y - node.y) * spring;
    259. node.vx += prev.vx * settings.dampening;
    260. node.vy += prev.vy * settings.dampening;
    261. }
    262. node.vx *= this.friction;
    263. node.vy *= this.friction;
    264. node.x += node.vx;
    265. node.y += node.vy;
    266. spring *= settings.tension;
    267. }
    268. },
    269. draw: function() {
    270. var x = this.nodes[0].x,
    271. y = this.nodes[0].y,
    272. a, b;
    273. ctx.beginPath();
    274. ctx.moveTo(x, y);
    275. for(var i = 1, n = this.nodes.length - 2; i < n; i++) {
    276. a = this.nodes[i];
    277. b = this.nodes[i + 1];
    278. x = (a.x + b.x) * 0.5;
    279. y = (a.y + b.y) * 0.5;
    280. ctx.quadraticCurveTo(a.x, a.y, x, y);
    281. }
    282. a = this.nodes[i];
    283. b = this.nodes[i + 1];
    284. ctx.quadraticCurveTo(a.x, a.y, b.x, b.y);
    285. ctx.stroke();
    286. ctx.closePath();
    287. }
    288. };
    289. })();
    290. // ----------------------------------------------------------------------------------------
    291. function init(event) {
    292. document.removeEventListener('mousemove', init);
    293. document.removeEventListener('touchstart', init);
    294. document.addEventListener('mousemove', mousemove);
    295. document.addEventListener('touchmove', mousemove);
    296. document.addEventListener('touchstart', touchstart);
    297. mousemove(event);
    298. reset();
    299. loop();
    300. }
    301. function reset() {
    302. tendrils = [];
    303. for(var i = 0; i < settings.trails; i++) {
    304. tendrils.push(new Tendril({
    305. spring: 0.45 + 0.025 * (i / settings.trails)
    306. }));
    307. }
    308. }
    309. function loop() {
    310. if(!ctx.running) return;
    311. ctx.globalCompositeOperation = 'source-over';
    312. ctx.fillStyle = 'rgba(8,5,16,0.4)';
    313. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    314. ctx.globalCompositeOperation = 'lighter';
    315. ctx.strokeStyle = 'hsla(' + Math.round(hue.update()) + ',90%,50%,0.25)';
    316. ctx.lineWidth = 1;
    317. if(ctx.frame % 60 == 0) {
    318. console.log(hue.update(), Math.round(hue.update()), hue.phase, hue.offset, hue.frequency, hue.amplitude);
    319. }
    320. for(var i = 0, tendril; i < settings.trails; i++) {
    321. tendril = tendrils[i];
    322. tendril.update();
    323. tendril.draw();
    324. }
    325. ctx.frame++;
    326. ctx.stats.update();
    327. requestAnimFrame(loop);
    328. }
    329. function resize() {
    330. ctx.canvas.width = window.innerWidth;
    331. ctx.canvas.height = window.innerHeight;
    332. }
    333. function start() {
    334. if(!ctx.running) {
    335. ctx.running = true;
    336. loop();
    337. }
    338. }
    339. function stop() {
    340. ctx.running = false;
    341. }
    342. function mousemove(event) {
    343. if(event.touches) {
    344. target.x = event.touches[0].pageX;
    345. target.y = event.touches[0].pageY;
    346. } else {
    347. target.x = event.clientX
    348. target.y = event.clientY;
    349. }
    350. event.preventDefault();
    351. }
    352. function touchstart(event) {
    353. if(event.touches.length == 1) {
    354. target.x = event.touches[0].pageX;
    355. target.y = event.touches[0].pageY;
    356. }
    357. }
    358. function keyup(event) {
    359. switch(event.keyCode) {
    360. case 32:
    361. save();
    362. break;
    363. default:
    364. // console.log(event.keyCode);
    365. }
    366. }
    367. function letters(id) {
    368. var el = document.getElementById(id),
    369. letters = el.innerHTML.replace('&amp;', '&').split(''),
    370. heading = '';
    371. for(var i = 0, n = letters.length, letter; i < n; i++) {
    372. letter = letters[i].replace('&', '&amp');
    373. heading += letter.trim() ? '<span class="letter-' + i + '">' + letter + '</span>' : '&nbsp;';
    374. }
    375. el.innerHTML = heading;
    376. setTimeout(function() {
    377. el.className = 'transition-in';
    378. }, (Math.random() * 500) + 500);
    379. }
    380. function save() {
    381. if(!buffer) {
    382. buffer = document.createElement('canvas');
    383. buffer.width = screen.availWidth;
    384. buffer.height = screen.availHeight;
    385. buffer.ctx = buffer.getContext('2d');
    386. form = document.createElement('form');
    387. form.method = 'post';
    388. form.input = document.createElement('input');
    389. form.input.type = 'hidden';
    390. form.input.name = 'data';
    391. form.appendChild(form.input);
    392. document.body.appendChild(form);
    393. }
    394. buffer.ctx.fillStyle = 'rgba(8,5,16)';
    395. buffer.ctx.fillRect(0, 0, buffer.width, buffer.height);
    396. buffer.ctx.drawImage(canvas,
    397. Math.round(buffer.width / 2 - canvas.width / 2),
    398. Math.round(buffer.height / 2 - canvas.height / 2)
    399. );
    400. buffer.ctx.drawImage(logo,
    401. Math.round(buffer.width / 2 - logo.width / 4),
    402. Math.round(buffer.height / 2 - logo.height / 4),
    403. logo.width / 2,
    404. logo.height / 2
    405. );
    406. window.open(buffer.toDataURL(), 'wallpaper', 'top=0,left=0,width=' + buffer.width + ',height=' + buffer.height);
    407. // form.input.value = buffer.toDataURL().substr(22);
    408. // form.submit();
    409. }
    410. window.requestAnimFrame = (function() {
    411. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(fn) { window.setTimeout(fn, 1000 / 60) };
    412. })();
    413. window.onload = function() {
    414. ctx = document.getElementById('canvas').getContext('2d');
    415. ctx.stats = new Stats();
    416. ctx.running = true;
    417. ctx.frame = 1;
    418. logo = new Image();
    419. logo.src = 'http://labs.nikrowell.com/lightsandmotion/ultraviolet/images/logo.png';
    420. hue = new Oscillator({
    421. phase: Math.random() * Math.TWO_PI,
    422. amplitude: 85,
    423. frequency: 0.0015,
    424. offset: 285
    425. });
    426. letters('h1');
    427. letters('h2');
    428. document.addEventListener('mousemove', init);
    429. document.addEventListener('touchstart', init);
    430. document.body.addEventListener('orientationchange', resize);
    431. window.addEventListener('resize', resize);
    432. window.addEventListener('keyup', keyup);
    433. window.addEventListener('focus', start);
    434. window.addEventListener('blur', stop);
    435. resize();
    436. if(window.DEBUG) {
    437. var gui = new dat.GUI();
    438. // gui.add(settings, 'debug');
    439. settings.gui.add(settings, 'trails', 1, 30).onChange(reset);
    440. settings.gui.add(settings, 'size', 25, 75).onFinishChange(reset);
    441. settings.gui.add(settings, 'friction', 0.45, 0.55).onFinishChange(reset);
    442. settings.gui.add(settings, 'dampening', 0.01, 0.4).onFinishChange(reset);
    443. settings.gui.add(settings, 'tension', 0.95, 0.999).onFinishChange(reset);
    444. document.body.appendChild(ctx.stats.domElement);
    445. }
    446. };
    447. })(window);
    448. </script>
    449. </body>
    450. </html>

效果图

具体可以上手体验,十分简单,只要会复制粘贴改后缀就行。

总结

       这段代码是一个交互式网页的示例,它利用HTML定义了页面结构,CSS负责页面的样式设计,包括动画和过渡效果,以及一个重置样式来统一不同浏览器的默认样式。JavaScript部分实现了动态效果,包括一个自定义的`Stats`函数来显示性能指标,以及`Oscillator`和`Tendril`类来创建视觉效果。此外,通过监听用户的鼠标和触摸动作,页面能够响应交互。代码中还包含了一些可能用于调试目的的设置,以及一些可能存在问题或需要优化的资源链接。整体而言,该页面通过结合前端技术,提供了视觉动画和用户交互的体验。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/589529
推荐阅读
相关标签
  

闽ICP备14008679号