首页 > web前端 > js教程 > 用js实现的仿sohu博客更换页面风格(简单版)_javascript技巧

用js实现的仿sohu博客更换页面风格(简单版)_javascript技巧

WBOY
发布: 2016-05-16 19:16:30
原创
1112 人浏览过
复制代码 代码如下:

prowin.js

/**
* @author ldgmc
*/
function PopWin(id,width,title){
    this.id=id;
    this.width=width;
    this.title=title;
    this.createIns=function(){
        var ins=document.createElement("div");
        ins.className="popWin";
        ins.id=this.id;
        ins.style.width=this.width+'px';
        return ins;    
    };
    this.items=new Array();
    this.instance=this.createIns();
    this.init=function(){
        this.addTitle(this.title);
        document.body.appendChild(this.instance);
    }
    this.addTitle=function(title){
        var titleDiv=document.createElement("div");
        titleDiv.className="title";
        var titleText=document.createTextNode(title);
        titleDiv.appendChild(titleText);
        this.instance.appendChild(titleDiv);
    }
    this.addItem=function(id,title){
        this.items[id]={};
        this.items[id]["title"]=ldg.common.setClassName("div","itemTitle");
        this.items[id]["title"].appendChild(document.createTextNode(title));
        this.items[id]["content"]=ldg.common.setClassName("div","itemContent");
        this.items[id]["title"].id=id;
        ldg.event.addEvent(this.items[id]["title"],"mouseover",this.mouseover.bindAsEventListener(this))
        ldg.event.addEvent(this.items[id]["title"],"mouseout",this.mouseout.bindAsEventListener(this));
        ldg.event.addEvent(this.items[id]["title"],"click",this.click.bindAsEventListener(this));
        this.instance.appendChild(this.items[id]["title"]);
        this.instance.appendChild(this.items[id]["content"]);
    }
    this.addItemContent=function(parentId,imgUri,cssUri,isLast){
        var itemDiv=ldg.common.setClassName("div","item");
        var itema=document.createElement("a");
        itema.href="#";
        var itemImg=document.createElement("img");
        itemImg.src=imgUri;
        itemImg.css=cssUri;
        ldg.event.addEvent(itemImg,"click",this.changeLink.bindAsEventListener(this));    
        itema.appendChild(itemImg);
        itemDiv.appendChild(itema);
        this.items[parentId]["content"].appendChild(itemDiv);
        if(isLast){
            this.items[parentId]["content"].appendChild(document.createElement("br"));
        }

    }                                          
    this.changeLink=function(e){
        var eObj=e.srcElement || e.target;
        var link=document.getElementsByTagName("link");
        link[1].href=eObj.css;
    }
    this.mouseover=function(e){
        var eObj=e.srcElement || e.target;
        eObj.style.backgroundColor="#e5e5e5";
    }
    this.mouseout=function(e){
        var eObj=e.srcElement || e.target;
        eObj.style.backgroundColor="#EEEEEE";
    }
    this.click=function(e){
        var eObj=e.srcElement || e.target;
        var item=this.items[eObj.id]["content"];
        item.style.display=="block"?item.style.display="none":item.style.display="block";
    }
    this.init(); 

复制代码 代码如下:

ldg.js
var ldg=window.ldg || {}
Array.prototype.append=function(item){
    this[this.length]=item
}
Function.prototype.bindAsEventListener=function(obj){
    var _method=this;
    return function(event){
        return _method.call(obj,event || window.event);
    }
}
ldg.event={
    addEvent:function(obj,evType,fun){
        if(obj.addEventListener){
            obj.addEventListener(evType,fun,false);
            return true;
        }else if(obj.attachEvent){
            obj.attachEvent("on"+evType,fun);
            return true;
        }else{return false}
    }
}
ldg.common={
    $:function(id){
        return document.getElementById(id);    
    },
    setClassName:function(eleType,className){
        var element=document.createElement(eleType)
        element.className=className;
        return element;
    }
}

复制代码 代码如下:


    
        
        testBlog
        
        
        <script></script>
        <script></script>
        <script> <BR> window.onload=function(){ <BR> var pop=new PopWin(1,440,"标题"); <BR> pop.addItem(1,"默认主题"); <BR> pop.addItem(2,"2222"); <BR> pop.addItem(3,"3333"); <BR> pop.addItem(4,"4444"); <BR> pop.addItemContent(1,"images/1.gif","css/bg2.css"); <BR> pop.addItemContent(1,"images/2.gif","css/bg3.css"); <BR> pop.addItemContent(1,"images/3.gif","css/bg4.css"); <BR> pop.addItemContent(1,"images/4.gif","css/bg5.css"); <BR> pop.addItemContent(1,"images/5.gif","css/bg6.css"); <BR> pop.addItemContent(1,"images/1.gif","css/bg7.css"); <BR> pop.addItemContent(1,"images/2.gif","css/bg8.css"); <BR> pop.addItemContent(1,"images/3.gif","css/bg9.css"); <BR> pop.addItemContent(1,"images/4.gif","css/bg0.css"); <BR> pop.addItemContent(1,"images/5.gif","css/bg4.css"); <BR> pop.addItemContent(1,"images/6.gif","css/bg2.css",true); <BR> pop.addItemContent(2,"images/7.gif","css/bg8.css"); <BR> pop.addItemContent(2,"images/8.gif","css/bg6.css"); <BR> pop.addItemContent(2,"images/9.gif","css/bg3.css"); <BR> pop.addItemContent(2,"images/10.gif","css/bg2.css"); <BR> pop.addItemContent(2,"images/11.gif","css/bg6.css"); <BR> pop.addItemContent(2,"images/12.gif","css/bg4.css",true); <BR> pop.addItemContent(3,"images/13.gif","css/bg9.css"); <BR> pop.addItemContent(3,"images/14.gif","css/bg1.css"); <BR> pop.addItemContent(3,"images/15.gif","css/bg7.css",true); <BR> pop.addItemContent(4,"images/4.gif","css/bg9.css"); <BR> pop.addItemContent(4,"images/5.gif","css/bg3.css"); <BR> pop.addItemContent(4,"images/1.gif","css/bg5.css"); <BR> pop.addItemContent(4,"images/2.gif","css/bg2.css"); <BR> pop.addItemContent(4,"images/3.gif","css/bg3.css"); <BR> pop.addItemContent(4,"images/4.gif","css/bg0.css"); <BR> pop.addItemContent(4,"images/5.gif","css/bg7.css"); <BR> pop.addItemContent(4,"images/1.gif","css/bg4.css"); <BR> pop.addItemContent(4,"images/2.gif","css/bg3.css"); <BR> pop.addItemContent(4,"images/4.gif","css/bg7.css"); <BR> pop.addItemContent(4,"images/5.gif","css/bg1.css"); <BR> pop.addItemContent(4,"images/4.gif","css/bg1.css",true); <BR> } <br><br> </script>
    
    

    


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板