笔者也是在网上找了很多文章,大部分都是基于Jquery实现的,但是项目中其他地方并不需要用到Jquery,为了这么一个特效而引入Jquery的话,并不划算。

于是在下载到源码之后,做了一定的修改,将JS里面Jquery的语法改为原生JS来实现。

Html   很简单

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head><meta charset="utf-8" /><title></title><style>.container {width: 100%;height: 200px;position: fixed;z-index: -1;opacity: 0.37;bottom: 0;left: 0;}</style><script src='renderer.js'></script>
</head><body><div id="jsi-flying-fish-container" class="container"></div><script>window.onload = () =>{RENDERER.init();}</script>
</body></html>

renderer.js  约350行

var RENDERER = {POINT_INTERVAL : 5,FISH_COUNT : 3,MAX_INTERVAL_COUNT : 50,INIT_HEIGHT_RATE : 0.5,THRESHOLD : 50,init : function(){this.setParameters();this.reconstructMethods();this.setup();this.bindEvent();this.render();},setParameters : function(){this.$window = window;this.$document = document.bodythis.$container = document.getElementById('jsi-flying-fish-container');this.$canvas = document.createElement('canvas');this.$container.appendChild(this.$canvas)this.context = this.$canvas.getContext('2d');this.points = [];this.fishes = [];this.watchIds = [];},createSurfacePoints : function(){var count = Math.round(this.width / this.POINT_INTERVAL);this.pointInterval = this.width / (count - 1);this.points.push(new SURFACE_POINT(this, 0));for(var i = 1; i < count; i++){var point = new SURFACE_POINT(this, i * this.pointInterval),previous = this.points[i - 1];point.setPreviousPoint(previous);previous.setNextPoint(point);this.points.push(point);}},reconstructMethods : function(){this.watchWindowSize = this.watchWindowSize.bind(this);this.jdugeToStopResize = this.jdugeToStopResize.bind(this);this.startEpicenter = this.startEpicenter.bind(this);this.moveEpicenter = this.moveEpicenter.bind(this);this.reverseVertical = this.reverseVertical.bind(this);this.render = this.render.bind(this);},setup : function(){this.points.length = 0;this.fishes.length = 0;this.watchIds.length = 0;this.intervalCount = this.MAX_INTERVAL_COUNT;this.width = this.$container.offsetWidth;this.height = this.$container.offsetHeight;this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500;this.$canvas.width = this.width;this.$canvas.height = this.height;this.reverse = false;this.fishes.push(new FISH(this));this.createSurfacePoints();},watchWindowSize : function(){this.clearTimer();this.tmpWidth = this.$window.width;this.tmpHeight = this.$window.height;this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL));},clearTimer : function(){while(this.watchIds.length > 0){clearTimeout(this.watchIds.pop());}},jdugeToStopResize : function(){var width = this.$window.width(),height = this.$window.height(),stopped = (width == this.tmpWidth && height == this.tmpHeight);this.tmpWidth = width;this.tmpHeight = height;if(stopped){this.setup();}},bindEvent : function(){this.$window.onresize = this.watchWindowSize;this.$container.onclick = this.reverseVertical;this.$container.onmouseenter = this.startEpicenter;this.$container.addEventListener('onmousemove', this.moveEpicenter);},getAxis : function(event){var offset = this.getOffset(this.$container);return {x : event.clientX - offset.left + this.$document.scrollLeft,y : event.clientY - offset.top + this.$document.scrollTop};},getOffset: function(Node, offset) {    if (!offset) {        offset = {};offset.top = 0; offset.left = 0;}if (Node == document.body) {//当该节点为body节点时,结束递归        return offset;   }offset.top += Node.offsetTop;    offset.left += Node.offsetLeft;return this.getOffset(Node.parentNode, offset);//向上累加offset里的值},startEpicenter : function(event){this.axis = this.getAxis(event);},moveEpicenter : function(event){var axis = this.getAxis(event);if(!this.axis){this.axis = axis;}this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y);this.axis = axis;},generateEpicenter : function(x, y, velocity){if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){return;}var index = Math.round(x / this.pointInterval);if(index < 0 || index >= this.points.length){return;}this.points[index].interfere(y, velocity);},reverseVertical : function(){this.reverse = !this.reverse;for(var i = 0, count = this.fishes.length; i < count; i++){this.fishes[i].reverseVertical();}},controlStatus : function(){for(var i = 0, count = this.points.length; i < count; i++){this.points[i].updateSelf();}for(var i = 0, count = this.points.length; i < count; i++){this.points[i].updateNeighbors();}if(this.fishes.length < this.fishCount){if(--this.intervalCount == 0){this.intervalCount = this.MAX_INTERVAL_COUNT;this.fishes.push(new FISH(this));}}},render : function(){requestAnimationFrame(this.render);this.controlStatus();this.context.clearRect(0, 0, this.width, this.height);this.context.fillStyle = 'hsl(0, 0%, 95%)';for(var i = 0, count = this.fishes.length; i < count; i++){this.fishes[i].render(this.context);}this.context.save();this.context.globalCompositeOperation = 'xor';this.context.beginPath();this.context.moveTo(0, this.reverse ? 0 : this.height);for(var i = 0, count = this.points.length; i < count; i++){this.points[i].render(this.context);}this.context.lineTo(this.width, this.reverse ? 0 : this.height);this.context.closePath();this.context.fill();this.context.restore();}
};
var SURFACE_POINT = function(renderer, x){this.renderer = renderer;this.x = x;this.init();
};
SURFACE_POINT.prototype = {SPRING_CONSTANT : 0.03,SPRING_FRICTION : 0.9,WAVE_SPREAD : 0.3,ACCELARATION_RATE : 0.01,init : function(){this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE;this.height = this.initHeight;this.fy = 0;this.force = {previous : 0, next : 0};},setPreviousPoint : function(previous){this.previous = previous;},setNextPoint : function(next){this.next = next;},interfere : function(y, velocity){this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity);},updateSelf : function(){this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height);this.fy *= this.SPRING_FRICTION;this.height += this.fy;},updateNeighbors : function(){if(this.previous){this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height);}if(this.next){this.force.next = this.WAVE_SPREAD * (this.height - this.next.height);}},render : function(context){if(this.previous){this.previous.height += this.force.previous;this.previous.fy += this.force.previous;}if(this.next){this.next.height += this.force.next;this.next.fy += this.force.next;}context.lineTo(this.x, this.renderer.height - this.height);}
};
var FISH = function(renderer){this.renderer = renderer;this.init();
};
FISH.prototype = {GRAVITY : 0.4,init : function(){this.direction = Math.random() < 0.5;this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD;this.previousY = this.y;this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1);if(this.renderer.reverse){this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10);this.vy = this.getRandomValue(2, 5);this.ay = this.getRandomValue(0.05, 0.2);}else{this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10);this.vy = this.getRandomValue(-5, -2);this.ay = this.getRandomValue(-0.2, -0.05);}this.isOut = false;this.theta = 0;this.phi = 0;},getRandomValue : function(min, max){return min + (max - min) * Math.random();},reverseVertical : function(){this.isOut = !this.isOut;this.ay *= -1;},controlStatus : function(context){this.previousY = this.y;this.x += this.vx;this.y += this.vy;this.vy += this.ay;if(this.renderer.reverse){if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){this.vy -= this.GRAVITY;this.isOut = true;}else{if(this.isOut){this.ay = this.getRandomValue(0.05, 0.2);}this.isOut = false;}}else{if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){this.vy += this.GRAVITY;this.isOut = true;}else{if(this.isOut){this.ay = this.getRandomValue(-0.2, -0.05);}this.isOut = false;}}if(!this.isOut){this.theta += Math.PI / 20;this.theta %= Math.PI * 2;this.phi += Math.PI / 30;this.phi %= Math.PI * 2;}this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY);if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){this.init();}},render : function(context){context.save();context.translate(this.x, this.y);context.rotate(Math.PI + Math.atan2(this.vy, this.vx));context.scale(1, this.direction ? 1 : -1);context.beginPath();context.moveTo(-30, 0);context.bezierCurveTo(-20, 15, 15, 10, 40, 0);context.bezierCurveTo(15, -10, -20, -15, -30, 0);context.fill();context.save();context.translate(40, 0);context.scale(0.9 + 0.2 * Math.sin(this.theta), 1);context.beginPath();context.moveTo(0, 0);context.quadraticCurveTo(5, 10, 20, 8);context.quadraticCurveTo(12, 5, 10, 0);context.quadraticCurveTo(12, -5, 20, -8);context.quadraticCurveTo(5, -10, 0, 0);context.fill();context.restore();context.save();context.translate(-3, 0);context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1));context.beginPath();if(this.renderer.reverse){context.moveTo(5, 0);context.bezierCurveTo(10, 10, 10, 30, 0, 40);context.bezierCurveTo(-12, 25, -8, 10, 0, 0);}else{context.moveTo(-5, 0);context.bezierCurveTo(-10, -10, -10, -30, 0, -40);context.bezierCurveTo(12, -25, 8, -10, 0, 0);}context.closePath();context.fill();context.restore();context.restore();this.controlStatus(context);}
};

