国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript放大鏡效果的簡單實現

2024-05-06 15:57:08
字體:
來源:轉載
供稿:網友
這篇文章主要是對javascript放大鏡效果的簡單實現進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助

這個效果并不難,要點是位置和比例設置,

捕獲鼠標位置、判斷鼠標位置區域、還有onmouseover事件、onmousemove事件、onmouseout事件

設置顯示大圖的比例,小圖上顯示的切圖比例都要弄準確點,最好是2倍啦,4倍啦。

主要注意寬度,我這里的圖片m.jpg是1440X900的....

復制代碼 代碼如下:


<!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=utf-8">
<title>放大鏡效果</title>
<style type="text/css">
*{margin:0;padding:0;}
#smallimg{width:360px;float:left;position:relative;border:1px solid red;}
#smallimg img{ width:360px;}
#bigimg{float:left;width:400px;height:400px;margin-left:40px;border:1px solid #ccc;display:none;}
#showimg{width:100px;height:100px;background:#fff;cursor:move; position:absolute;border:1px solid #666;opacity:0.5;filter:alpha(opacity=50);display:none;}
</style>
</head>
<body>
<div>
 <img src="jq/m.jpg" />
 <div> </div>
</div>
<div> </div>


<script type="text/javascript">
var $=function(id){return typeof id=="string"?document.getElementById(id):id}
var smallimg = $("smallimg");
var showimg = $("showimg");//濾鏡圖片
var bigimg = $("bigimg");
var small_url = smallimg.getElementsByTagName("img")[0].getAttribute("src");
var show_half = maxWidth = maxHeight = 0;
smallimg.onmouseover = function(){
 showimg.style.display = "block";
 bigimg.style.display = "inline";
 show_half = showimg.offsetHeight/2;
 maxWidth = smallimg.clientWidth - showimg.offsetWidth;
 maxHeight = smallimg.clientHeight - showimg.offsetHeight;
 //上面兩個變量指明showimg允許活動的區域
};
smallimg.onmousemove = function(e){
 var e=window.event?window.event:e;
 var num=bigimg.clientWidth/showimg.clientWidth;
 var Top = e.clientY - smallimg.offsetTop - show_half;
 var Left = e.clientX - smallimg.offsetLeft - show_half;
 //獲取當前移動的showimg位置 計算方法是 鼠標坐標 - 最外面容器的坐標 - 盒子的寬(高)的/2
 Top = Top<0?0:Top>maxHeight?maxHeight:Top;
 Left = Left<0?0:Left>maxWidth?maxWidth:Left;
 showimg.style.top = Top + "px";
 showimg.style.left = Left + "px";
 bigimg.style.background = "url("+small_url+") -"+Left*num+"px -"+Top*num+"px no-repeat";
};
smallimg.onmouseout = function(){
 showimg.style.display="none";
 bigimg.style.background ="";
 bigimg.style.display="none"
};
</script>
</body>
</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 门源| 确山县| 泰宁县| 双辽市| 论坛| 内丘县| 遂川县| 平安县| 连州市| 丹棱县| 闵行区| 成武县| 睢宁县| 凉城县| 济南市| 新安县| 岳阳市| 喀喇沁旗| 陇川县| 和顺县| 海原县| 乐业县| 承德县| 宝清县| 伊宁市| 永州市| 乌兰浩特市| 财经| 铅山县| 仪陇县| 乌恰县| 泸水县| 武川县| 灵石县| 乐亭县| 房山区| 堆龙德庆县| 皋兰县| 太保市| 台前县| 遵义市|