JS实用的动画弹出层效果实例


Posted in Javascript onMay 05, 2015

本文实例讲述了JS实用的动画弹出层效果的方法。分享给大家供大家参考。具体实现方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动画弹出层</title>
<style>
.list{
position:relative;;
background:#eee;
border:1px #ccc solid;
margin:10px;
height:30px;
width:100px;
cursor :pointer ;
}
.listShow{
position:relative;
background:#eff;
border:1px #ddd solid;
margin:10px;
height:30px;
width:100px;
cursor :pointer ;
}
.comment{
position:absolute;
left:0;
display:none;
position:absolute;
border:1px #ccc solid;
background:#fee;
width:200px;
height:200px;
overflow:hidden;
z-index:100;
}
</style>
</head>
<body>
<div class="" id="show">
0
</div>
<div class="list" id="list1">1
<div class="comment" id="comment1">内容显示111<br/>
</div>
<div class="list" id="list2">2
<div class="comment" id="comment2">内容显示222</div>
</div>
<div class="list" id="list3">3
<div class="comment" id="comment3">内容显示333</div>
</div>
</body>
</html>
<script>
var zindex=0;
function $id(id){
return document.getElementById(id);
}
var Bind = function(object,fun){
var args = Array.prototype.slice.call(arguments).slice(2);
return function(){
return fun.apply(object,args);
}
}
function addEventHandler(oTarget, sEventType, fnHandler){ 
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType, fnHandler, false);
}
else if(oTarget.attachEvent){
oTarget.attachEvent('on' + sEventType, fnHandler);
}
else{oTarget['on' + sEventType] = fnHandler;}
}
var Shower=function(){
this.list=null;
this.comment=null;
this.moveLeft=80; 
this.moveTop=20;
this.height=150;
this.width=250;
this.time=800;
this.init=function(lisObj,comObj){
this.list=lisObj;
this.comment=comObj;
var _this=this;
this._fnMove=Bind(this,this.move);
(function(){
var obj=_this;
addEventHandler(obj.list,"click",obj._fnMove);
})();
};
this.move=function(){
var _this=this;
var w=0; 
var h=0; 
var height=0; //弹出div的高
var width=0; //弹出div的宽
var t=0;
var startTime = new Date().getTime();//开始执行的时间
if(!_this.comment.style.display||_this.comment.style.display=="none"){
_this.comment.style.display="block";
_this.comment.style.height=0+"px";
_this.comment.style.width=0+"px";
_this.list.style.zIndex=++zindex;
_this.list.className="listShow";
var comment=_this.comment.innerHTML; 
_this.comment.innerHTML=""; //去掉显示内容
var timer=setInterval(function(){
var newTime = new Date().getTime();
var timestamp = newTime - startTime;
_this.comment.style.left=Math.ceil(w)+"px";
_this.comment.style.top=Math.ceil(h)+"px";
_this.comment.style.height=height+"px";
_this.comment.style.width=width+"px";
t++;
var change=(Math.pow((timestamp/_this.time-1), 3) +1);
//根据运行时间得到基础变化量
w=_this.moveLeft*change;
h=_this.moveTop*change;
height=_this.height*change;
width=_this.width*change;
$id("show").innerHTML=w;
if(w>_this.moveLeft){
clearInterval(timer);
_this.comment.style.left=_this.moveLeft+"px";
_this.comment.style.top=_this.moveTop+"px"; _this.comment.style.height=_this.height+"px";
_this.comment.style.width=_this.width+"px";
_this.comment.innerHTML=comment; //回复显示内容
}
},1,_this.comment);
}else{
_this.hidden();
}
}
this.hidden=function(){
var _this=this;
var flag=1;
var hiddenTimer=setInterval(function(){
if(flag==1){
_this.comment.style.height=parseInt(_this.comment.style.height)-10+"px";
}else{
_this.comment.style.width=parseInt(_this.comment.style.width)-15+"px";
_this.comment.style.left=parseInt(_this.comment.style.left)+5+"px";
}
if(flag==1 && parseInt(_this.comment.style.height)<10){
flag=-flag;
}
if(parseInt(_this.comment.style.width)<20){
clearInterval(hiddenTimer);
_this.comment.style.left="0px";
_this.comment.style.top="0px";
_this.comment.style.height="0px";
_this.comment.style.width="0px";
_this.comment.style.display="none";
if(_this.list.style.zIndex==zindex){
zindex--;
};
_this.list.style.zIndex=0;
_this.list.className="list";
}
},1)
}
}
window.onload=function(){
//建立各个菜单对象
var shower1=new Shower();
shower1.init($id("list1"),$id("comment1"));
var shower2=new Shower();
shower2.init($id("list2"),$id("comment2"));
var shower3=new Shower();
shower3.init($id("list3"),$id("comment3"));
}
</script>

