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 ...
随机推荐
- JDBC批处理数据
JDBC3.0 的增强支持BLOB,CLOB,ARRAY,REF数据类型.的ResultSet对象UPDATEBLOB(),updateCLOB(),updateArray()和updateRef( ...
- 【转载】你知道 Linux 内核是如何构建的吗?
内核的根 Makefile 负责构建两个主要的文件:vmlinux (内核镜像可执行文件)和模块文件.内核的 Makefile 从定义如下变量开始: VERSION = PATCHLEVEL = SU ...
- MSSQL grant权限
--创建登录名 create login test_user with password='123456a.'; --创建用户 create user test_user for login test ...
- 205. Isomorphic Strings两个数组变形记,是否符合规则
[抄题]: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 存储前set方法相互关联 只关联了一方 分别set
17:51:45,580 ERROR SqlExceptionHelper:129 - Column 'lkm_cust_id' cannot be nullorg.hibernate.excepti ...
- Spring 第一天课程
一. 面试题部分 1. 什么是IOC?什么是DI?他们有什么区别? 答案: IOC,即控制反转.是指将原来程序中自己创建实现类对象的控制权反转到IOC容器中. IOC的别名:依赖注入(DI),DI 是 ...
- OpenCV Mat数据类型指针ptr的使用
OpenCV Mat数据类型指针ptr的使用 cv::Mat image = cv::Mat(400, 600, CV_8UC1); //宽400,长600 uchar * data00 = imag ...
- php学习笔记-逻辑运算符
$a and $b 只有当$a和$b都是true才返回true,否则false. $a or $b 只要$a或者$b至少有一个是true则返回true.意思是或者$a是true,或者是$b是true, ...
- Python沙盒环境配置
一.简介 本文介绍配置python沙盒环境的方法步骤. 二.安装步骤 1.安装pyenv http://www.cnblogs.com/274914765qq/p/4948530.html 2.安装v ...
- 关于Java中hashCode方法的实现源码
首先来看一下String中hashCode方法的实现源码. public int hashCode() { int h = hash; if (h == 0 && value.leng ...