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代码压缩器
Oct 12 Javascript
改进UCHOME的记录发布,增强可访问性用户体验
Jan 17 Javascript
JS数组操作(数组增加、删除、翻转、转字符串、取索引、截取(切片)slice、剪接splice、数组合并)
May 20 Javascript
jQuery弹出下拉列表插件(实现kindeditor的@功能)
Aug 16 Javascript
jQuery实现的自定义弹出层效果实例详解
Sep 04 Javascript
JavaScript对象创建模式实例汇总
Oct 03 Javascript
jQuery实现手机版页面翻页效果的简单实例
Oct 05 Javascript
vue中实现图片和文件上传的示例代码
Mar 16 Javascript
原生JS实现自定义下拉单选选择框功能
Oct 12 Javascript
Vue实现开心消消乐游戏算法
Oct 22 Javascript
JS端基于download.js实现图片、视频时直接下载而不是打开预览
May 09 Javascript
Vue组件通信$attrs、$listeners实现原理解析
Sep 03 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读注册表
2006/10/09 PHP
php面向对象全攻略 (五) 封装性
2009/09/30 PHP
php实现的网络相册图片防盗链完美破解方法
2015/07/01 PHP
PHP模板引擎Smarty中的保留变量用法分析
2016/04/11 PHP
PHP实现的自定义数组排序函数与排序类示例
2016/11/18 PHP
javascript 读取xml,写入xml 实现代码
2009/07/10 Javascript
JavaScript计算字符串中每个字符出现次数的小例子
2013/07/02 Javascript
JS图像无缝滚动脚本非常好用
2014/02/10 Javascript
jquery实现更改表格行顺序示例
2014/04/30 Javascript
浅析JavaScript中的变量复制、参数传递和作用域链
2016/01/13 Javascript
深入分析javascript中的错误处理机制
2016/07/17 Javascript
JS简单实现移动端日历功能示例
2016/12/28 Javascript
利用vue.js插入dom节点的方法
2017/03/15 Javascript
vue-resource + json-server模拟数据的方法
2017/11/02 Javascript
VUE2.0+ElementUI2.0表格el-table实现表头扩展el-tooltip
2018/11/30 Javascript
vue远程加载sfc组件思路详解
2019/12/25 Javascript
Python使用matplotlib绘制动画的方法
2015/05/20 Python
python中字符串变二维数组的实例讲解
2018/04/03 Python
python实现栅栏加解密 支持密钥加密
2019/03/20 Python
python学习--使用QQ邮箱发送邮件代码实例
2019/04/16 Python
Django 外键的使用方法详解
2019/07/19 Python
python 利用jinja2模板生成html代码实例
2019/10/10 Python
python模拟实现斗地主发牌
2020/01/07 Python
Python3和PyCharm安装与环境配置【图文教程】
2020/02/14 Python
Python urllib request模块发送请求实现过程解析
2020/12/10 Python
python中if嵌套命令实例讲解
2021/02/25 Python
可打印的优惠券、杂货和优惠券代码:Coupons.com
2018/06/12 全球购物
豪华床上用品 :Jennifer Adams
2019/09/15 全球购物
求职推荐信
2013/10/28 职场文书
业绩考核岗位职责
2014/02/01 职场文书
剪彩仪式主持词
2014/03/19 职场文书
党员公开承诺践诺书
2014/03/25 职场文书
简历自我评价范文
2019/04/24 职场文书
关于JS中的作用域中的问题思考分享
2022/04/06 Javascript
Redis实战高并发之扣减库存项目
2022/04/14 Redis
hive数据仓库新增字段方法
2022/06/25 数据库