卡通小人的眼睛跟着鼠标动

 来源 ,我做了一些改变。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>眼镜随着鼠标动-模仿</title>
	<link rel="stylesheet" type="text/css" href="2.css">
	<script type="text/javascript" src="2.js"></script>
</head>
<body>
	<div id="wrapper">
			<div class="face" id="blue">
				<div class="eyes">
					<div class="eye-left">
						<div class="eye-in"></div>
					</div>
					<div class="eye-right">
						<div class="eye-in"></div>
					</div>
				</div>
				<div class="nose">
					<a></a>
					<a></a>
				</div>
				<div class="mouth">
					<div class="tooth" id="tooth1"></div>
					<div class="tooth" id="tooth2"></div>
					<div class="tooth" id="tooth3"></div>
					<div class="tooth" id="tooth4"></div>

				</div>
			</div>
	</div>
</body>
</html>

*{
	margin: 0;
	padding: 0;
}

div#wrapper{
	width:100%;
	height: 1500px;   /*height:100%;*/

	background-color: #eee;

	text-align: center;
}
div.face{
	position: relative;
	top: 20%;


	margin-left: 0px;
	margin-right: 0px;

	width: 200px;
	height: 250px;
	display: inline-block;
	overflow: hidden;


	background-color: #2db5ae;

	border-radius: 45% 45% 0 0 ;
}


div.eyes{
	position: relative;
	top: 70px;
	width: 100%;
	height: 80px;

	background-color: #2db5ae;
}

div.eye-left{

	position: absolute;
	left: 10%;
	
	overflow: hidden;
	width: 35%;
	height: 100%;

	background-color: white;
	border-radius: 45% 45% 55% 55%;
}
div.eye-right{

	position: absolute;
	right: 10%;

	overflow: hidden;
	width: 35%;
	height: 100%;

	
	background-color: white;
	border-radius: 45% 45% 55% 55%;
}

div.eye-in{
	position: relative;

	display: inline-block;
	
	width: 45%;   
	height: 55%;
	
	margin-right: 0;
	margin-left: 0;
	margin-top: 20px;

	background-color: black;

	border-radius: 50% 50%;    /*border-radius的弧度大小与宽度高度有关*/
								/*四个值依次控制的是哪个角。*/
}

div.nose{
	position: relative;
	top: 32%;
	text-align: center;

}

div.nose a{

	display: inline-block;
	background-color: white;
	height: 10px;
	width: 10px;

	border-radius: 5px;
}

div.mouth{ /*为什么会卡一半!??????*/
	position: relative;
	top:90px;
	width: 100%;
	height: 25%;
	
	display: inline-block;
	overflow: hidden;
	/*margin-left: 0px;
	margin-right: 0px;*/
	
	text-align: center;
	background-color: #614030;

	border-radius: 81% 5% 81% 5%;
}

div.tooth{
	position: relative;
	/*top: -5px;*/
	
	height: 50px;
	width: 35px;
	
	display: inline-block;
	margin-left: -7px;

	background-color: white;
	
	border-radius: 0 0 50% 50%;

}

div#tooth1{
	/*height: 50px;*/
}
div#tooth2{
	/*height: 45px;*/
	top: -5px;
}
div#tooth3{
	/*height: 40px;*/
	top: -10px;
}
div#tooth4{
	/*height: 35px;*/
	top: -15px;
}