效果如图

网页底部小鱼游动特效相关推荐

  1. 手机(wap)网页底部固定悬浮广告带轮播特效代码

    // 作者:xycms // 网址:http://wwww.jsxyidc.com // 日期:2019-03-3 // QQ:364500483 // code:网页底部悬浮广告代码,带单独关闭va ...

  2. html5 文字滑动特效代码,三种网页状态栏文字滚动特效代码

    网页状态栏底部滚动文字特效代码 文字滚动特效代码一: >

  3. css方法div固定在网页底部

    css .bottom{width:100%;height:40px;background:#ededed;float:left;margin-bottom:0px;position:fixed;bo ...

  4. 用HTML和CSS3做个鱼,如何使用CSS和D3实现小鱼游动的交互动画(附代码)

    本篇文章给大家带来的内容是关于如何使用CSS和D3实现小鱼游动的交互动画(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 效果预览 源代码下载 https://github. ...

  5. 微信开发 Weixin JS接口 隐藏微信中网页底部导航栏

    公众号在有需要时(如认为用户在该页面不会用到浏览器前进后退功能),可在网页中通过JavaScript代码隐藏网页底部导航栏. 接口调用代码(JavaScript) document.addEventL ...

  6. 网页鼠标点击特效案例收集(直播间红心同理)

    1. 鼠标点击出随机颜色的爱心 <!DOCTYPE html> <html lang="en"> <head><meta charset= ...

  7. html里按钮始终在底部,详解footer始终位于网页底部的方法介绍

    上次说把网页的头部和尾部分离出来作为一个单独的文件,所有网页共用,这样比较方便修改,然而,,,我发现某些方法里尾部会紧跟在头部后面,把内容挤在下面..而且有的页面内容少的话不能把尾部挤到最下面,所以, ...

  8. 【CSS】课程网站网页底部开发 ( 网页底部盒子模型测量及样式 | 代码示例 )

    文章目录 一.网页底部盒子模型测量及样式 1.盒子布局说明 2.底部的大盒子测量及样式 3.版心盒子 4.版权盒子 5.链接盒子 二.代码示例 1.HTML 标签结构 2.CSS 样式 3.显示效果 ...

  9. HTML5七夕情人节表白网页(庆祝生日蛋糕烟花特效) HTML+CSS+JavaScript

    HTML5七夕情人节表白网页❤庆祝生日蛋糕烟花特效❤ HTML+CSS+JavaScript 这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看. 此波共 ...

  10. html div页面固定,将div固定浮动网页底部代码

    div固定浮动网页底部CSS代码,非JS实现纯DIV CSS构造将DIV层静止浮动在网页阅读器底部的. CSS代码: position: fixed;bottom: 0;z-index: 100 正文 ...

最新文章

  1. 打造标杆,中科院人工智能战队发布新一代智算平台
  2. 【Linux 内核 内存管理】优化内存屏障 ① ( barrier 优化屏障 | 编译器优化 | CPU 执行优化 | 优化屏障源码 barrier 宏 )
  3. python-15:装饰函数之一
  4. combobox异步加载 easyui_如何解决多条数据加载easyui-combobox样式反应慢的问题
  5. SQL查询效率:100万数据查询只需要1秒钟
  6. mysql:Java通过驱动包(jar包)连接MySQL数据库---步骤总结及验证
  7. MongoVUE 使用教程
  8. Croe文件在线预览
  9. 系统封装 如何加载PE到Easyboot进行合盘
  10. scrapy项目:爬取豆瓣畅销书排行榜内容(仅爬取2020年1-3页:无保存)
  11. 【python】【Gif制作】使用多张图片合成gif动图
  12. pcntl php windows_PHP各版本安装pcntl扩展
  13. Java利用数组求某年某日某月是某年的第几天(数组)
  14. 取消双Shift全局搜索
  15. Mac OS X pl2303 的驱动下载
  16. pdf修改文字内容怎么修改
  17. 淘宝API接口(item_sku - 获取sku详细信息)
  18. 在调试器下理解RK3588和LINUX5.10
  19. 最详细xmind绘制思维导图操作
  20. GPRS对比CDMA

热门文章

  1. Ruby中yield和block的用法
  2. ElasticSearch分词器IK安装教程
  3. (十二)c#Winform自定义控件-分页控件
  4. DTD-文档类型定义(Document Type Definition)
  5. 云烟阁--Java8系列之函数式接口和Lambda表达式(一)
  6. Activity设置透明主题
  7. 后渗透阶段的权限维持
  8. pytorch实现GAN
  9. java调用python库pyd_Java怎么调用pyd文件
  10. 一个记账易app开发