這個效果并不難,要點是位置和比例設置,
捕獲鼠標位置、判斷鼠標位置區域、還有onmouseover事件、onmousemove事件、onmouseout事件
設置顯示大圖的比例,小圖上顯示的切圖比例都要弄準確點,最好是2倍啦,4倍啦。
主要注意寬度,我這里的圖片m.jpg是1440X900的....
<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>
新聞熱點
疑難解答