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 ...
随机推荐
- ssh整合(dao使用hibernateTemplate)
- maven手动安装oracle驱动到仓库
1. 2.打开http://maven.jahia.org/maven2/一步步打开找到 我需要的版本 https://devtools.jahia.com/nexus/content/groups/ ...
- MySQL存储引擎 -- MyISAM 与 InnoDB 实现
一.MyISAM索引实现MyISAM引擎使用B树作为索引结构,叶节点的data域存放的是数据记录的地址. MyISAM主键索引这里设表一共有三列,假设我们以Col1为主键,Col2为辅助索引.则下图是 ...
- PHP自定义函数获取汉字首字母的方法
使用场景:城市列表等根据首字母排序的场景 function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($ ...
- EZOJ #87
传送门 分析 由于我不知道壶里到底有多少水,那么显然我第一次 分别向两个杯子分别到 L/2 +1 和 L/2 才是最优的.(这样既维护了两个人的差值不超1,又正好倒了L的水).那么接下来如果壶里还有水 ...
- vue 之 介绍及简单使用
浏览目录 vue的介绍 vue的使用 vue的介绍 简介 vue官网说:Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底 ...
- 解决.jsp及静态资源文件访问404的问题
我们在做Web项目时,经常将.jsp文件放到webapp\WEB-INF下,这时,我们访问jsp等文件的时候,就会报404. 如果是纯前后端分离的项目,后端只返回数据,不处理页面,也没问题.但,有时我 ...
- 2018年第九届蓝桥杯国赛总结(JavaB组)
懒更,之前的删了补一个国赛总结 记yzm10的第一次国赛(赛点:首都经贸大学) 第一次就拿到了国一,运气不要太好~(同组lz学长豪取国特orz) 从省赛一路水过来,总算有了点成绩.其实最后一题有些遗憾 ...
- easyui textbox 设置只读不可编辑状态
在使用easyul的时候,发现输入框内容及不容易获取与设置,用jQuery的方式大部分失效.依稀记得好像是因为easyul会在原页面的基础上,生成了一些新的独有样式,并且暂时覆盖掉使用了easyul的 ...
- org.apache.commons.lang3包中的isEmpty和isBlank
主要为了区分一下empty和blank的用法,先看源码: isEmpty public static boolean isEmpty(CharSequence cs) { return cs == n ...