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 相关文章推荐
CLASS_CONFUSION JS混淆 全源码
Dec 12 Javascript
关于JQuery($.load)事件的用法和分析
Apr 09 Javascript
javascript怎么禁用浏览器后退按钮
Mar 27 Javascript
javascript通过元素id和name直接取得元素的方法
Apr 28 Javascript
js过滤HTML标签完整实例
Nov 26 Javascript
window.close(); 关闭浏览器窗口js代码的总结介绍
Jul 14 Javascript
js 实现数值的千分位及保存小数方法(推荐)
Aug 01 Javascript
jQuery ajax请求struts action实现异步刷新
Apr 19 jQuery
JavaScript实现数组全排列、去重及求最大值算法示例
Jul 30 Javascript
React 使用recharts实现散点地图的示例代码
Dec 07 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
Apr 14 Javascript
jQuery实现简单评论区功能
Oct 26 jQuery
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 socket实现的聊天室代码分享
2014/08/16 PHP
php筛选不存在的图片资源
2015/04/28 PHP
php curl操作API接口类完整示例
2019/05/21 PHP
解决windows上php xdebug 无法调试的问题
2020/02/19 PHP
XP折叠菜单&amp;仿QQ2006菜单
2006/12/16 Javascript
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
jQuery.autocomplete 支持中文输入(firefox)修正方法
2011/03/10 Javascript
Three.js源码阅读笔记(基础的核心Core对象)
2012/12/27 Javascript
JS中把字符转成ASCII值的函数示例代码
2013/11/21 Javascript
jQuery中的val()示例应用
2014/02/26 Javascript
JavaScript中Object.prototype.toString方法的原理
2016/02/24 Javascript
JavaScript之Map和Set_动力节点Java学院整理
2017/06/29 Javascript
详解vuex 渐进式教程实例代码
2018/11/27 Javascript
angularjs http与后台交互的实现示例
2018/12/21 Javascript
vue中uni-app 实现小程序登录注册功能
2019/10/12 Javascript
js实现GIF动图分解成多帧图片上传
2019/10/24 Javascript
python避免死锁方法实例分析
2015/06/04 Python
视觉直观感受若干常用排序算法
2017/04/13 Python
Python用Pillow(PIL)进行简单的图像操作方法
2017/07/07 Python
pandas.read_csv参数详解(小结)
2019/06/21 Python
Python数据可视化:饼状图的实例讲解
2019/12/07 Python
简单了解Python读取大文件代码实例
2019/12/18 Python
Python实现疫情通定时自动填写功能(附代码)
2020/05/27 Python
基于Python实现2种反转链表方法代码实例
2020/07/06 Python
Python 日期与时间转换的方法
2020/08/01 Python
英国文胸专家:AmpleBosom.com
2018/02/06 全球购物
马来西亚最热门的在线时尚商店:FashionValet
2018/11/11 全球购物
几个Linux面试题笔试题
2012/12/01 面试题
计算机专业毕业生自荐信范文
2014/03/06 职场文书
跟单业务员岗位职责
2014/03/08 职场文书
六查六看个人剖析材料
2014/10/14 职场文书
成都人事代理协议书
2014/10/25 职场文书
骨干教师事迹材料
2014/12/17 职场文书
单位委托函范文
2015/01/29 职场文书
公文写作指导之倡议书!
2019/07/03 职场文书
导游词之太行山青龙峡
2020/01/14 职场文书