js 点击页面出现烟花 心形
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 点击页面出现烟花 心形的更多相关文章
- js点击页面其他地方如何隐藏div元素菜单
web页面常用的一个需求,写下拉菜单是我们往往不是用select_option,而是自定义一个元素列出选项来满足需求,当我们点击按钮出现菜单, 点击按钮或菜单以外页面空白地方隐藏该菜单,这里提供一种简 ...
- selenium—JS点击方法
package com.allin.pc;import java.util.NoSuchElementException;import org.openqa.selenium.By;import or ...
- js点击button按钮跳转到页面代码
点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...
- js点击button按钮跳转到另一个新页面
点击按钮怎么跳转到另外一个页面呢?我们在网站制作中可能是需要的,因为有时我们需要做这样的效果,尤其是将按钮做成一个图片,而点击图片要跳转到新的页面时,怎么做到呢? 这样的效果可以:onclick=&q ...
- 项目遇到的小问题(关于vue-cli中js点击事件不起作用和iconfont图片下载页面css样式乱的解答)
第一个:关于vue-cli中js点击事件不起作用 在vue的methods方法queryBtnFun()中拼接html和click操作事件的时候,发现点击事件一起未起作用: 后来发现是DOM执行顺序 ...
- html js点击按钮滚动跳转定位到页面指定位置(DIV)的方法代码
一:通过html锚点实现滚动定位到页面指定位置(DIV): 如果我们要点击实现跳转的地方是一个html锚点,也就是点击一个A标签超链接实现跳转,可以把A标签的href属性直接指向跳转指定位置的d ...
- PHP——0128练习相关2——js点击button按钮跳转到另一个新页面
js点击button按钮跳转到另一个新页面 投稿:whsnow 字体:[增加 减小] 类型:转载 时间:2014-10-10我要评论 点击按钮怎么跳转到另外一个页面呢?点击图片要跳转到新的页面时,怎么 ...
- Python和Js打印心形
看到一行Python写的代码,会用LovePython输出心形: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y* ...
- JS如何实现点击页面内任意的链接均加参数跳转?
1.JS跳转页面参考代码 第一种: <script language="javascript" type="text/javascript"> wi ...
随机推荐
- asp.net的验证码插件及方法、ashx验证码一般处理程序
需要引入一个ashx的一般处理程序! 把这个程序在前台当作一个图片使用就可以! 前台代码: <td> <img title="看不清?" style=" ...
- js-tree坑
今天遇到一个js坑,一个页面,有两棵树,用同一个套参数初始化的,,,,当选择完另一个棵树之后,再操作另一颗树,不选择树节点,就会有错误出现,,,
- 202. Happy Number 平方循环数
[抄题]: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- java中怎么把String转化为字符数组呢?
我想让用户输入一组字符串,然后将这个字符串的每一个赋给一个char.即,abcde char ch[]=new char[5]; ch[0]=a;ch[1]=b;..... 解决方案如下图所示: St ...
- Luogu 2149 [SDOI2009]Elaxia的路线
感觉这题可以模板化. 听说spfa死了,所以要练堆优化dijkstra. 首先对$x_{1},y_{1},x_{2},y_{2}$各跑一遍最短路,然后扫一遍所有边看看是不是同时在两个点的最短路里面,如 ...
- Python程序设计3——字典
1 字典 字典是Python唯一内建的映射类型.字典是键值对的集合. 1.1 字典的使用 某些情况下字典更加好用,比如一个电话列表.注意:电话号码只能用字符串数字表示,否则会出问题.因为电话号码一旦以 ...
- Umbraco Form 中需要为一个Form的某个field设置特别的CSS样式
在项目开发中,我有一个需求,需要建立一个页面,这个页面上有一个form, 这个form上有一个checkbox, 就是普通的接受terms & conditions, 类似下图 这个项目中的U ...
- 《Maven实战》笔记-4-生命周期和插件
除了坐标.依赖以及仓库外,Maven另外两个核心概念是生命周期和插件. 一.生命周期 Maven的生命周期是抽象的,其本身不做任务实际的工作,实际的任务(如编译源代码)都交由插件来完成. 三套生命周期 ...
- HTML 4.0 触发事件
HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 在现代浏览器中都内置有大量的事件处理器.这些处理器会监视特 ...
- Android将程序崩溃信息保存本地文件
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...