js 自定义类Android吐司提示框
(function(){
var mouseX = 0;
var mouseY = 0;
//定义一个全局toaslist用来存在新建的吐司
var toastLsit = [];
window.Toast = function(content,duration,positon)
{
return new Toast(content,duration,positon);
}
function Toast(content,duration,positon)
{
//显示的内容
this.content = content || "注意";
this.duration = duration || 500;
this.ToastDom = null;
this.ToastDomOpacity = 100;
this.toastTimer = false;
this.toastTimeOut = false;
this.mouseX = mouseX;
this.mouseY = mouseY;
this.zindex = 999;
this.position = positon || "mouse";
this.initToastDom();
this.bindEvent();
this.hiddenToast();
toastLsit.push(this);
}
//绑定事件,缓慢变透明,然后移除,如果鼠标hover透明度又恢复
Toast.prototype.bindEvent = function(){
var _this = this;
this.ToastDom.onselectstart = function(){return false;}
this.ToastDom.onmouseover = function(){
_this.zindex = 999;
_this.ToastDomOpacity = 100;
_this.ToastDom.style.filter = 'alpha(opacity:'+ _this.ToastDomOpacity +')';
_this.ToastDom.style.opacity = _this.ToastDomOpacity/100;
_this.ToastDom.style.zIndex = _this.zIndex;
clearInterval(_this.toastTimer);
clearTimeout(_this.toastTimeOut);
}
this.ToastDom.onmouseout = function(){
_this.hiddenToast();
}
};
Toast.prototype.hiddenToast = function(){
clearTimeout(this.toastTimeOut);
var _this = this;
_this.toastTimeOut = setTimeout(function(){
_this.toastTimer = setInterval(
function(){
_this.ToastDomOpacity --;
_this.zIndex --;
_this.ToastDom.style.zIndex = _this.zIndex;
_this.ToastDom.style.filter = 'alpha(opacity:'+ _this.ToastDomOpacity +')';
_this.ToastDom.style.opacity = _this.ToastDomOpacity/100;
if(_this.ToastDomOpacity <= 0)
{
clearInterval(_this.toastTimer);
document.body.removeChild(_this.ToastDom);
toastLsit.shift();
}
},10);
},800);
}
//初始化布局
Toast.prototype.initToastDom = function(){
//新建一个DIV
this.ToastDom = document.createElement("div");
this.ToastDom.innerHTML = this.content;
//然后给这个元素布局
//这个元素的位置应该是 浏览器的最底部 居中的位置,并且在所有元素的顶部 且不能影响其他元素的布局
this.ToastDom.style.position = "fixed";
//背景色
this.ToastDom .style.backgroundColor = "#000";
this.ToastDom .style.color = "#fff";
this.ToastDom .style.minWidth = "200px";
this.ToastDom .style.textAlign = "center";
this.ToastDom .style.padding = "10px";
this.ToastDom .style.borderRadius = "5px";
this.ToastDom .style.cursor = "pointer";
//只有先上树之后才有具体的宽高
document.body.appendChild(this.ToastDom);
if(this.position == "mouse")
{
this.ToastDom.style.top = this.mouseY + 10 + "px";
this.ToastDom.style.left = this.mouseX + 10 + "px";
}
else if(this.position == "top")
{
this.ToastDom.style.top = "25px";
this.ToastDom.style.left = "50%";
this.ToastDom.style.marginLeft = -(getStyle(this.ToastDom,"width") / 2) +"px";
}
else
{
this.ToastDom.style.bottom = "25px";
this.ToastDom.style.left = "50%";
this.ToastDom.style.marginLeft = -(getStyle(this.ToastDom,"width") / 2) +"px";
}
}
function getStyle(obj,name)
{
if(obj.currentStyle)
{
return parseFloat(obj.currentStyle[name]);
}
else
{
return parseFloat(getComputedStyle(obj)[name]);
}
}
//监听鼠标移动事件
document.onmousemove = function(e){
e = e || window.event;
mouseX = e.pageX;
mouseY = e.pageY;
}
})();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>模拟手机吐司</title>
<script type="text/javascript" src="js/Toast.js" ></script>
</head>
<body> <input type="text" />
<button>吐司</button> <div style="height:1500px;"> </div>
<script> document.getElementsByTagName("button")[0].onclick = function(){
Toast(document.getElementsByTagName("input")[0].value); } </script>
</body>
</html>
js 自定义类Android吐司提示框的更多相关文章
- Android消息提示框Toast
Android消息提示框Toast Toast是Android中一种简易的消息提示框.和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,t ...
- JS使用cookie实现DIV提示框只显示一次的方法
本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法.分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第 ...
- 通过自定义window来实现提示框效果
#import <UIKit/UIKit.h>#import <Foundation/Foundation.h> @interface ZMStatuBarHud : NSOb ...
- JS学习笔记 -- 定时器,提示框的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js实现弹出的提示框只弹出一次
<script type="text/javascript"> var ua = navigator.userAgent.toLowerCase(); if (/iph ...
- Android 退出提示框 代码
转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.contex ...
- js自定义类和对象及继承
1.工厂方式 <script type="text/javascript"> function createObject(name){ var p = new Obje ...
- js关于移入移出延迟提示框效果处理
html部分 <div id="div1">我是导航君</div> <div id="div2" style="disp ...
- js 自定义类
将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...
随机推荐
- 安装win10 和win中的一些杂项问题
reg中的 policies是政策/策略, 主要是指 控制面板中的一些控制项目, 其中 explorer是 控制面板的 显示项目... DWORD值是一个32位(4个字节:即双字)长度的数值, 是数字 ...
- Linux配置中文输入法(搜狗输入法)
一.基础知识 在原生ubuntu14.04英文环境系统中只有IBus拼音,真的好难用.由于搜狗输入法确实比Linux系统下其它的中文输入法都要好用得多,所以我决定在我的Ubuntu 14.04系统中安 ...
- npm发包注意
ES6语法的包需要转换为ES5语法 安装bebel npm install --save-dev babel-cli babel-preset-es2015 babel-preset-es2017 b ...
- guxh的python笔记六:类的属性
1,私有属性 class Foo: def __init__(self, x): self.x = x 类的属性在实例化之后是可以更改的: f = Foo(1) print(f.x) # 1 f.x ...
- hdoj5769后缀自动机版本
网上的题解都是后缀数组,我来个后缀自动机题解. 建好后缀自动机后由于后缀自动机是单向的,那么dfs一遍记录各节点的size,要保证一个节点只经过一次才是O(n),否则是O(n^2).表示这个节点及后面 ...
- ranch 源码分析(三)
接上ranch 源码分析(二) 上次讲到了ranch_conns_sup和ranch_acceptors_sup这2个ranch的核心模块,我们接着分析 首先查看ranch_conns_sup.erl ...
- 学习GT一书前九章的体会
学习Gilbarg和Trudinger一书前九章的体会 本书第二章,调和函数的基本性质进行展示.特别的对比较定理有深刻的阐述以及Perron方法的基本说明,并对Wiener准则作了简要说明. 第三章的 ...
- Java反序列化漏洞实现
一.说明 以前去面试被问反序列化的原理只是笼统地答在参数中注入一些代码当其反序列化时被执行,其实“一些代码”是什么代码“反序列化”时为什么就会被执行并不懂:反来在运营商做乙方经常会因为java反反序列 ...
- easyui datebox时间控件如何只显示年月
easyui datebox控件,只显示年月,不显示年月日 需要的效果图如下: 具体的js代码: <script> $(function(){ intiMonthBox('costTime ...
- test pthread code
#include <iostream> #include <pthread.h> using namespace std; ; void * add(void *); pthr ...