jQuery实现小球点击发射动画

 更新时间:2022年01月14日 13:28:31   作者:前后端杂货铺  
这篇文章主要为大家详细介绍了jQuery实现小球点击发射动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天花了两个小时使用jQuery写了一个小动画游戏,如下图所示,通过鼠标点击,发射球。

代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            #main {
                width: 500px;
                height: 650px;
                border: 3px solid #efefef;
                margin: 30px auto;
                position: relative;
            }

            #fireSpace {
                width: 100%;
                height: 400px;
                position: absolute;
                top: 0;
                left: 0;

            }

            #gun {
                display: block;
                width: 60px;
                height: 60px;
                position: absolute;
                bottom: 20px;
                left: 50%;
                transform: translate(-50%, 0);
            }
        </style>
    </head>
    <body>
        <div id="main">
            <div id="fireSpace">

            </div>
            <img src="./gun.png" id="gun">
        </div>
    </body>
</html>

<script src="./jquery.js"></script>
<script>
    let initX = 0,
        initY = 300,
        initDeg = 90,
        thenDeg, gunX, gunY, boo, x = 0,
        y = 300,
        nx, ny, dg = 90,
        ndg, rdg, isLeft0, isLeft;
    document.getElementById("fireSpace").onmousemove = function(e) {
        if (e.offsetX - 220 >= 0) {
            // nx = e.offsetX - 220;
            // ny = 600-e.offsetY;
            gunX = e.offsetX - 220;
            isLeft = false;
        } else if (e.offsetX - 220 <= 0) {
            gunX = 220 - e.offsetX;
            isLeft = true;
        }
        gunY = 650 - e.offsetY;
        if (e.offsetX - 220 == 0) {
            thenDeg = 90;
        } else {
            thenDeg = gunY - gunX >= 0 ? (90 - Math.asin(gunX / gunY) * 180 / Math.PI) : (Math.asin(gunY / gunX) *
                180 / Math.PI);
            // thenDeg = Math.asin(gunY / gunX) * 180 / Math.PI;
        }
        if (initX - 220 == 0) {
            initDeg = 90;
        } else {
            initDeg = initY - initX >= 0 ? (90 - Math.asin(initX / initY) * 180 / Math.PI) : (Math.asin(initY /
                    initX) *
                180 / Math.PI);
        }
        if (initY <= 3) {
            initDeg = 0;
        }
        if (gunY <= 3) {
            thenDeg = 0;
        }
        if (!isLeft0 && isLeft) {
            rdg = -(180 - initDeg - thenDeg);
        } else if (isLeft0 && !isLeft) {
            rdg = 180 - initDeg - thenDeg;
        } else if (isLeft0 && isLeft) {
            rdg = (thenDeg - initDeg)
        } else if (!isLeft0 && !isLeft) {
            rdg = (initDeg - thenDeg)
        }
        document.getElementById("gun").style.transform = "translate(-50%, 0) rotate(" + rdg + "deg)";
        x = nx;
        y = ny;
        isLeft0 = isLeft;
    }
    let fireX,fireY,iX=0,iY=0
    document.getElementById("fireSpace").onclick = function(e) {
        fireX = e.offsetX;
        fireY = e.offsetY;
        let boll = document.createElement("img");
        boll.style.width = "50px";
        boll.style.height = "50px";
        boll.setAttribute("src", "./boll.png");
        boll.style.position = "absolute";
        boll.style.bottom = "0";
        boll.style.left = "50%";
        boll.style.transform = "translate(-40%,0)";
        boll.style.zIndex = "-1";
        document.getElementById("main").appendChild(boll);
        $(boll).animate({
            top: fireY,
            left: fireX
        }, 1000);
        setTimeout(function() {
            boll.parentNode.removeChild(boll);
        }, 1000);
    }
</script>

图片素材:

感兴趣的的小伙伴可以去试试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于Jquery的仿照flash放大图片效果代码

    基于Jquery的仿照flash放大图片效果代码

    基于Jquery的仿照flash放大图片效果代码,需要的朋友可以参考下。
    2011-03-03
  • 高效的jQuery代码编写技巧总结

    高效的jQuery代码编写技巧总结

    好的代码会带来速度的提升,快速渲染和响应意味着更好的用户体验。本文主要总结了如何高效的编写jQuery代码的技巧,对提升你的jQuery和javascript代码具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 使用jQuery.Pin垂直滚动时固定导航

    使用jQuery.Pin垂直滚动时固定导航

    这篇文章主要为大家详细介绍了使用jQuery.Pin垂直滚动时固定导航的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • jquery+正则实现统一的表单验证

    jquery+正则实现统一的表单验证

    表单验证一直很繁琐,特别是大点的表单,如果每个input都去单独写验证简直要写死人,最近写了一小段js统一的验证表单内容是否正确。需要的朋友可以参考下
    2015-09-09
  • JQuery包裹DOM节点的方法

    JQuery包裹DOM节点的方法

    这篇文章主要介绍了JQuery包裹DOM节点的方法,实例分析了wrap与wrapAll方法实现包裹DOM节点的技巧与相关注意事项,需要的朋友可以参考下
    2015-06-06
  • Jquery中Event对象属性小结

    Jquery中Event对象属性小结

    这篇文章主要介绍了Jquery中Event对象属性的操作方法的总结,非常的详细,是篇十分不错的文章,这里推荐给大家。
    2015-02-02
  • jQuery跨域问题解决方案

    jQuery跨域问题解决方案

    通过XMLHTTPRquest请求不同域上的数据,原来js跨域访问是后台有个处理路径“/test”的函数。下面给大家介绍jQuery跨域问题解决方案,有需要的小伙伴可以参考下
    2015-08-08
  • jquery 学习笔记一

    jquery 学习笔记一

    jquery是javascript的类库,提供了大量的javascript的类库和API,方便javascript开发。
    2010-04-04
  • jquery淡化版banner异步图片文字效果切换图片特效

    jquery淡化版banner异步图片文字效果切换图片特效

    全屏淡入淡出简洁banner,异步图片文字效果切换图片特效,需要的朋友可以参考下
    2014-04-04
  • JQ选择器_选择同类元素的第N个子元素的实现方法

    JQ选择器_选择同类元素的第N个子元素的实现方法

    下面小编就为大家带来一篇JQ选择器_选择同类元素的第N个子元素的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09

最新评论