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 相关文章推荐
做网页的一些技巧
Feb 01 Javascript
Display SQL Server Version Information
Jun 21 Javascript
javascript 全角转换实现代码
Jul 17 Javascript
表单序列化与jq中的serialize使用示例
Feb 21 Javascript
javascript设计模式之工厂模式示例讲解
Mar 04 Javascript
兼容主流浏览器的JS复制内容到剪贴板
Dec 12 Javascript
jQuery绑定事件on()与弹窗的简要概述
Apr 27 Javascript
简单实现js无缝滚动效果
Feb 05 Javascript
JS实现无缝循环marquee滚动效果
May 22 Javascript
vue .js绑定checkbox并获取、改变选中状态的实例
Aug 24 Javascript
js尾调用优化的实现
May 23 Javascript
vue 中this.$set 动态绑定数据的案例讲解
Jan 29 Vue.js
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
15个小时----从修改程序到自己些程序
2006/10/09 PHP
解析php中var_dump,var_export,print_r三个函数的区别
2013/06/21 PHP
使用PHP备份MySQL和网站发送到邮箱实例代码
2013/11/28 PHP
php+mysql查询优化简单实例
2015/01/13 PHP
PHP使用GD库输出汉字的方法【测试可用】
2016/11/10 PHP
php使用正则表达式获取字符串中的URL
2016/12/29 PHP
jquery.ui.draggable中文文档
2009/11/24 Javascript
jQuery之过滤元素操作小结
2013/11/30 Javascript
浅析jquery的作用与优势
2013/12/02 Javascript
下拉列表select 由左边框移动到右边示例
2013/12/04 Javascript
javascript简单实现命名空间效果
2014/03/06 Javascript
jQuery中cookie插件用法实例分析
2015/12/04 Javascript
js实现滚动条滚动到某个位置便自动定位某个tr
2021/01/20 Javascript
jQuery多文件异步上传带进度条实例代码
2016/08/16 Javascript
javascript基于原型链的继承及call和apply函数用法分析
2016/12/15 Javascript
jquery中用函数来设置css样式
2016/12/22 Javascript
Vue自定义指令拖拽功能示例
2017/02/17 Javascript
bootstrap timepicker在angular中取值并转化为时间戳
2017/06/13 Javascript
JQuery 获取多个select标签option的text内容(实例)
2017/09/07 jQuery
vue学习笔记之v-if和v-show的区别
2017/09/20 Javascript
微信小程序url与token设置详解
2017/09/26 Javascript
vue通信方式EventBus的实现代码详解
2019/06/10 Javascript
利用python求相邻数的方法示例
2017/08/18 Python
python调用staf自动化框架的方法
2018/12/26 Python
Python 调用PIL库失败的解决方法
2019/01/08 Python
Python Pandas中根据列的值选取多行数据
2019/07/08 Python
详解python statistics模块及函数用法
2019/10/27 Python
Python装饰器用法与知识点小结
2020/03/09 Python
PyCharm2020.1.1与Python3.7.7的安装教程图文详解
2020/08/07 Python
HTML5 离线应用之打造零请求、无流量网站的解决方法
2013/04/25 HTML / CSS
浅析HTML5 meta viewport参数
2020/10/28 HTML / CSS
英文版区域经理求职信
2013/10/23 职场文书
冰淇淋店创业计划书范文
2013/12/27 职场文书
2014大学生批评与自我批评思想汇报
2014/09/21 职场文书
创业计划书之书店
2019/09/10 职场文书
7个关于Python的经典基础案例
2021/11/07 Python