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中匿名函数的N种写法
Sep 08 Javascript
基于JQuery实现异步刷新的代码(转载)
Mar 29 Javascript
解析使用JS 清空File控件的路径值
Jul 08 Javascript
js 动态修改css文件的方法
Aug 05 Javascript
jQuery中:input选择器用法实例
Jan 03 Javascript
JavaScript基本语法学习教程
Jan 14 Javascript
JS实现重新加载当前页面
Nov 29 Javascript
Django1.7+JQuery+Ajax验证用户注册集成小例子
Apr 08 jQuery
Vue2 模板template的四种写法总结
Feb 23 Javascript
Vue双向绑定实现原理与方法详解
May 07 Javascript
小程序实现背景音乐播放和暂停
Jun 19 Javascript
JS如何监听div的resize事件详解
Dec 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
CI(CodeIgniter)框架配置
2014/06/10 PHP
PHP-Java-Bridge使用笔记
2014/09/22 PHP
浅谈php和.net的区别
2014/09/28 PHP
php判断用户是否手机访问代码
2015/06/08 PHP
php封装的验证码类分享
2017/02/26 PHP
Yii2中添加全局函数的方法分析
2017/05/04 PHP
php基于SQLite实现的分页功能示例
2017/06/21 PHP
关于PHP转换超过2038年日期出错的问题解决
2017/06/28 PHP
[原创]php token使用与验证示例【测试可用】
2017/08/30 PHP
PHP程序员简单的开展服务治理架构操作详解(二)
2020/05/14 PHP
javascript 内存回收机制理解
2011/01/17 Javascript
JavaScript快速检测浏览器对CSS3特性的支持情况
2012/09/26 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
Ubuntu 16.04 64位中搭建Node.js开发环境教程
2016/10/19 Javascript
Bootstrap栅格系统简单实现代码
2017/03/06 Javascript
jQuery插件zTree实现单独选中根节点中第一个节点示例
2017/03/08 Javascript
jQuery封装placeholder效果实现方法,让低版本浏览器支持该效果
2017/07/08 jQuery
Vue+mui实现图片的本地缓存示例代码
2018/05/24 Javascript
Koa 使用小技巧(小结)
2018/10/22 Javascript
vue实现抽屉弹窗效果
2020/11/15 Javascript
python使用PyGame模块播放声音的方法
2015/05/20 Python
浅谈python日志的配置文件路径问题
2018/04/28 Python
Windows下Anaconda2安装NLTK教程
2018/09/19 Python
Python 日期的转换及计算的具体使用详解
2020/01/16 Python
python 输入字符串生成所有有效的IP地址(LeetCode 93号题)
2020/10/15 Python
scrapy处理python爬虫调度详解
2020/11/23 Python
python脚本定时发送邮件
2020/12/22 Python
美发活动策划书
2014/01/14 职场文书
欢送退休感言
2014/02/08 职场文书
学校火灾防控方案
2014/06/09 职场文书
学校政风行风评议工作总结
2014/10/21 职场文书
群众路线教育实践活动整改落实情况汇报
2014/10/28 职场文书
三八妇女节新闻稿
2015/07/17 职场文书
入伍志愿书怎么写?
2019/07/19 职场文书
导游词之淮安明祖陵
2019/11/25 职场文书
Python Socket编程详解
2021/04/25 Python