css :
1 body{width:1000px; height:500px;}
input{ margin:100px; width:250px; height: 50px }
3 .Fireworks{width:4px; height: 4px; position: absolute;}
.heart{width: 10px;height: 10px;position: fixed;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}
js: <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
js:
1 $(function(){
var input_e=$("input");
//javascript检测浏览器是否支持某些事件
//typeof input["oninput"] == "undefined" var y=input_e.offset().top;
var x=input_e.offset().left;
var len=0;
var firewNum=15;// 烟花个数
// 点击页面 在鼠标位置 出现心形 烟花
$("body").click(function(ev){
xin(ev); // 点击心形
Fireworks(ev)// 点击烟花
}); // 鼠标点击出现心形
function xin(ev)
{
var color=randColor();
var div=document.createElement("div");
div.className='heart';
div.style.backgroundColor=color;
div.style.left=ev.pageX+'px';
div.style.top=ev.pageY+'px';
         //解决滚动屏幕后心形不在点击位置问题
          div.style.position='absolute';
document.body.append(div);
var i=1;
var t=setInterval(function(){
div.style.top=div.offsetTop-2+'px';
i-=0.01;
div.style.opacity=i;
var scale=1+(1-i);
div.style.transform='scale('+scale+','+scale+') rotate(45deg)';
if(i<0.01 || div.style.top+div.offsetTop>=window.innerHeight)
{
div.remove();
clearInterval(t);
}
},30)
} // 随机色
function randColor()
{
var r=parseInt(Math.random()*256);
var g=parseInt(Math.random()*256);
var b=parseInt(Math.random()*256);
var a=Math.random().toFixed(1)
var color='rgba('+r+','+g+','+b+','+a+')';
return color;
} // 创建烟花
function Fireworks(ev)
{
// 烟花一种颜色-- 90去掉注释
/*var color=randColor();
for(var i=0;i<firewNum;i++)
{
create(ev,color);
}*/
for(var i=0;i<firewNum;i++)
{
create(ev,null);
}
} function create(ev,color)
{// 操作元素使用的 原生 js ,使用jquery 失败 取不到元素
var div=document.createElement("div");
div.className='Fireworks';
div.style.backgroundColor=randColor();
// div.style.backgroundColor=color;
div.style.left=ev.pageX+'px';
div.style.top=ev.pageY+'px';
         //解决滚动屏幕后心形不在点击位置问题
          div.style.position='absolute';
document.body.append(div);
var i=1;
// 正负 -5右 +5左
var speedX =(parseInt(Math.random()*2) == 0 ? 1 : -1)*parseInt(Math.random()*5 + 1);
// 向上 -0--17
var speedY=-parseInt(Math.random()*18); var time=setInterval(function()
{
++i;
var left=div.offsetLeft+speedX;
var top=div.offsetTop+speedY+i;
// 加 i top 越来越大, 烟花下落,否则烟花向上飘 每次获取得div.offsetTop越来越大 速度越来越慢
div.style.left=left+'px';
div.style.top=top+'px';
            //解决滚动屏幕后点击不出现烟花问题 div.offsetTop+div.offsetHeight>window.innerHeight 改为 div.offsetTop+div.offsetHeight-document.documentElement.scrollTop>window.innerHeight
if(div.offsetLeft+div.offsetWidth>window.innerWidth || div.offsetLeft<2 || div.offsetTop+div.offsetHeight>window.innerHeight || div.offsetTop<2 )
{
//如果超出屏幕 移除div 关闭定时器
div.remove();
clearInterval(time);
} },40)
}
});

另一种封装的点击页面出现心形

 //-------------------------------封装的 点击心 形 自动执行
!function(e, t,evt) {
// 元素属性赋值
function r() {
for (var e = 0; e < s.length; e++)
{
if(s[e].alpha <= 0)
{
t.body.removeChild(s[e].el), s.splice(e, 1);
}else{
s[e].y--,
s[e].scale += .004,
s[e].alpha -= .013,
s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999";
}
}
requestAnimationFrame(r);
return;
}
// 如果存在点击事件 执行动画
function n() {
var t = "function" == typeof e[evt] && e[evt];
e[evt] = function(e) {
t && t(), o(e);
}
}
// 创建元素并且定义初始属性数组
function o(e) {
var a = t.createElement("div");
a.className = "heart", s.push({
el: a,
x: e.clientX - 5,
y: e.clientY - 5,
scale: 1,
alpha: 1,
color: c()
}), t.body.appendChild(a)
}
// 定义样式文件并添加
function i(e) {
var a = t.createElement("style");
a.type = "text/css";
try {
a.appendChild(t.createTextNode(e))
} catch (t) {
a.styleSheet.cssText = e
}
t.getElementsByTagName("head")[0].appendChild(a)
}
// 返回随机色
function c() {
return "rgb(" + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + ")"
}
// 执行动画兼容处理
var s = [];
e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame ||
function(e) {
setTimeout(e, 1e3 / 60)
}, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
}(window, document,'onclick');

