汤姆猫

汤姆猫
说明:点击汤姆猫的不同部位会做不同的动作发出不同的声音
素材: https://download.csdn.net/download/lollipop_sun/10282500
代码:
<! DOCTYPE html >
< html lang = "en" >

< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta http-equiv = "X-UA-Compatible" content = "ie=edge" >
< title > 汤姆猫 </ title >
< style >
html,
body {
height : 100% ;
margin : 0px ;
overflow : hidden;
}

img {
width : 100% ;
height : 100% ;
}
< / style >
</ head >
<!-- 添加点击事件 -->

< body onclick = "action(event)" >
< img id = 'tom' src = "img\angry\angry_00.jpg" alt = "" >
< audio id = "player" src = "" ></ audio >
< script >
// 使用变量记录当前动画状态,false表示没做动画
var playing = false ;
var tom = document.getElementById( 'tom' )
var player = document.getElementById( 'player' )

var width = document.body.clientWidth
var height = document.body.clientHeight

// console.log(width)
// console.log(height)

// 当窗口大小改变的时候 会调用方法 重新获取宽高
window.onresize = function () {
width = document.body.clientWidth;
height = document.body.clientHeight;
}
// 动画名称 图片张数 播放声音张数
function play(animationName, count, startAt) {

// 如果正在做动画,就返回
if (playing == true ) {
return ;
}
// 如果点击tom而且点击时没有做动画,说明现在是可以做动画的,状态改成true
playing = true
// 声明一个变量作为初始图片的编号
var i = 0 ;
// 切换图片
function doAnimation() {
// var image = 'img/pie/pie_' + (i < 10 ? '0' + i : i) + '.jpg';
var image = 'img/' + animationName + '/' + animationName + '_' + (i < 10 ? '0' + i : i) + '.jpg' ;
// 将图片添加到img标签上
tom.src = image;
i++
if (i == startAt) {
// player.src = 'img/sounds/pie.m4a'
player.src = 'img/sounds/' + animationName + '.m4a'
player.play()
}
if (animationName == 'drink' && i == 16 ) {
player.src = 'img/sounds/pour.m4a'
player.play()
}
if (animationName == 'knockout' && i == 1 ) {
player.src = 'img/sounds/fall.m4a'
}
if (i < count) {
setTimeout(doAnimation, 100 );
}
else {
playing = false ;
}
}
doAnimation();
}
function action(event) {
// event.pageX鼠标的X坐标
// event.pageY鼠标的Y坐标
// 1440 1080 屏幕的宽一般是360的倍数
var pointX = event.pageX * 360 / width;
var pointY = event.pageY * 640 / height;

console.log(pointX);
console.log(pointY);

if (pointX > 140 && pointX < 225 && pointY > 400 && pointY < 450 ) {
// 将参数对应的传到play函数中
play( 'angry' , 25 , 0 )
}

if (pointX > 119 && pointX < 245 && pointY > 465 && pointY < 565 ) {
play( 'stomach' , 33 , 0 )
}

if (pointX > 155 && pointX < 210 && pointY > 190 && pointY < 230 ) {
play( 'knockout' , 80 , 13 )
}

if (pointX > 137 && pointX < 235 && pointY > 250 && pointY < 280 ) {
if (pointX < 185 )
play( 'eat' , 39 , 0 )
else
play( 'drink' , 80 , 30 )
}

if (pointX > 124 && pointX < 240 && pointY > 160 && pointY < 190 ) {
play( 'cymbal' , 12 , 0 )
}

if (pointX > 246 && pointX < 280 && pointY > 490 && pointY < 580 ) {
play( 'fart' , 27 , 0 )
}

if (pointX > 80 && pointX < 120 && pointY > 110 && pointY < 195 ) {
play( 'scratch' , 55 , 15 )
}

if (pointX > 240 && pointX < 290 && pointY > 110 && pointY < 200 ) {
play( 'pie' , 24 , 12 )
}

if (pointX > 125 && pointX < 175 && pointY > 570 && pointY < 620 ) {
play( 'foot_right' , 29 , 0 )
}

if (pointX > 180 && pointX < 230 && pointY > 570 && pointY < 620 ) {
play( 'foot_left' , 29 , 3 )
}
}
< / script >
</ body >

</ html >

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值