新鲜出炉的js tips提示效果

 更新时间:2011年04月03日 01:34:39   作者:  
提示不难做,那个三角号样式很难调兼容,死了好多脑细胞预览主窗口提示错位,保存本地测比较好。
兼容性已经测过:IE6\IE7\IE8\FF3\CHROME10
复制代码 代码如下:

<!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">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body>
<br><br><br><br><br><br><br><br>
<input id='test'>

<br><br><br><br><br><br><br><br>
<a href='#' id='test2' onmouseover="tips.show('普通链接或按钮提示-灰色-鼠标离开消失-300像素', 'test2', null, '#000000', 300)" onmouseout="tips.hidden('test2')">普通链接或按钮提示</a>

<script>
//提示消息类
var tips = {
temp : {},
/***
* 弹出提示
*
* @param string msg 提示文字内容
* @param string id 要弹出提示的目标对象的id,如果id错误/null/false/0则主窗口弹出
* @param int time 定时消失时间毫秒数,如果为null/0/false则不定时
* @param string color 提示内容的背景颜色格式为#000000
* @param int width 提示窗宽度,默认300
*/
show : function(msg, id, time, color, width)
{
var target = this._get(id);
if(!target) { id = 'window'; }

//如果弹出过则移除重新弹出
if(this._get(id+'_tips')) { this.remove(id); }

//设置默认值
msg = msg || 'error';
color = color || '#ea0000';
width = width || 300;
time = time ? parseInt(time) : false;

if(id=='window') {
var y = document.body.clientHeight/2+document.body.scrollTop;
var x = (document.body.clientWidth-width)/2;
var textAlign = 'center', fontSize = '15',fontWeight = 'bold';
} else {
//获取对象坐标信息
for(var y=0,x=0; target!=null; y+=target.offsetTop, x+=target.offsetLeft, target=target.offsetParent);
var textAlign = 'left', fontSize = '12',fontWeight = 'normal';
}

//弹出提示
var tipsDiv = this._create({display:'block',position:'absolute',zIndex:'1001',width:(width-2)+'px',left:(x+1)+'px',padding:'5px',color:'#ffffff',fontSize:fontSize+'px',backgroundColor:color,textAlign:textAlign,fontWeight:fontWeight,filter:'Alpha(Opacity=50)',opacity:'0.7'}, {id:id+'_text', innerHTML:msg, onclick:function(){tips.hidden(id);}});
document.body.appendChild(tipsDiv);
tipsDiv.style.top = (y-tipsDiv.offsetHeight-12)+'px';
document.body.appendChild(this._create({display:'block',position:'absolute',zIndex:'1000',width:(width+10)+'px',height:(tipsDiv.offsetHeight-2)+'px',left:x+'px',top:(y-tipsDiv.offsetHeight-11)+'px',backgroundColor:color,filter:'Alpha(Opacity=50)',opacity:'0.7'}, {id:id+'_bg'}));
if(id!='window') {
var arrow = this._create({display:'block',position:'absolute',overflow:'hidden',zIndex:'999',width:'20px',height:'10px',left:(x+20)+'px',top:(y-13)+'px'}, {id:id+'_arrow'});
arrow.appendChild(this._create({display:'block',overflow:'hidden',width:'0px',height:'10px',borderTop:'10px solid '+color,borderRight:'10px solid #fff', borderLeft:'10px solid #fff',filter:'Alpha(Opacity=70)',opacity:'0.8'}));
document.body.appendChild(arrow);
}

//标记已经弹出
this.temp[id] = id;

//如果定时关闭
if(time) { setTimeout(function(){tips.hidden(id);}, time) }

return id;
},
/***
* 隐藏提示
*
* @param string id 要隐藏提示的id,如果要隐藏主窗口提示id为window,如果要隐藏所有提示id为空即可
*/
hidden : function(id)
{
if(!id) { for(var i in this.temp) { this.hidden(i); } return; }
var t = this._get(id+'_text'), d = this._get(id+'_bg'), a = this._get(id+'_arrow');
if(t) { t.parentNode.removeChild(t); }
if(d) { d.parentNode.removeChild(d); }
if(a) { a.parentNode.removeChild(a); }
},
_create : function(set, attr)
{
var obj = document.createElement('div');
for(var i in set) { obj.style[i] = set[i]; }
for(var i in attr) { obj[i] = attr[i]; }
return obj;
},
_get : function(id)
{
return document.getElementById(id);
}
};


window.onload = function(){
tips.show('主窗口提示-绿色-不定时-300像素', null, null, '#009900', 300);
tips.show('表单报错提示-红色-3000毫秒消失-250像素', 'test', 3000, '#ea0000', 250);
}
document.onclick = function(){
tips.hidden();
}
</script>

相关文章

  • UEditor 自定义图片视频尺寸校验功能的实现代码

    UEditor 自定义图片视频尺寸校验功能的实现代码

    UEditor支持单图、多图以及视频上传,编辑器配置项支持文件格式、文件大小校验,对于文件宽高尺寸校验暂不支持。本文给大家介绍UEditor 自定义图片视频尺寸校验功能的实现代码,感兴趣的朋友一起看看吧
    2020-10-10
  • 使用Layui搭建后台管理界面的操作方法

    使用Layui搭建后台管理界面的操作方法

    今天小编就为大家分享一篇使用Layui搭建后台管理界面的操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • javascript 特性检测并非浏览器检测

    javascript 特性检测并非浏览器检测

    NCZ在他的同名博客《Feature detection is not browser detection》中,讲述了一直以来前端开发中的一个热门技术——检测用户的浏览器平台,并详细地叙说历史发展以及各种办法的优缺点。
    2010-01-01
  • uniapp中实现App自动检测版本升级的示例代码

    uniapp中实现App自动检测版本升级的示例代码

    本文主要介绍了uniapp中实现App自动检测版本升级的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • 基于uni-app开发刻度尺组件的实现示例

    基于uni-app开发刻度尺组件的实现示例

    本文主要介绍了基于uni-app开发刻度尺组件的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • uni-file-picker文件选择上传功能实现

    uni-file-picker文件选择上传功能实现

    这篇文章主要介绍了uni-file-picker文件选择上传,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • javascript获取元素偏移量的方法有哪些

    javascript获取元素偏移量的方法有哪些

    javascript中可以通过四个属性获得元素的偏移量,offsetHeight、offsetWidth、offsetLeft、offsetTop,下面为大家解释下各属性的含义
    2014-06-06
  • js实现的切换面板实例代码

    js实现的切换面板实例代码

    切换面板具备功能:鼠标划过不同的题目会有不同的图片和解说,可以连接到不同的网站,实例代码如下,感兴趣的朋友可以参考下哈
    2013-06-06
  • Bootstrap php制作动态分页标签

    Bootstrap php制作动态分页标签

    这篇文章主要为大家详细介绍了Bootstrap php制作动态分页标签的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • bootstrap和jQuery.Gantt的css冲突 如何解决

    bootstrap和jQuery.Gantt的css冲突 如何解决

    这篇文章主要介绍了bootstrap和jQuery.Gantt的css冲突,本文给出了解决冲突的解决方法,感兴趣的小伙伴们可以参考一下
    2016-05-05

最新评论