zoukankan      html  css  js  c++  java
  • 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>

    直接上代码了

  • 相关阅读:
    内聚和耦合的举例
    OneZero第四周第五次站立会议(2016.4.15)
    OneZero第四周第四次站立会议(2016.4.14)
    OneZero团队Beta发布剧透
    PSP(4.6——4.12)以及周记录
    关于“内聚和耦合”
    CSV 注入实战
    BurpSuite 一些小技巧
    博客园URL跳转钓鱼
    【Demo 0005】Android 资源
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3428020.html
Copyright © 2011-2022 走看看