Baidu space pop-up effect analysis_javascript skills
Since I started using Firefox browser, I have almost developed a habit of using WebDeveloper to download and analyze the JS and CSS of some beautiful websites for learning.
The pop-up windows and drag-and-drop effects of Baidu Space look pretty good. Many well-known websites now use this technology. Below I will send out my down js code. I have analyzed part of it, but there are still many things that I don’t understand. There are no comments. I hope an expert can help me explain it. I am a beginner, so please tell me if I am wrong.
Let me make a statement. This code is for learning purposes only, and the technical copyright belongs to Baidu.
Mainly a file called: popup.js, as follows:
/**//*********************************************** popup.js**************************************************/
//Add a push method to the array Array
//Add a push method to the end of the array Object
if(!Array.prototype.push)
{
Array.prototype.push=function ()
{
var startLength=this.length;
for(var i =0;i
this[startLength i]=arguments[i];
}
return this.length;
}
};
//Process the parameters of the G function
function G()
{
//Define an array to save the parameters
var elements=new Array();
//Loop to analyze the content of parameters in G
for(var i=0;i
var element=arguments[i];
//If the parameter type is string, get the object with this parameter as the ID
if(typeof element=='string')
{
element=document.getElementById(element);
}
//If the length of the parameter is 1
if(arguments.length==1)
{
return element;
}
//Add the object to the end of the array
elements.push(element);
};
return elements;
};
Function.prototype.bind=function (object)
{
var __method=this;
return function ()
{
__method.apply(object,arguments);
};
};
//Bind event
Function.prototype.bindAsEventListener=function (object)
{
var __method=this;
return function (event){__method.call(object,event||window.event);};
};
Object.extend=function (destination,source)
{
for(property in source)
{
destination[property]=source[property ];
};
return destination;
};
if(!window.Event)
{
var Event=new Object();
};
Object.extend(
Event,
{
observers:false,
element:function (event)
{
return event.target||event.srcElement;
},
isLeftClick:function (event)
{
return (((event.which)&&(event.which==1) )||((event.button)&&(event.button==1)));
},
pointerX:function (event)
{
return event.pageX| |(event.clientX (document.documentElement.scrollLeft||document.body.scrollLeft));
},
pointerY:function (event)
{
return event.pageY| |(event.clientY (document.documentElement.scrollTop||document.body.scrollTop));
},
stop:function (event)
{
if(event.preventDefault )
{
event.preventDefault();
event.stopPropagation();
}
else
{
event.returnValue=false;
event.cancelBubble =true;
};
},
findElement:function (event,tagName)
{
var element=Event.element(event);
while(element .parentNode&&(!element.tagName||(element.tagName.toUpperCase()!=tagName.toUpperCase())))
element=element.parentNode;
return element;
},
_observeAndCache:function (element,name,observer,useCapture)
{
if(!this.observers)
this.observers=[];
if(element.addEventListener)
{
this.observers.push([element,name,observer,useCapture]);
element.addEventListener(name,observer,useCapture);
}
else if(element.attachEvent)
{
this.observers.push([element,name,observer,useCapture]);
element.attachEvent('on' name,observer);
};
},
unloadCache:function ()
{
if(!Event.observers)
return;
for(var i=0;i
Event.stopObserving.apply(this,Event.observers[i]);
Event.observers[i][0]=null;
};
Event.observers=false ;
},
observe:function (element,name,observer,useCapture)
{
var element=G(element);
useCapture=useCapture||false;
if(name=='keypress'&&(navigator.appVersion.match(/Konqueror|Safari|KHTML/)||element.attachEvent))
name='keydown';
this._observeAndCache(element,name,observer,useCapture);
},
stopObserving:function (element,name,observer,useCapture)
{
var element=G(element);
useCapture=useCapture||false;
if(name=='keypress'&&(navigator.appVersion.match(/Konqueror|Safari|KHTML/)||element.detachEvent))
name='keydown';
if(element.removeEventListener)
{
element.removeEventListener(name,observer,useCapture);
}
else if(element.detachEvent)
{
element.detachEvent('on' name,observer);
};
}
}
);
Event.observe(window,'unload',Event.unloadCache,false);
var Class=function ()
{
var _class=function ()
{
this.initialize.apply(this,arguments);
};
for(i=0;i
superClass=arguments[i];
for(member in superClass.prototype)
{
_class.prototype[member]=superClass.prototype[member];
};
};
_class.child=function ()
{
return new Class(this);
};
_class.extend=function (f)
{
for(property in f)
{
_class.prototype[property]=f[property];
};
};
return _class;
};
//改变百度空间的最顶端和最低端的p的id值
//如果flag为begin,则为弹出状态的id值
//如果flag为end,则为非弹出状态的id值,即原本的id值
function space(flag)
{
if(flag=="begin")
{
var ele=document.getElementById("ft");
if(typeof(ele)!="#ff0000"&&ele!=null)
ele.id="ft_popup";
ele=document.getElementById("usrbar");
if(typeof(ele)!="undefined"&&ele!=null)
ele.id="usrbar_popup";
}
else if(flag=="end")
{
var ele=document.getElementById("ft_popup");
if(typeof(ele)!="undefined"&&ele!=null)
ele.id="ft";
ele=document.getElementById("usrbar_popup");
if(typeof(ele)!="undefined"&&ele!=null)
ele.id="usrbar";
};
};
//**************************************************Popup类弹出窗口***************************************************************
var Popup=new Class();
Popup.prototype={
//弹出窗口中框架的name名称
iframeIdName:'ifr_popup',
initialize:function (config)
{
//---------------弹出对话框的配置信息------------------
//contentType:设置内容区域为什么类型:1为另外一个html文件 | 2为自定义html字符串 | 3为confirm对话框 | 4为alert警告对话框
//isHaveTitle:是否显示标题栏
//scrollType:设置或获取对话框中的框架是否可被滚动
//isBackgroundCanClick:弹出对话框后,是否允许蒙布后的所有元素被点击。也就是如果为false的话,就会有全屏蒙布,如果为true的话,就会去掉全屏蒙布
//isSupportDraging:是否支持拖拽
//isShowShadow:是否现实阴影
//isReloadOnClose:是否刷新页面,并关闭对话框
//width:宽度
//height:高度
this.config=Object.extend({contentType:1,isHaveTitle:true,scrollType:'yes',isBackgroundCanClick:false,isSupportDraging:true,isShowShadow:true,isReloadOnClose:true,width:400,height:300},config||{});
//----------------对话框的参数值信息------------------------
//shadowWidth :阴影的宽度
//contentUrl :html链接页面
//contentHtml :html内容
//callBack :回调的函数名
//parameter :回调的函数名中传的参数
//confirmCon :对话框内容
//alertCon :警告框内容
//someHiddenTag:页面中需要隐藏的元素列表,以逗号分割
//someHiddenEle: ID list of elements that need to be hidden (the difference from someToHidden is: someHiddenEle uses getElementById, while someToHidden uses getElementByTagName, which contains objects)
//overlay:
//coverOpacity: cover Cloth transparency value
this.info={shadowWidth:4,title:"",contentUrl:"",contentHtml:"",callBack:null,parameter:null,confirmCon:"",alertCon:"",someHiddenTag :"select,object,embed",someHiddenEle:"",overlay:0,coverOpacity:40};
//Set the color cColor: the background of the mask, bColor: the background of the content area, tColor: title The color of the column and border, wColor: the background color of the font
this.color={cColor:"#EEEEEE",bColor:"#FFFFFF",tColor:"#709CD2",wColor:"#FFFFFF"};
this.dropClass=null;
//Used to place the hidden object list, the first call in the hiddenTag method
this.someToHidden=[];
//If there is no title bar, dragging is not supported
if(!this.config.isHaveTitle)
{
this.config.isSupportDraging=false;
}
//Initialization
this.iniBuild();
},
//Set configuration information and parameter content
setContent:function (arrt,val)
{
if(val! ='')
{
switch(arrt)
{
case 'width':this.config.width=val;
break;
case 'height':this. config.height=val;
break;
case 'title':this.info.title=val;
break;
case 'contentUrl':this.info.contentUrl=val;
break;
case 'contentHtml':this.info.contentHtml=val;
break;
case 'callBack':this.info.callBack=val;
break;
case ' parameter':this.info.parameter=val;
break;
case 'confirmCon':this.info.confirmCon=val;
break;
case 'alertCon':this.info.alertCon =val;
break;
case 'someHiddenTag':this.info.someHiddenTag=val;
break;
case 'someHiddenEle':this.info.someHiddenEle=val;
break;
case 'overlay':this.info.overlay=val;
};
};
},
iniBuild:function ()
{
G ('dialogCase')?G('dialogCase').parentNode.removeChild(G('dialogCase')):function (){};
var op=document.createElement('span');
op .id='dialogCase';
document.body.appendChild(op);
},
build:function ()
{
//Set the z of the full screen mask -index
var baseZIndex=10001 this.info.overlay*10;
//Set the z-index of the pop-up window on the mask (2 values higher than the z-index of the mask)
var showZIndex=baseZIndex 2;
//Define the frame name
this.iframeIdName='ifr_popup' this.info.overlay;
//Set the main path of the image
var path ="http://img.baidu.com/hi/img/";
//Close button
var close='';
//Use filter settings Transparency of the object
var cB='filter: alpha(opacity=' this.info.coverOpacity ');opacity:' this.info.coverOpacity/100 ';';
//Set full screen Cover
var cover='
//Set the pop-up main window settings
var mainBox='
| ||||
' close ' | ||||
//如果有蒙布
if(!this.config.isBackgroundCanClick)
{
G('dialogCase').innerHTML=cover mainBox;
G('dialogBoxBG').style.height=document.body.scrollHeight;
}
else
{
G('dialogCase').innerHTML=mainBox;
}
Event.observe(G('dialogBoxClose'),"click",this.reset.bindAsEventListener(this),false);
//如果支持拖动,则设置拖动处理
if(this.config.isSupportDraging)
{
dropClass=new Dragdrop(this.config.width,this.config.height,this.info.shadowWidth,this.config.isSupportDraging,this.config.contentType);
G("dialogBoxTitle").style.cursor="move";
};
this.lastBuild();
},
lastBuild:function ()
{
//设置confim对话框的具体内容
var confirm='
' this.info.confirmCon '
';
//设置alert对话框的具体内容
var alert='
' this.info.alertCon '
';var baseZIndex=10001 this.info.overlay*10;
var coverIfZIndex=baseZIndex 4;
//判断内容类型决定窗口的主内容区域应该显示什么
if(this.config.contentType==1)
{
var openIframe="";
var coverIframe="
";
G("dialogBody").innerHTML=openIframe coverIframe;
}
else if(this.config.contentType==2)
{
G("dialogBody").innerHTML=this.info.contentHtml;
}
else if(this.config.contentType= =3)
{
G("dialogBody").innerHTML=confirm;Event.observe(G('dialogOk'),"click",this.forCallback.bindAsEventListener(this),false);
Event.observe(G('dialogCancel'),"click",this.close.bindAsEventListener(this),false);
}
else if(this.config.contentType==4)
{
G("dialogBody").innerHTML=alert;
Event.observe(G('dialogYES'),"click",this.close.bindAsEventListener(this),false);
};
},
//Reload the height and content of the pop-up window
reBuild:function ()
{
G('dialogBody').height=G('dialogBody' ).clientHeight;
this.lastBuild();
},
show:function ()
{
//Hide some elements specified in info
this .hiddenSome();
//Center the pop-up window
this.middle();
//Set the shadow
if(this.config.isShowShadow)
this.shadow();
},
//Set the callback function
forCallback:function ()
{
return this.info.callBack(this.info.parameter);
},
//Set a shadow for the pop-up window
shadow:function ()
{
var oShadow=G('dialogBoxShadow');
var oDialog=G('dialogBox'); oShadow['style']['position']="absolute";
oShadow['style']['background']="#000";
oShadow['style']['display'] ="";
oShadow['style']['opacity']="0.2";
oShadow['style']['filter']="alpha(opacity=20)";
oShadow['style']['top']=oDialog.offsetTop this.info.shadowWidth;
oShadow['style']['left']=oDialog.offsetLeft this.info.shadowWidth;
oShadow[ 'style']['width']=oDialog.offsetWidth;oShadow['style']['height']=oDialog.offsetHeight;
},
// Center the pop-up window
middle:function ()
{
if(!this.config.isBackgroundCanClick)
G('dialogBoxBG').style.display='';
var oDialog=G('dialogBox' );
oDialog['style']['position']="absolute";
oDialog['style']['display']='';
var sClientWidth=document.body.clientWidth ;
var sClientHeight=document.body.clientHeight;
var sScrollTop=document.body.scrollTop;
//alert("document.body.clientWidth = " sClientWidth "ndocument.body.clientHeight" sClientHeight) ;
var sleft=(document.body.clientWidth/2)-(oDialog.offsetWidth/2);
var iTop=-80 (sClientHeight/2 sScrollTop)-(oDialog.offsetHeight/2);
var sTop=iTop>0?iTop:(sClientHeight/2 sScrollTop)-(oDialog.offsetHeight/2);
//alert("var iTop=-80 (sClientHeight/2 sScrollTop)-(oDialog.offsetHeight/ 2);n" "sClientHeight is " sClientHeight "nsScrollTop is " sScrollTop);
//alert("iTop is " iTop);
if(sTop<1)
sTop="20";
if(sleft<1)
sleft="20";
oDialog['style']['left']=sleft;
oDialog['style']['top']=sTop ;
//alert("sleft is " sleft);
//alert("sTop is " sTop);
},
//Refresh the page and close the current pop-up window
reset:function ()
{
if(this.config.isReloadOnClose)
{
top.location.reload();
};
this.close( );
},
//Close the current pop-up window
close:function ()
{
G('dialogBox').style.display='none';
if(!this.config.isBackgroundCanClick)
G('dialogBoxBG').style.display='none';
if(this.config.isShowShadow)
G('dialogBoxShadow'). style.display='none';
G('dialogBody').innerHTML='';
this.showSome();
},
//Hide someHiddenTag and all elements in someHiddenEle
hiddenSome:function ()
{
//Hide all elements in someHiddenTag
var tag=this.info.someHiddenTag.split(",");
if(tag.length==1&&tag[0]=="")
{
tag.length=0;
}
for(var i=0;i
this.hiddenTag(tag[i]);
};
//Hide all comma-separated ID elements in someHiddenEle
var ids=this.info.someHiddenEle.split(",");
if(ids.length==1&&ids[ 0]=="")
ids.length=0;
for(var i=0;i
this.hiddenEle(ids[i]) ;
};
//Change the id value of the top and bottom p to the id value of the pop-up state, see the implementation of space
space("begin");
},
//Hide a group of elements
hiddenTag:function (tagName)
{
var ele=document.getElementsByTagName(tagName);
if(ele!=null)
{
for(var i=0;i
if(ele[i].style.display!="none"&&ele[i].style.visibility!=' hidden')
{
ele[i].style.visibility='hidden';
this.someToHidden.push(ele[i]);
};
};
};
},
//Hide a single element
hiddenEle:function (id)
{
var ele=document.getElementById(id);
if( typeof(ele)!="undefined"&&ele!=null)
{
ele.style.visibility='hidden';
this.someToHidden.push(ele);
}
},
//Display all hidden elements saved in someToHidden
//And restore the top and bottom p to the original id value
showSome:function ()
{
for(var i=0;i
this.someToHidden[i].style.visibility='visible';
};
space( "end");
}
};
//************************ *************************************Dragdrop class (drag action)****** *************************************************** ****
var Dragdrop=new Class();
Dragdrop.prototype={
initialize:function (width,height,shadowWidth,showShadow,contentType)
{
this.dragData=null;
this.dragDataIn=null;
this.backData=null;
this.width=width;
this.height=height;
this. shadowWidth=shadowWidth;
this.showShadow=showShadow;
this.contentType=contentType;
this.IsDraging=false;
this.oObj=G('dialogBox');
Event. observe(G('dialogBoxTitle'),"mousedown",this.moveStart.bindAsEventListener(this),false);
},
moveStart:function (event)
{
this .IsDraging=true;
if(this.contentType==1)
{
G("iframeBG").style.display="";
G("iframeBG").style. width=this.width;
G("iframeBG").style.height=this.height;
};
Event.observe(document,"mousemove",this.mousemove.bindAsEventListener(this) ,false);
Event.observe(document,"mouseup",this.mouseup.bindAsEventListener(this),false);
Event.observe(document,"selectstart",this.returnFalse,false);
this.dragData={x:Event.pointerX(event),y:Event.pointerY(event)};
this.backData={x:parseInt(this.oObj.style.left),y:parseInt (this.oObj.style.top)};
},
mousemove:function (event)
{
if(!this.IsDraging)
return ;
var iLeft=Event.pointerX(event)-this.dragData["x"] parseInt(this.oObj.style.left);
var iTop=Event.pointerY(event)-this.dragData["y"] parseInt(this.oObj.style.top);
if(this.dragData["y"]
else if(this.dragData["y"]>parseInt(this.oObj.style.top) 25)
iTop=iTop 12;
this.oObj.style.left=iLeft;
this. oObj.style.top=iTop;
if(this.showShadow)
{
G('dialogBoxShadow').style.left=iLeft this.shadowWidth;
G('dialogBoxShadow'). style.top=iTop this.shadowWidth;
};
this.dragData={x:Event.pointerX(event),y:Event.pointerY(event)};
document.body.style. cursor="move";
},
mouseup:function (event)
{
if(!this.IsDraging)
return ;
if(this.contentType ==1)
G("iframeBG").style.display="none";
document.onmousemove=null;
document.onmouseup=null;
var mousX=Event.pointerX(event)-(document.documentElement.scrollLeft||document.body.scrollLeft);
var mousY=Event.pointerY(event)-(document. documentElement.scrollTop||document.body.scrollTop);
if(mousX<1||mousY<1||mousX>document.body.clientWidth||mousY>document.body.clientHeight)
{
this.oObj.style.left=this.backData["x"];
this.oObj.style.top=this.backData["y"];
if(this.showShadow)
{
G('dialogBoxShadow').style.left=this.backData.x this.shadowWidth;
G('dialogBoxShadow').style.top=this.backData.y this.shadowWidth;
};
};
this.IsDraging=false;
document.body.style.cursor="";
Event.stopObserving(document,"selectstart",this.returnFalse,false);
},
returnFalse:function ()
{
return false;
}
};
Do the above to the popup.js file A reference, the following is the code of the html page that tests this js file. You can go back and test it easily.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PopUpTest.aspx.cs" Inherits="PopUpTest" %>
html>
코드가 잔뜩 있어요! 그를 끌고 간 사람은 무슨 일이 일어나고 있는지 몰랐나요? 위의 클래스 기능이 어떻게 수행되는지 정확히 모르겠습니다. 의견이 없으면 기본적으로 명확하지 않습니다. 좀 더 자세히 분석해 보는 시간을 갖도록 할게요! 다른 메모도 작성할 시간을 찾으십시오. 이해가 된다면 더 많은 조언을 부탁드립니다. 더 자세히 설명할수록 좋습니다.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











On May 15, Baidu Apollo held Apollo Day 2024 in Wuhan Baidu Luobo Automobile Robot Zhixing Valley, comprehensively demonstrating Baidu's major progress in autonomous driving over the past ten years, bringing technological leaps based on large models and a new definition of passenger safety. With the world's largest autonomous vehicle operation network, Baidu has made autonomous driving safer than human driving. Thanks to this, safer, more comfortable, green and low-carbon travel methods are turning from ideal to reality. Wang Yunpeng, vice president of Baidu Group and president of the Intelligent Driving Business Group, said on the spot: "Our original intention to build autonomous vehicles is to satisfy people's growing yearning for better travel. People's satisfaction is our driving force. Because safety, So beautiful, we are happy to see

In today's smartphone market, screen quality has become one of the key indicators to measure the overall performance of a mobile phone. iQOO's Neo series has always been committed to providing users with excellent gaming experience and visual enjoyment. The latest product iQOO Neo9SPro+ uses a "Three Good Eye Protection Gaming Screen". Next, let's take a look at the quality of this screen. How brilliant. iQOO Neo9S Pro+ is equipped with a 1.5 KOLED e-sports direct screen, which supports flagship LTPO adaptive refresh rate from 1Hz to 144Hz, which means that it can achieve ultra-low power standby state when displaying static content, and it can also be intelligent during gaming. Switch to dynamic high from 90Hz to 144Hz

According to news from this site on May 7, on May 6, Robin Li, founder, chairman and CEO of Baidu, led a team to visit China National Petroleum Corporation (hereinafter referred to as "PetroChina") in Beijing and met with directors of China National Petroleum Corporation Chairman and Party Secretary Dai Houliang held talks. The two parties had in-depth exchanges on strengthening cooperation and promoting the deep integration of the energy industry with digital intelligence. PetroChina will accelerate the construction of a digital China Petroleum Corporation, strengthen cooperation with Baidu Group, promote the in-depth integration of the energy industry with digital intelligence, and make greater contributions to ensuring national energy security. Robin Li said that the "intelligent emergence" and core capabilities of understanding, generation, logic, and memory displayed by large models have opened up a broader space for imagination for the combination of cutting-edge technology and oil and gas business. Always

In this fast-paced era, OPPO Find X7 can use its imaging power to let us savor every beautiful moment in life. Whether it's magnificent mountains, rivers, lakes, or seas, warm family gatherings, or encounters and surprises on the street, it can help you record them with "unparalleled" picture quality. From the outside, the camera Deco design of Find It looks very recognizable and has a high-end feel. The inside is also unique, starting with the basic hardware configuration. FindX7 maintains the previous

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

According to news on May 31, blogger @ibinguniverse broke the news today that the Chinese version of Samsung Galaxy S24 series mobile phones will support Google search. The blogger did not disclose the specific launch time. According to Samsung’s previous introduction, the Samsung Galaxy S24 series has been equipped with many high-level AI capabilities, AI-based practical functions such as input, translation, recorder, notes, and cameras, to provide users with a more convenient and efficient comprehensive experience. Different from the overseas version, most of the AI functions of the Samsung Galaxy S24 series are provided by domestic manufacturers, such as Baidu. Previously reported, Galaxy AI deeply integrates multiple capabilities of Baidu Wenxin large model, which can provide end-side enabled call and translation functions, as well as intelligent summary brought by generative AI.

In the previous article of Kyushu Fengshen "Storm Sea View Room", we introduced the game player platform based on Kyushu Fengshen's new Ice Storm 360 water-cooled radiator, CH780 chassis, and PX1200G power supply "three-piece set". So today, we will bring you a pure white ITX personalized combination with the new ITX chassis-CH160, the new Assassin 4S radiator, and the DQ750M-V3LWH power supply. In the past two years, Kyushu Fengshen pure white ITX electromechanical loose kit has attracted more and more players and ordinary DIY users who like small cases. Many netizens left messages through the background and privately messaged us on new media platforms, hoping that we could recommend some ITX products that are "easy to use, not expensive, and easy to install." So the three products we selected today

This article introduces six popular AI tools, including Douyin Doubao, Wenxin Yige, Tencent Zhiying, Baidu Feipiao EasyDL, Baidu AI Studio and iFlytek Spark Cognitive Large Model. These tools cover different functions such as text creation, image generation, video editing, and AI model development. Choosing the right AI tool requires consideration of factors such as functional requirements, technical level, and cost budget. These tools provide convenient and efficient solutions for individuals and businesses in need of AI assistance.
