Use javascript to browse the big picture code in a small box

Source: Internet
Author: User

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Style type = "text/css">
<! --
# Pic {
Height: 300px;
Width: 420px;
Border: 3px solid # ccc;
Background-image: url (http://www.happyshow.org/sample/20060619/3.jpg );
Background-repeat: no-repeat;
Background-position: 0px 0px;
Background-color: #333;
Cursor: crosshair;
}
-->
</Style>
<Script type = "text/javascript">
<! --
Var p = new Array ();
Var speed = 1.0; // 1 indicates a speed of 1 times, that is, the original speed.
Var x, y // coordinates of the background when the mouse clicks down
Var x_new, y_new // displacement
Function getmouseposition (event)
{
If (document. all)
{
X = document. body. scrollLeft + event. clientX;
Y = document. body. scrollTop + event. clientY;
} Else
{
X = event. layerX;
Y = event. layerY;
}
}
Function setmouseposition (event)
{
If (document. getElementById ('pic '). style. backgroundPosition. length = 0)
{Document. getElementById ('pic '). style. backgroundPosition = "0px 0px ";}
P = document. getElementById ('pic '). style. backgroundPosition. split ("")
If (document. all)
{
X_new = document. body. scrollLeft + event. clientX;
Y_new = document. body. scrollTop + event. clientY;
} Else
{
X_new = event. layerX;
Y_new = event. layerY;
}

X2 = (speed * (x_new-x) + parseInt (p [0]). toString (10); // calculates the displacement
Y2 = (speed * (y_new-y) + parseInt (p [1]). toString (10 );
Document. getElementById ('pic '). style. backgroundPosition = x2 + "px" + y2 + "px ";
}
-->
</Script>
</Head>
<Body>
<Div id = "pic" onmousedown = "getmouseposition (event)" onmouseup = "setmouseposition (event)"> </div>
Today, I am playing google earth 4.0b and found that the Print Screen has a large image. If I put it on a webpage directly, it is not suitable because it is too large and I don't want to compress the image size, as a result, I thought of this method and did not expect it to be easier to implement it than I expected. Oh, damn it, this code is also compatible with firefox.
</Body>
</Html>
Today, I am playing google earth 4.0b and found that the Print Screen has a large image size. If I don't want to compress the image size when I put it on a webpage, I thought of this method, it was easier to implement than expected. Oh, damn it, this code is also compatible with firefox.

Bytes --------------------------------------------------------------------------------------
2006.6.20 modification:

· Added a rolling range without a background
· The onmousemove event is used to move an image as the mouse moves in real time.

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Style type = "text/css">
<! --
# Pic {
Width: 420px;
Height: 300px;
Border: 3px solid # ccc;
Background-image: url (http://www.happyshow.org/sample/20060619/3.jpg );
Background-repeat: no-repeat;
Background-position: 0px 0px;
Cursor: move;
}
-->
</Style>
<Script type = "text/javascript">
<! --
Var p = new Array ();
Var speed = 0.05; // speed
Var picWidth = 1280; // the width and height of the big image
Var picHeight = 971;
Var x, y // coordinates of the background when the mouse clicks down
Var x_new, y_new // displacement
Var haveclick = false;
Function getmouseposition (event)
{
If (document. all)
{
X = document. body. scrollLeft + event. clientX;
Y = document. body. scrollTop + event. clientY;
} Else
{
X = event. layerX;
Y = event. layerY;
}
Haveclick = true;
}
Function movestop ()
{
Haveclick = false;
}
Function movestart (event)
{
If (haveclick)
{
If (document. getElementById ('pic '). style. backgroundPosition. length = 0)
{Document. getElementById ('pic '). style. backgroundPosition = "0px 0px ";}
P = document. getElementById ('pic '). style. backgroundPosition. split ("")
If (document. all)
{
X_new = document. body. scrollLeft + event. clientX;
Y_new = document. body. scrollTop + event. clientY;
} Else
{
X_new = event. layerX;
Y_new = event. layerY;
}

X2 = (speed * (x_new-x) + parseInt (p [0]). toString (10); // calculates the displacement
Y2 = (speed * (y_new-y) + parseInt (p [1]). toString (10 );

If (x2 <-picWidth + 420) x2 =-picWidth + 420;
If (y2> 0) y2 = 0;
If (x2> 0) x2 = 0;
If (2' <-picHeight + 300) y2 =-picHeight + 300;
Document. getElementById ('pic '). style. backgroundPosition = x2 + "px" + y2 + "px ";
}
}
-->
</Script>
</Head>
<Body>
<Div id = "pic" onmousedown = "getmouseposition (event)" onmousemove = "movestart (event)" onmouseup = "movestop ()" onmouseout = "movestop () "> </div>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.