This article describes the method of JS+CSS to imitate the browser's web character search function. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows: <html>
<head>
<title>JS+CSS imitated web character search function</title>
<style type=text/css>
BODY {
FONT-SIZE: 12px; LETTER-SPACING: 1pt; LINE-HEIGHT: 22px; MARGIN-LEFT: 5pt; MARGIN-TOP: 5pt
}
#scontentmain {
HEIGHT: 30px; POSITION: absolute; TOP: 28px; WIDTH: 228px
}
#scontentbar {
BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; CURSOR: hand; HEIGHT: 15px; PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; PADDING-RIGHT: 1px; PADDING-TOP: 1px; POSITION: absolute; TOP: 5px; WIDTH: 100%
}
#scontentsub {
POSITION: absolute; TOP: 28px; WIDTH: 100%
}
A.a1:hover {
BORDER-BOTTOM: #4a8ff2 1px solid; BORDER-LEFT: #4a8ff2 1px solid; BORDER-RIGHT: #4a8ff2 1px solid; BORDER-TOP: #4a8ff2 1px solid; LINE-HEIGHT: 13pt; WIDTH: 83px; 12px;: 1869FE; align: center
}
A.a1:link {
COLOR: #ffffff; TEXT-DECORATION: none; 12px;align: center
}
TD {
FONT-SIZE: 12px
}
.pos {
POSITION: relative
}
</style>
</head>
<body>
<br>China<br>Jinzhou<br>Farewell to the World War<br>Luo<br>Huang Loss<br>Jianghuai<br>BMW<br>Exaggeration<br>America<br>Ajax<br>ASP<br>JAVA<br>JQUERY
<SCRIPT language=JavaScript>
var dragapproved=false
var zcor,xcor,ycor
function drag_onclick() {
if (search1.drag.checked == true ) dragapproved=false
else dragapproved=true
}
function movescontentmain(){
if (event.button==1&&dragapproved&&search1.drag.checked == true){
zcor.style.pixelLeft=tempvar1+event.clientX-xcor
zcor.style.pixelTop=tempvar2+event.clientY-ycor
leftpos=document.all.scontentmain.style.pixelLeft-document.body.scrollLeft
toppos=document.all.scontentmain.style.pixelTop-document.body.scrollTop
return false
}
}
function dragscontentmain(){
if (!document.all)
Return
if (event.srcElement.id=="scontentbar"){
dragapproved=true
zcor=scontentmain
tempvar1=zcor.style.pixelLeft
tempvar2=zcor.style.pixelTop
xcor=event.clientX
ycor=event.clientY
document.onmousemove=movescontentmain
}
}
document.onmousedown=dragscontentmain
document.onmouseup=new Function("dragapproved=false")
function aa(e)
{
if (e==0) alert("/n- = Page search engine 1.0 = /n/n/n- Rewrite some code/n- Fix the bug of mouse drag jump, /n- Keep selecting whether to drag...");
return;
}
function cose()
{
if(confirm("This window will be closed soon. If you confirm, please press OK! /n/n If you want to reopen this window, please refresh the page."))
scontentmain.style.display='none'
else
scontentmain.style.display=''
}
document.write('<div id="scontentmain"><form name="search1" onSubmit="return findInPage(this.Word.value);"><div id="scontentbar">Click the check box below, hold down and drag</div><div id="scontentsub"" style="visibility: show"><table cellpadding="2" cellpacing="0" bordercolorlight="#000000" bordercolordark="#FFFFF" bgcolor="#0099CC" class=font1><tr bgcolor="#2D96FF"><td colspan="2" align="center"><input type="text" name="Word" size="30" onChange="n = 0;" onFocus=window.document.search1.Word.value="" style="background-image: url()" ></td></tr><tr bgcolor="#CCCCCC"><td> <input type="checkbox" name="drag" value="Search" LANGUAGE=javascript onclick="return drag_onclick()">Drag the window with the mouse<a href="#top"></a></td><td align="center"><input type="submit" name="Submit" value="Search"></td></tr></form></table></div></DIV>');
var w=document.body.clientWidth-450
var h=190
w+=document.body.scrollLeft
h+=document.body.scrollTop
var leftpos=w
var toppos=h
scontentmain.style.left=w
scontentmain.style.top=h
function ondisplay(){
if (scontentsub.style.display=='')
scontentsub.style.display='none'
else
scontentsub.style.display==''
}
function offdisplay(){
if (scontentsub.style.display=='none')
scontentsub.style.display=''
else
scontentsub.style.display=='none'
}
function staticize(){
w2=document.body.scrollLeft+leftpos
h2=document.body.scrollTop+toppos
scontentmain.style.left=w2
scontentmain.style.top=h2
}
window.onscroll=staticize
function GetWord()
{
var tr
}
function FindWord(w)
{
for (var i=0;i<WordList.options.length;i++)
if (WordList.options[i].text==w)
{
WordList.options[i].selected=true
Output.value=WordExpln[i]
RESULT.style.visibility="visible"
return true
}
return false
}
var canDrag=0,canGetWord=0
var oldtop=0, oldscrolltop=0
var WordExpln=new Array()
var G_xmlHTTP
document.onmousemove = GetWord
var NS4 = (document.layers);
var IE4 = (document.all);
var win = this;
var n = 0;
function findInPage(str)
{
var txt, i, found;
if (str == "")
return false;
if (NS4)
{
if (!win.find(str))
while(win.find(str, false, true))
n++;
else
n++;
if (n == 0) alert(str + " ... The text you are looking for does not exist. /n /n Please try to enter the keywords in the page and look up again! /n /n and please be careful not to enter "?" or ":" to find too!!!");
}
if (IE4)
{
txt = win.document.body.createTextRange();
for (i = 0; i <= n && (found = txt.findText(str)) != false; i++)
{
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
if (found)
{
txt.moveStart("character", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++; }
else
{
if (n > 0)
{
n = 0;
findInPage(str);
}
else
alert(str + "... The text you are looking for does not exist. /n /n Please try to enter the keywords in the page and look up again! /n /n and please be careful not to enter "?" or ":" to find too!!!");
}
}
return false;
}
self.onError=null;
currentX = currentY = 0;
whichIt = null;
lastScrollX = 0; lastScrollY = 0;
NS = (document.layers) ? 1 : 0;
IE = (document.all) ? 1: 0;
</SCRIPT>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.