用javascript實現在小方框中瀏覽大圖的代碼

來源:互聯網
上載者: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>無標題文檔</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 表示1倍速度,即原速
var x,y // 滑鼠點下去時背景的座標
var x_new,y_new //位移
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); // 計算位移量
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>
今天在玩 google earth 4.0b,發現 Print Screen 下來的圖片很大,如果直接放在網頁上,因為尺寸太大又不合適,又不想壓縮圖片的尺寸,於是乎就想到了這種方法,沒想到實現起來比預想的要容易。呵呵,該死的是,這段代碼還相容 firefox。
</body>
</html>
今天在玩 google earth 4.0b,發現 Print Screen 下來的圖片很大,如果放在網頁不合適,又不想壓縮圖片的尺寸,於是乎就想到了這種方法,沒想到實現起來比預想的要容易。呵呵,該死的是,這段代碼還相容 firefox。

--------------------------------------------------------------------------------------
2006.6.20 修改:

·添加了滾動的範圍,不會出現背景
·用到onmousemove事件,圖片即時隨滑鼠移動而移動

<!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>無標題文檔</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; //速度
var picWidth = 1280; // 大圖的寬高
var picHeight = 971;
var x,y // 滑鼠點下去時背景的座標
var x_new,y_new //位移
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); // 計算位移量
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 (y2<-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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.