效果如下图所示:

JS实用的动画弹出层效果实例

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
常见JS效果之图片减速度滚动实现代码
Dec 08 Javascript
js如何取消事件冒泡
Sep 23 Javascript
倾力总结40条常见的移动端Web页面问题解决方案
May 24 Javascript
jquery+ajax+text文本框实现智能提示完整实例
Jul 09 Javascript
js控制按钮,防止频繁点击响应的实例
Feb 15 Javascript
基于DOM节点删除之empty和remove的区别(详解)
Sep 11 Javascript
JS写谷歌浏览器chrome的外挂实例
Jan 11 Javascript
angular4 共享服务在多个组件中数据通信的示例
Mar 30 Javascript
详解vue文件中使用echarts.js的两种方式
Oct 18 Javascript
D3.js(v3)+react 实现带坐标与比例尺的柱形图 (V3版本)
May 09 Javascript
对TypeScript库进行单元测试的方法
Jul 18 Javascript
JavaScript:ES2019 的新特性(译)
Aug 08 Javascript
js日期范围初始化得到前一个月日期的方法
May 05 #Javascript
javascript实现捕捉键盘上按下的键
May 05 #Javascript
js中this用法实例详解
May 05 #Javascript
javascript中返回顶部按钮的实现
May 05 #Javascript
JS简单实现动画弹出层效果
May 05 #Javascript
教你使用javascript简单写一个页面模板引擎
May 05 #Javascript
关于延迟加载JavaScript
May 05 #Javascript
You might like
ThinkPHP3.1的Widget新用法
2014/06/19 PHP
php empty 函数判断结果为空但实际值却为非空的原因解析
2018/05/28 PHP
基于jquery的兼容各种浏览器的iframe自适应高度的脚本
2010/08/13 Javascript
潜说js对象和数组
2011/05/25 Javascript
基于JQuery实现的类似购物商城的购物车
2011/12/06 Javascript
js 调用本地exe的例子(支持IE内核的浏览器)
2012/12/26 Javascript
jquery foreach使用示例
2013/09/12 Javascript
JS简单实现文件上传实例代码(无需插件)
2013/11/15 Javascript
JQuery记住用户名和密码的具体实现
2014/04/04 Javascript
js对象的复制继承实例
2015/01/10 Javascript
JavaScript实现倒计时代码段Item1(非常实用)
2015/11/03 Javascript
简单实现Bootstrap标签页
2020/08/09 Javascript
微信小程序 引入es6 promise
2017/04/12 Javascript
js与jquery获取input输入框中的值实例讲解
2020/02/27 jQuery
在vue中配置不同的代理同时访问不同的后台操作
2020/09/11 Javascript
Python对小数进行除法运算的正确方法示例
2014/08/25 Python
Python列表append和+的区别浅析
2015/02/02 Python
简介Python设计模式中的代理模式与模板方法模式编程
2016/02/02 Python
单利模式及python实现方式详解
2018/03/20 Python
python+opencv+caffe+摄像头做目标检测的实例代码
2018/08/03 Python
Python神奇的内置函数locals的实例讲解
2019/02/22 Python
Python3列表内置方法大全及示例代码小结
2019/05/10 Python
500行Python代码打造刷脸考勤系统
2019/06/03 Python
使用CSS3制作饼状旋转载入效果的实例
2015/06/23 HTML / CSS
NET程序员上机面试题
2015/05/23 面试题
公司新员工的演讲稿注意事项
2014/01/01 职场文书
护理专业大学生自我推荐信
2014/01/25 职场文书
超市国庆节促销方案
2014/02/20 职场文书
新春文艺演出主持词
2014/03/27 职场文书
房屋买卖协议书
2014/04/10 职场文书
电气工程及其自动化专业毕业生自荐信
2014/06/21 职场文书
2014光棍节大学生联谊活动方案
2014/10/10 职场文书
2015年元旦促销方案书
2014/12/09 职场文书
物业客服专员岗位职责
2015/04/07 职场文书
安全生产警示教育活动总结
2015/05/09 职场文书
分析mysql中一条SQL查询语句是如何执行的
2021/06/21 MySQL