每天一个不秃头的HTML+JS+CSS案例

每天一个不秃头的HTML+JS+CSS案例之在网页底部养鱼

效果图
效果实现图

html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>在网页底部养鱼</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="fish-container" class="container"></div>

<!-- partial -->
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script><script src="./script.js"></script>
</body>
</html>

css文件

html, body{
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.container{
  width: 100%;
  height: 50%;
  transform: translateY(450px);
  margin: 0;
  padding: 0;
  background-color: #FFFFFF;
}

js文件

// 使用到的参数
var RENDERER = {
	POINT_INTERVAL : 10,
	FISH_COUNT : 2,
	MAX_INTERVAL_COUNT : 40,
	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.$container = $('#fish-container');
		this.$canvas = $('<canvas />');
		this.context = this.$canvas.appendTo(this.$container).get(0).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.width();
		this.height = this.$container.height();
		this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500;
		this.$canvas.attr({width : this.width, 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.on('resize', this.watchWindowSize);
		this.$container.on('mouseenter', this.startEpicenter);
		this.$container.on('mousemove', this.moveEpicenter);
		this.$container.on('click', this.reverseVertical);
	},
	getAxis : function(event){
		var offset = this.$container.offset();
		
		return {
			x : event.clientX - offset.left + this.$window.scrollLeft(),
			y : event.clientY - offset.top + this.$window.scrollTop()
		};
	},
	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%, 0%)';
		
		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.8,
	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);
	}
};

$(function(){
	RENDERER.init();
});
以下是一个简单的电影网页的HTMLCSSJavaScript代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>电影网页</title> <style> body { margin: 0; padding: 0; background-color: #f6f6f6; font-family: Arial, sans-serif; } header { background-color: #dd5555; color: #fff; padding: 20px; text-align: center; font-size: 36px; } nav { background-color: #333; color: #fff; display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; font-size: 20px; } nav a { color: #fff; text-decoration: none; margin-left: 20px; transition: all 0.3s ease; } nav a:hover { color: #dd5555; } .container { max-width: 1200px; margin: 20px auto; display: flex; flex-wrap: wrap; justify-content: center; align-items: stretch; } .movie { width: 300px; margin: 20px; background-color: #fff; border-radius: 10px; box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2); overflow: hidden; position: relative; } .movie img { width: 100%; height: 300px; object-fit: cover; object-position: center; } .movie h3 { font-size: 24px; padding: 10px; margin: 0; color: #333; text-align: center; } .movie p { font-size: 16px; padding: 0 20px; margin: 10px 0; color: #666; text-align: justify; height: 100px; overflow: hidden; } .movie:hover { box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.4); transform: translateY(-10px); } .movie:hover:after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 1; } .movie:hover h3, .movie:hover p { color: #fff; z-index: 2; } .movie:hover p { height: auto; } .movie:hover .btn { transform: translateY(0); } .btn { background-color: #dd5555; color: #fff; padding: 10px 20px; border-radius: 20px; position: absolute; bottom: -30px; left: 50%; transform: translateX(-50%) translateY(20px); transition: all 0.3s ease; z-index: 3; cursor: pointer; } .btn:hover { background-color: #fff; color: #dd5555; box-shadow: 0 0 5px rgba(0, 0, 0, 0.4); } </style> </head> <body> <header> 电影网页 </header> <nav> <a href="#">首页</a> <a href="#">电影分类</a> <a href="#">最新电影</a> <a href="#">我的收藏</a> <a href="#">关于我们</a> </nav> <div class="container"> <div class="movie"> <img src="https://picsum.photos/id/237/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> <div class="movie"> <img src="https://picsum.photos/id/238/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> <div class="movie"> <img src="https://picsum.photos/id/239/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> <div class="movie"> <img src="https://picsum.photos/id/240/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> <div class="movie"> <img src="https://picsum.photos/id/241/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> <div class="movie"> <img src="https://picsum.photos/id/242/300/300" alt="电影海报"> <h3>电影标题</h3> <p>电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介,电影简介。</p> <a href="#" class="btn">查看详情</a> </div> </div> <script> // 可以在这里添加一些JavaScript代码,比如实现搜索功能、轮播图等等。 </script> </body> </html> ``` 在上面的代码中,我们使用了HTMLCSS来创建一个简单的电影网页。该网页包含了一个顶部的标题栏和一个导航栏,以及一个电影列表。电影列表中的每个电影块都包含了电影海报、电影标题、电影简介和一个查看详情的按钮。当鼠标悬停在电影块上时,电影块会有一个简单的动画效果,并可以展开电影简介和点击查看详情。此外,我们还可以在JavaScript中添加一些功能,比如搜索、轮播图等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值