js 点击页面出现烟花 心形的更多相关文章

  1. js点击页面其他地方如何隐藏div元素菜单

    web页面常用的一个需求,写下拉菜单是我们往往不是用select_option,而是自定义一个元素列出选项来满足需求,当我们点击按钮出现菜单, 点击按钮或菜单以外页面空白地方隐藏该菜单,这里提供一种简 ...

  2. selenium—JS点击方法

    package com.allin.pc;import java.util.NoSuchElementException;import org.openqa.selenium.By;import or ...

  3. js点击button按钮跳转到页面代码

    点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...

  4. js点击button按钮跳转到另一个新页面

    点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...

  5. 项目遇到的小问题(关于vue-cli中js点击事件不起作用和iconfont图片下载页面css样式乱的解答)

     第一个:关于vue-cli中js点击事件不起作用 在vue的methods方法queryBtnFun()中拼接html和click操作事件的时候,发现点击事件一起未起作用: 后来发现是DOM执行顺序 ...

  6. html js点击按钮滚动跳转定位到页面指定位置(DIV)的方法代码

    一:通过html锚点实现滚动定位到页面指定位置(DIV):    如果我们要点击实现跳转的地方是一个html锚点,也就是点击一个A标签超链接实现跳转,可以把A标签的href属性直接指向跳转指定位置的d ...

  7. PHP——0128练习相关2——js点击button按钮跳转到另一个新页面

    js点击button按钮跳转到另一个新页面 投稿:whsnow 字体:[增加 减小] 类型:转载 时间:2014-10-10我要评论 点击按钮怎么跳转到另外一个页面呢?点击图片要跳转到新的页面时,怎么 ...

  8. Python和Js打印心形

    看到一行Python写的代码,会用LovePython输出心形: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y* ...

  9. JS如何实现点击页面内任意的链接均加参数跳转?

    1.JS跳转页面参考代码 第一种: <script language="javascript" type="text/javascript"> wi ...

随机推荐

  1. Delphi BLE 控件

    TBluetoothLEDevice LDevice.Address;//"00:11:22:DD:EE:FF". LDevice.DeviceName//Mi LDevice.I ...

  2. sql 一些偶尔会用到的写法和函数 不定时更新

    小数转整数: --round() 遵循四舍五入把原值转化为指定小数位数,如: ) -- =1 ) -- =2 --floor() 向下舍入为指定小数位数 如: SELECT floor(1.45) - ...

  3. 【整理】使用AIDL跨进程传递复杂对象的实践例子

    首先定义对象类,并实现Parcelable接口,实现接口内的几个方法,看代码,Person.java package com.example.u3.aidltest; import android.o ...

  4. elasticsearch2.x优化小结(单节点)

    最近es一直卡顿,甚至宕机,用bigdesk看了,才晓得,es一直用的默认配置(可以看出我有多懒,先前数据量小,es足以应付,现在数据量上去后就不行了). 这里总结三方面: 1.提升jvm内存 vi ...

  5. Swing的MVC结构

    --------------siwuxie095                             工程名:TestMVC 包名:com.siwuxie095.mvc 类名:Test.java ...

  6. go语言的源码文件的分类及含义

    Go源码文件:名称以.go为后缀,内容以Go语言代码组织的文件 多个Go源码文件是需要用代码包组织起来的 源码文件分为三类:命令源码文件.库源码文件(go语言程序) 测试源码文件(辅助源码文件) 命令 ...

  7. WarTransportation TopCoder - 8404

    传送门 分析 我们高兴的发现数据范围特别小,所以我们可以随便搞.因为一共只砍掉一条路,所以我们先算出对于任意一个点如果将它的出边割掉一条则它到达终点的最坏情况的最短距离是多少,然后我们从终点向起点反着 ...

  8. 初识Servlet(JSP)

    Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层.  Servle ...

  9. 《Maven实战》笔记-2-坐标和依赖

    一.依赖范围 Maven在编译项目主代码的时候,需要使用一套classpath——编译classpath: 在编译和执行测试的时候,使用另一套classpath——测试classpath: 实际运行M ...

  10. Mac下的UI自动化测试 (一)

    在我看来,实现UI自动化测试的过程一向都是令人快乐的事情,而维护它们就是跟噩梦一样了,尤其是对每次CI产生的build进行BVT测试,由于开发不会告诉你任何UI的变化,那么你拿到的测试结果就势必会一片 ...