(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. Intellij IDEA的下载安装与破解

    一,下载与安装 1.官网下载:https://www.jetbrains.com/idea/ 2.选择Windows对应版本安装 二,破解 1.下载破解补丁:http://idea.lanyus.co ...

  2. linux安装nord,卸载nord源

    需要提前准备好:能使用的sock代理. 1.在这里 https://nordvpn.com/zh/download/linux/ 下载初始安装包,这包不是真正的软件,而是会给你添加一个源,大概为了安全 ...

  3. python 获取subprocess进程执行后返回值

    test.py #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr= ...

  4. python检测服务器端口

    import socket sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) try: sk.conne ...

  5. 英语发音规则---I字母常见发音组合有哪些

    英语发音规则---I字母常见发音组合有哪些 一.总结 一句话总结: I/y在开音节中发/aɪ/,例如:bite /baɪt/ n. 咬 I/y在闭音节中发 /ɪ/,例如:clinic /'klɪnɪk ...

  6. WCF传输过大的数据导致失败的解决办法

    WCF传输过大的数据导致失败的解决办法   WCF服务默认是不配置数据传输的限制大小的,那么默认的大小好像是65535B,这才65KB左右,如果希望传输更大一些的数据呢,就需要手动指定一下缓冲区的大小 ...

  7. C++句柄解析

    C++句柄类解析 引题:在C++中,对于运行时类型识别问题.在程序中使用引用或者指针在运行时动态识别对象类型.然而使用指针或者引用却增加了用户负担(在继承体系中,没有明确的基类到派生类的转换,必须用户 ...

  8. Socket中SO_REUSEADDR详解

    1.一般来说,一个端口释放后会等待两分钟之后才能再被使用,SO_REUSEADDR是让端口释放后立即就可以被再次使用. SO_REUSEADDR用于对TCP套接字处于TIME_WAIT状态下的sock ...

  9. .net core部署到Ubuntu碰到的问题

    数据库连接的时候,会报错“MySql.Data.MySqlClient.MySqlException:“The host localhost does not support SSL connecti ...

  10. Django的下载与项目的创建

    一.Django的下载安装 Django官网下载页面 二.DOS窗口下的django项目从创建和启动 1. DOS窗口下载Django pip3 install django==1.11.9 2.DO ...