(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吐司提示框的更多相关文章

  1. Android消息提示框Toast

    Android消息提示框Toast Toast是Android中一种简易的消息提示框.和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,t ...

  2. JS使用cookie实现DIV提示框只显示一次的方法

    本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法.分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第 ...

  3. 通过自定义window来实现提示框效果

    #import <UIKit/UIKit.h>#import <Foundation/Foundation.h> @interface ZMStatuBarHud : NSOb ...

  4. JS学习笔记 -- 定时器,提示框的应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. js实现弹出的提示框只弹出一次

    <script type="text/javascript"> var ua = navigator.userAgent.toLowerCase(); if (/iph ...

  6. Android 退出提示框 代码

    转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.contex ...

  7. js自定义类和对象及继承

    1.工厂方式 <script type="text/javascript"> function createObject(name){ var p = new Obje ...

  8. js关于移入移出延迟提示框效果处理

    html部分 <div id="div1">我是导航君</div> <div id="div2" style="disp ...

  9. js 自定义类

    将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...

随机推荐

  1. Windows7下安装redmine-3.4.6

    Redmine 是一个开源的.基于Web的项目管理和缺陷跟踪工具.Redmine建立在Ruby on Rails(一个用于开发数据库驱动的网络应用程序的完整框架,基于计算机软件语言Ruby,给程序开发 ...

  2. jsp/servlet学习三之会话管理初解

    由于http的无状态性,使得会话管理或会话跟踪成为web应用开发一个无可避免的主题.默认下,一个web服务器无法区分一个http请求是否为第一次访问.例如,一个web邮件应用要求用户登陆后才能查看邮件 ...

  3. The Guideline of Setting Up Samba Server on linux(Ubuntu)

    The Guideline of Setting Up Samba Server on linux(Ubuntu) From terminate command window, install the ...

  4. 在1-10中选择一个数,输出x+xx+xxx+xxx....x之和,如:数字为2,则2+22=24

    代码: package bao; import java.util.Random; public class a { public static void main(String[] args) { ...

  5. pip3 freeze导出依赖包 --Python3

    1.导出依赖包到*.txt文件 pip3 freeze>blueflag.txt 2.导出后的结果

  6. C#反射详解

    http://blog.csdn.net/educast/article/details/2894892(转) 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏 ...

  7. Confluence 6 服务器硬件要求指南

    服务器管理员可以通过本页面的指南来对在运行 Confluence 评估版本的最小服务器硬件需求进行评估.应为实际的服务器负载是很难进行预测的,所以最好的办法是通过实际运行一个 Confluence 实 ...

  8. National Property CodeForces - 875C (拓扑排序)

    大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...

  9. Kali linux 2016.2(Rolling)里安装OpenVAS

    不多说,直接上干货! 本博文,是在Kali 2.0 linux里,安装OpenVAS. 前言 OpenVAS是一款开放式的漏洞评估工具,主要用来检测目标网络或主机的安全性.与安全焦点的X-Scan工具 ...

  10. js实现下雪雪花特效

    两种下雪特效: 1. <script type="text/javascript"> (function($){ $.fn.snow = function(option ...