window.onload = function(){
	
	// first Step  

	// 动态语言直接 var eyes = document.querySelectorAll(".eye-in");
	var eyes = [];
	eyes = document.querySelectorAll(".eye-in");

	// second Step	为元素添加事件处理
	document.onmousemove = function(event){
		var mouseLeft = event.clientX,
			mouseTop = event.clientY;
		// console.log(getViewportSize().height);

		throttle(function(){   
				$forEach(eyes, function(o, i){
						var chaX = mouseLeft - getOffset(eyes[i]).left;
						var chaY = mouseTop  - getOffset(eyes[i]).top;
						var viewportWidth = getViewportSize().width /2;
						var viewportHeight = getViewportSize().height /2;
						//mouseLeft 与 mouseTop的值有时候太小,导致差值太小
						eyes[i].style.left = ((chaX / viewportWidth)*50) + "%";  
						eyes[i].style.top  = ((chaY / viewportHeight)*50) + "%";
				});
			}
		);
		// clearTimeout(tId);

		// var tId = function(){
		// 	setTimeout(function(){
		// 		$forEach(eyes, function(o, i){
		// 			var chaX = mouseLeft - getOffset(eyes[i]).left;
		// 			var chaY = mouseTop  - getOffset(eyes[i]).top;
		// 			var viewportWidth = getViewportSize().width /2;
		// 			var viewportHeight = getViewportSize().height /2;
		// 			//mouseLeft 与 mouseTop的值有时候太小,导致差值太小
		// 			eyes[i].style.left = ((chaX / viewportWidth)*50) + "%";  
		// 			eyes[i].style.top  = ((chaY / viewportHeight)*50) + "%";
		// 		});
		// 	},70);
		// }();
			
	};





	// 要用到的函数
	function throttle(method, context){
		clearTimeout(method.tId);
		method.tId = setTimeout(function(){
			method.call(context);
		}, 70);
	}

	
	function $forEach(arr, callback, thisp){
		if (arr.forEach) {
			arr.forEach(callback, thisp);
		} else {
			for (var i = 0, len=arr.length; i<len; i++){
				callback.call(thisp, arr[i], i, arr);
			}
		}
	}


	// 单位????
	function getOffset(element){
		var actualLeft = element.offsetLeft;
		var actualTop  = element.offsetTop;
		// var actualTop = element.offsetWidth / 2;
		// var actualLeft = element.offsetHeight /2;
		var current = element.offsetParent;

		while(current !== null){
			actualTop += current.offsetTop;
			actualLeft += current.offsetLeft;
			current = current.offsetParent;
		}

		return {left: actualLeft,top:actualTop};
	}

	function getViewportSize(){
		var pageWidth = window.innerWidth,
			pageHeight = window.innerHeight;
		
		if (typeof pageWidth != "number") {
				pageWidth = document.documentElement.clientWidth;
				pageHeight = document.documentElement.clientHeight;
		}else {
			pageWidth = document.body.clientWidth;
			pageHeight = document.body.clientHeight;
		}

		// console.log(pageWidth);
		return {width:pageWidth,height:pageHeight}	
	}
	


};





 
  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity卡通小人模型是一种使用Unity引擎进行开发的3D模型,它具有卡通风格的外观和画。这种模型通常被用于游戏开发、虚拟现实和画制作等领域。 Unity卡通小人模型的制作过程一般包括以下几个步骤:首先是进行角色设计,确定小人的外貌特征、服装风格和作表情等。其次是进行3D建模,使用建模软件创建人物的基本形状和细节,并将其导入Unity中以供后续操作。接下来是贴图和材质的设置,为小人模型添加颜色和纹理,使其更加真实和生。然后是骨骼绑定与画制作,给小人模型添加骨骼,通过对骨骼进行操作和控制,实现不同作的切换和表现。最后是调整和优化,对小人模型的外观、材质和画进行调整和优化,以提高性能和画质。 Unity卡通小人模型的应用非常广泛。在游戏开发中,卡通小人模型可以作为游戏角色或角色配角出现,给游戏增加趣味性和可玩性。在虚拟现实领域,卡通小人模型可以用于虚拟人物的表现和交互,使用户沉浸于虚拟世界中。在画制作中,卡通小人模型可以通过不同的画表达情感和故事,创造出生有趣的画片段。 总的来说,Unity卡通小人模型是一种利用Unity引擎制作的3D模型,具有卡通风格的外观和画,广泛应用于游戏开发、虚拟现实和画制作等领域,为用户带来更多的创意和乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值