• JS所谓的享元模式-->


    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <style>
        .tooltip {
            300px;
            height:300px;
            background:#f00;
        }
    </style>
    <script>
        var TooltipManager = (function(){
            var storedInstance = null;
            var Tooltip = function(){
                this.delayTimeout = null;
                this.delay = 1500;
    
                this.element = document.createElement('div');
                this.element.style.display = 'none';
                this.element.style.position = 'absolute';
                this.element.className = 'tooltip';
                document.getElementsByTagName('body')[0].appendChild(this.element);
            };
    
            Tooltip.prototype = {
                startDelay : function(e,text){
                    if(this.delayTimeout == null){
                        var  that = this;
                        var x = e.clientX;
                        var y = e.clientY;
                        this.delayTimeout = setTimeout(function(){
                            that.show(x,y,text);
                        },this.delay)
                    }
                },
                show : function(x,y,text){
                    clearTimeout(this.delayTimeout);
                    this.delayTimeout = null;
                    this.element.innerHTML = text;
                    this.element.style.left = x + 'px';
                    this.element.style.top = (y+20) + 'px';
                    this.element.style.display = 'block';
                },
                hide : function(){
                    clearTimeout(this.delayTimeout);
                    this.delayTimeout = null;
                    this.elements.style.display = 'none';
                }
            };
    
            return {
                addTooltip : function(targetElement,text){
                    var tt = this.getTooltip();
                    addEvent(targetElement,'mouseover',function(e){ tt.startDelay(e,text)});
                    addEvent(targetElement,'mouseout',function(e){ tt.hide()})
                },
                getTooltip : function(){
                    if(storedInstance == null){
                        storedInstance = new Tooltip();
                    };
                    return storedInstance;
                }
            }
        }())
    </script>
    </body>
    </html>

    直接上代码了

  • 相关阅读:
    下载并使用ASP.NET MVC v1.0 Futures
    关于DataGridView的数据源两个值得注意的小问题
    C++网络编程(二)客户端服务器程序
    C++多态、继承的简单分析
    XML文件解析器TXml
    数组
    CTS类型系统
    光阴不会虚度
    软件的大规模生产
    微创新和山寨的关系
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3428020.html
Copyright © 2020-2023  润新知