红包飞过效果

点击产生红包,到指定位置


这里写图片描述

如图所示,点击按钮就产生红包,然后飞向指定位置,需要有一个斜线的角度,还需要知道点击的位置和红包的位置

html代码

<body>
    <ul class="ul">
        <li class=""><button onclick="hong.getBao(event,this)">点击置评</button></li>
        <li class=""><button onclick="hong.getBao(event,this)">点击置评</button></li>
        <li class=""><button onclick="hong.getBao(event,this)">点击置评</button></li>
        <li class=""><button onclick="hong.getBao(event,this)">点击置评</button></li>
    </ul>
    <div class="hongbao">
        <p class="text-center">88.88</p>
        <p class="text-center">恭喜获得红包</p>
    </div>
</body>

css代码

ul{
	width: 1200px;
	background-color: #ffffff;
	margin: 30px auto;
	padding: 20px 30px;
}
ul li{
	list-style: none;
	padding: 20px;
	border:1px solid #ebebeb;
}
.text-center{
	text-align: center;
}
.hongbao{
	position: fixed;
	bottom: 30px;
	right: 30px;
	width: 100px;
	height: 100px;
	background-color: red;
	color: #fff;
}
.package{
	position: fixed;
}

JS代码

var hong={
    getBao:function(event,ob){
        // alert('ces');

        var package='<p class="package"><img src="images/hong.png"/></p>';
        $(ob).parent().append(package);
        // var win_height=$(document).height()-80;  //屏幕的高度
        // var win_width=$(document).width()-80;  //屏幕的宽度
        // console.log(event.x+','+event.y+','+win_height+','+win_width);
        // $(".hongbao").position().top;  //红包的高度
        // $(".hongbao").position().left; //红包的宽度
        // console.log($(".hongbao").position().top+','+$(".hongbao").position().left);
        //

        var diff_x = event.x - $(".hongbao").position().left;
        var diff_y = event.y - $(".hongbao").position().top;
        var data = 360*Math.atan(diff_y/diff_x)/(2*Math.PI);   //获取红包飞的角度
        // console.log(data);
        $('.package').css({
            'position':'fixed',
            'left': event.x + 'px',
            'top' : event.y + 'px',
            'transform': "rotate("+data+"deg)"
        });
        $('.package').animate({
            'left':$(".hongbao").position().left +'px',
            'top':$(".hongbao").position().top+'px'
        },9800,function(){
            $(ob).parents().children('.package').remove();
        });

    }
}

知识点摘要
  1. 点击传过来this ,可以在方法里面传递参数
  2. 一开始使用的是屏幕的高度和宽度,然后减去固定的数值来确定红包飞向的位置,但是会在屏幕出现滚动条的时候有BUG,修改之后可以利用更加简便的,大红包位置来确定
  3. 对于数学函数的复习和使用
  4. ainimate函数的参数,最后一个参数可以在动画完成之后指定下一步动作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值