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 相关文章推荐
IE7提供XMLHttpRequest对象为兼容
Mar 08 Javascript
映彩衣的js随笔(js图片切换效果)
Jul 31 Javascript
Javascript 闭包引起的IE内存泄露分析
May 23 Javascript
滚动条的监听与内容随着滚动条动态加载的实现
Feb 08 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
Feb 10 Javascript
详解vuex 中的 state 在组件中如何监听
May 23 Javascript
详解Vue双向数据绑定原理解析
Sep 11 Javascript
解决Linux无法正常安装与卸载Node.js的方法
Jan 19 Javascript
vue拖拽排序插件vuedraggable使用方法详解
Aug 21 Javascript
jQuery表单元素过滤选择器用法实例分析
Feb 20 jQuery
vue router总结 $router和$route及router与 router与route区别
Jul 05 Javascript
JavaScript异步操作的几种常见处理方法实例总结
May 11 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
php set_magic_quotes_runtime() 函数过时解决方法
2010/07/08 PHP
PHP递归返回值时出现的问题解决办法
2013/02/19 PHP
PHP可变函数的使用详解
2013/06/14 PHP
简单谈谈php延迟静态绑定
2016/01/26 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
PHPstorm快捷键(分享)
2017/07/17 PHP
php PDO属性设置与操作方法分析
2018/12/27 PHP
php session_decode函数用法讲解
2019/05/26 PHP
基于JavaScript 类的使用详解
2013/05/07 Javascript
模拟多级复选框效果的jquery代码
2013/08/13 Javascript
jQuery自定义事件的简单实现代码
2014/01/27 Javascript
JavaScript DOM事件(笔记)
2015/04/08 Javascript
swtich/if...else的替代语句
2015/08/16 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
JavaScript File API实现文件上传预览
2016/02/02 Javascript
【经典源码收藏】jQuery实用代码片段(筛选,搜索,样式,清除默认值,多选等)
2016/06/07 Javascript
Node.js的Koa框架上手及MySQL操作指南
2016/06/13 Javascript
js面向对象实现canvas制作彩虹球喷枪效果
2016/09/24 Javascript
vue-cli单页应用改成多页应用配置详解
2017/07/14 Javascript
react路由配置方式详解
2017/08/07 Javascript
JavaScript实现的搜索及高亮显示功能示例
2017/08/14 Javascript
nodejs实现的http、https 请求封装操作示例
2020/02/06 NodeJs
[01:05:56]2018DOTA2亚洲邀请赛3月29日 小组赛A组 Newbee VS VG
2018/03/30 DOTA
用Python编写一个简单的Lisp解释器的教程
2015/04/03 Python
推荐10款最受Python开发者欢迎的Python IDE
2018/09/16 Python
Python学习笔记之视频人脸检测识别实例教程
2019/03/06 Python
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
2019/06/04 Python
Python 抓取微信公众号账号信息的方法
2019/06/14 Python
Python中低维数组填充高维数组的实现
2019/12/02 Python
Python使用turtle库绘制小猪佩奇(实例代码)
2020/01/16 Python
Python urllib3软件包的使用说明
2020/11/18 Python
俄罗斯三星品牌商店:Samsungstore
2020/04/05 全球购物
2014国庆节国旗下演讲稿(精选版)
2014/09/26 职场文书
2015年学生会工作总结范文
2015/03/31 职场文书
超强台风观后感
2015/06/09 职场文书
《飘》英文读后感五篇
2019/10/11 职场文书