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

首頁 > 編程 > JavaScript > 正文

javascript放大鏡效果的簡單實現

2019-11-20 21:30:12
字體:
來源:轉載
供稿:網友

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

捕獲鼠標位置、判斷鼠標位置區域、還有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 id="smallimg">
 <img src="jq/m.jpg" alt=""/>
 <div id="showimg"> </div>
</div>
<div id="bigimg"> </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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 突泉县| 曲麻莱县| 基隆市| 石楼县| 且末县| 定远县| 新宾| 鄂州市| 武清区| 仁怀市| 南投市| 彩票| 金沙县| 阿合奇县| 普宁市| 广元市| 房山区| 射洪县| 江西省| 六盘水市| 东丰县| 嘉祥县| 青河县| 长阳| 略阳县| 汝城县| 高阳县| 诏安县| 安多县| 图木舒克市| 称多县| 普格县| 东阿县| 封开县| 桐乡市| 乌什县| 阳江市| 英吉沙县| 临西县| 勃利县| 全南县|