(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. Spring Boot Log4j2 日志学习

    简介 Java 中比较常用的日志工具类,有: Log4j. SLF4j. Commons-logging(简称jcl). Logback. Log4j2(Log4j 升级版). Jdk Logging ...

  2. 搭建Elasticsearch平台

    https://cloud.tencent.com/developer/article/1189282 https://blog.csdn.net/qq_34021712/article/detail ...

  3. SAP Hybris电子商务最新功能

    SAP Hybris电子商务最新功能   SAP Hybris 电子商务6.0中国加速器是专为中国市场设计的电子商务平台,可满足企业在全渠道销售和订单履行方面的所有需求.新版的中国加速器基于SAP H ...

  4. 01.什么是Vue.js

    VUE.JS 什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的, ...

  5. Nginx 配置负载均衡

    nginx负载均衡配置,主要是proxy_pass,upstream的使用. 注意问题,多台机器间session的共享问题. 不用session,用户cookie.或者用redis替代session. ...

  6. vue-update-表单形式复写方法上传图片

    handleSave() { const formData = new FormData(); /* eslint-disable */ for (let key in this.dataInfo) ...

  7. (19)ThreadPoolExecutor线程池

    # 线程池 # 实例化线程池 ThreadPoolExcutor (推荐cpu_count*(n+1)) # 异步提交任务 submit / map # 阻塞直到任务完成 shutdown # 获取子 ...

  8. 用GraphX分析伴生网络(二)

    8. 过滤噪声边 在当前的伴生关系中,边的权重是基于一对概念同时出现在一篇论文中的频率来计算的.这种简单的权重机制的问题在于:它并没有对一对概念同时出现的原因加以区分,有时一对概念同时出现是由于它们具 ...

  9. python学习:数据类型

    python有两种索引方式.一种从左至右,下标从0开始:一种从右至左,下标从-1开始. python有六种数据类型: 不可变数据(四个):Number(数字).String(字符串).Tuple(元组 ...

  10. 第5天(半天)【shell编程初步、grep及正则表达式】

    第5天(半天)[shell编程初步.grep及正则表达式] shell编程初步(01)_recv shell脚本:文本文件 #!:/bin/bash #!:/usr/bin/python #!:/us ...