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中单选框和复选框获取值的方式
Nov 06 Javascript
JQuery与JSon实现的无刷新分页代码
Sep 13 Javascript
js 编码转换 gb2312 和 utf8 互转的2种方法
Aug 07 Javascript
JS中使用apply方法通过不同数量的参数调用函数的方法
May 31 Javascript
artDialog+plupload实现多文件上传
Jul 19 Javascript
javascript中replace使用方法总结
Mar 01 Javascript
深入浅析vue组件间事件传递
Dec 29 Javascript
基于D3.js实现时钟效果
Jul 17 Javascript
vue中使用cookies和crypto-js实现记住密码和加密的方法
Oct 18 Javascript
微信小程序文章详情页跳转案例详解
Jul 09 Javascript
详解vuex数据传输的两种方式及this.$store undefined的解决办法
Aug 26 Javascript
原生JS封装vue Tab切换效果
Apr 28 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
PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
2010/04/28 PHP
PHP下escape解码函数的实现方法
2010/08/08 PHP
php实现的zip文件内容比较类
2014/09/24 PHP
PHP实现随机生成水印图片功能
2017/03/22 PHP
PHP与Perl之间知识点区别整理
2019/03/19 PHP
php正则表达式使用方法整理集合
2020/01/31 PHP
JS效率个人经验谈(8-15更新),加入range技巧
2007/01/09 Javascript
JS代码同步文本框内容的实例方法
2013/07/12 Javascript
javascript 获取模态窗口的滚动位置代码
2013/08/06 Javascript
js 剪切板应用clipboardData详细解析
2013/12/17 Javascript
基于编写jQuery的无缝滚动插件
2014/08/02 Javascript
Jquery1.9.1源码分析系列(十五)动画处理之外篇
2015/12/04 Javascript
JavaScript中值类型和引用类型的区别
2017/02/23 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
jQuery EasyUI 为Combo,Combobox添加清除值功能的实例
2017/04/13 jQuery
不到200行 JavaScript 代码实现富文本编辑器的方法
2018/01/03 Javascript
Angular实现搜索框及价格上下限功能
2018/01/19 Javascript
[01:05:41]EG vs Optic Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
Python 获得13位unix时间戳的方法
2017/10/20 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2018/02/26 Python
python实现的Iou与Giou代码
2020/01/18 Python
浅谈keras的深度模型训练过程及结果记录方式
2020/01/24 Python
基于Python爬虫采集天气网实时信息
2020/06/05 Python
Python计算矩阵的和积的实例详解
2020/09/10 Python
利用CSS3的定位页面元素
2009/08/29 HTML / CSS
英国绿色商店:Natural Collection
2019/05/03 全球购物
高中军训感言600字
2014/03/11 职场文书
优秀教师演讲稿
2014/05/06 职场文书
白岩松演讲
2014/05/21 职场文书
运动会横幅标语
2014/06/17 职场文书
护士2015年终工作总结
2015/04/29 职场文书
沂蒙六姐妹观后感
2015/06/08 职场文书
诚信教育主题班会
2015/08/13 职场文书
乡镇团代会开幕词
2016/03/04 职场文书
简单了解 MySQL 中相关的锁
2021/05/25 MySQL
MySQL 用 limit 为什么会影响性能
2021/09/15 MySQL